OSDN Git Service

fix build system ofmoflib1 and saisei1
[moflib/moflib.git] / moflib-1.0 / src / mof / FactoryMethod.hpp
1 #pragma once
2 #include <boost/function/function0.hpp>
3
4 namespace mof
5 {
6     //TODO タイプリストとかで、そのままのインタフェースでより汎用的にする
7     //非const参照を渡したい場合は?(constructorにそれはありえない)
8
9
10 //{{{ FactoryMethod
11     template < class T > 
12     class FactoryMethod
13     {
14     public: 
15         FactoryMethod( const boost::function<T* (void)>& factory)
16             : m_factory(factory){}
17
18         T* operator() () const{ return m_factory(); }
19         
20         template<class T>
21         operator FactoryMethod<T>( ) const
22         {
23             return FactoryMethod<T>(m_factory); 
24         }
25
26
27
28     private: 
29         boost::function<T* (void)> m_factory;
30     };
31
32         
33 //}}}
34 //{{{ Constructor0
35     template < class T > 
36     class Constructor0
37     {
38     public: 
39         T* operator() ( ) const {return new T();}
40     };
41 //}}}
42 //{{{ Constructor1
43     template <class T , class P1> 
44     class Constructor1 : protected Constructor0<T>
45     {
46     protected:
47         P1 p1;
48     public: 
49         Constructor1(const P1& p1_)
50             : p1(p1_)
51         {}
52
53         T* operator() ( ) const {return new T(p1);}
54     };
55 //}}}
56 //{{{ Constructor2
57     template <class T , class P1 , class P2> 
58     class Constructor2 : protected Constructor1<T , P1>
59     {
60     protected:
61         P2 p2;
62     public: 
63         Constructor2(const P1& p1_ , const P2& p2_)
64             : Constructor1(p1_) , p2(p2_)
65         {}
66
67         T* operator() ( ) const {return new T(p1 , p2);}
68     };
69 //}}}
70 //{{{ Constructor3
71     template <class T , class P1 , class P2 , class P3> 
72     class Constructor3 : protected Constructor2<T , P1 , P2>
73     {
74     protected:
75         P3 p3;
76     public: 
77         Constructor3(const P1& p1_ , const P2& p2_ , const P3& p3_)
78             : Constructor2(p1_ , p2_) , p3(p3_)
79         {}
80
81         T* operator() ( ) const {return new T(p1 , p2 , p3);}
82     };
83 //}}}
84 //{{{ Constructor4
85     template <class T , class P1 , class P2 , class P3 , class P4> 
86     class Constructor4 : protected Constructor3<T , P1 , P2 , P3>
87     {
88     protected:
89         P4 p4;
90     public: 
91         Constructor4(const P1& p1_ , const P2& p2_ , const P3& p3_ , const P4& p4_)
92             : Constructor3(p1_ , p2_ , p3_) , p4(p4_)
93         {}
94
95         T* operator() ( ) const {return new T(p1 , p2 , p3 , p4);}
96     };
97 //}}}
98 //{{{ Constructor5
99     template <class T , class P1 , class P2 , class P3 , class P4, class P5> 
100     class Constructor5 : protected Constructor4<T , P1 , P2 , P3, P4>
101     {
102     protected:
103         P5 p5;
104     public: 
105         Constructor5(const P1& p1_ , const P2& p2_ , const P3& p3_ , const P4& p4_, const P5& p5_)
106             : Constructor4(p1_ , p2_ , p3_, p4_), p5(p5_)
107         {}
108
109         T* operator() ( ) const {return new T(p1 , p2 , p3 , p4, p5);}
110     };
111 //}}}
112  
113 //{{{ makeFactoryMethod
114     template<class T>
115     FactoryMethod<T>
116     makeFactoryMethod()
117     {
118         return FactoryMethod<T>( Constructor0<T>() );
119     }
120         
121     template<class T , class P1>
122     FactoryMethod<T>
123     makeFactoryMethod(const P1& p1)
124     {
125         return FactoryMethod<T>( Constructor1<T , P1>(p1) );
126     }
127     
128     template<class T , class P1 , class P2>
129     FactoryMethod<T>
130     makeFactoryMethod(const P1& p1 , const P2& p2)
131     {
132         return FactoryMethod<T>( Constructor2<T , P1 , P2>(p1 , p2) );
133     }
134     
135     template<class T , class P1 , class P2 , class P3>
136     FactoryMethod<T>
137     makeFactoryMethod(const P1& p1 , const P2& p2 , const P3& p3)
138     {
139         return FactoryMethod<T>( Constructor3<T , P1 , P2 , P3>(p1 , p2 , p3) );
140     }
141         
142     template<class T , class P1 , class P2 , class P3 , class P4>
143     FactoryMethod<T>
144     makeFactoryMethod(const P1& p1 , const P2& p2 , const P3& p3 , const P4& p4)
145     {
146         return FactoryMethod<T>( Constructor4<T , P1 , P2 , P3 , P4>(p1 , p2 , p3 , p4) );
147     }
148         
149         template<class T , class P1 , class P2 , class P3 , class P4, class P5>
150     FactoryMethod<T>
151     makeFactoryMethod(const P1& p1 , const P2& p2 , const P3& p3 , const P4& p4, const P5& p5)
152     {
153         return FactoryMethod<T>( Constructor5<T , P1 , P2 , P3 , P4 , P5>(p1 , p2 , p3 , p4, p5) );
154     }
155
156 //}}}
157
158
159 }