1 | /*
|
---|
2 | * PrototypeFactory_impl.hpp
|
---|
3 | *
|
---|
4 | * Created on: Jan 3, 2011
|
---|
5 | * Author: heber
|
---|
6 | */
|
---|
7 |
|
---|
8 | // include config.h
|
---|
9 | #ifdef HAVE_CONFIG_H
|
---|
10 | #include <config.h>
|
---|
11 | #endif
|
---|
12 |
|
---|
13 | #include <boost/preprocessor/debug/assert.hpp>
|
---|
14 | #include <boost/preprocessor/facilities/empty.hpp>
|
---|
15 | #include <boost/preprocessor/iteration/local.hpp>
|
---|
16 | #include <boost/preprocessor/punctuation/comma.hpp>
|
---|
17 | #include <boost/preprocessor/punctuation/comma_if.hpp>
|
---|
18 | #include <boost/preprocessor/punctuation/paren.hpp>
|
---|
19 | #include <boost/preprocessor/seq/elem.hpp>
|
---|
20 | #include <boost/preprocessor/seq/size.hpp>
|
---|
21 | #include <boost/preprocessor/stringize.hpp>
|
---|
22 |
|
---|
23 | // check for required type_seq
|
---|
24 | #ifdef type_seq
|
---|
25 | #define type_seq_size BOOST_PP_SEQ_SIZE(type_seq)
|
---|
26 | #else
|
---|
27 | BOOST_PP_ASSERT_MSG( 0 , \
|
---|
28 | "std::cout << \"missing type_seq for creating PrototypeFactory<T>\" << std::endl")
|
---|
29 | #define type_seq ()
|
---|
30 | #endif
|
---|
31 |
|
---|
32 | // check for required FactoryNAME
|
---|
33 | #ifndef FactoryNAME
|
---|
34 | BOOST_PP_ASSERT_MSG( 0 , \
|
---|
35 | "std::cout << \"missing FactoryNAME for creating PrototypeFactory<T>\" << std::endl")
|
---|
36 | #define FactoryNAME NONE
|
---|
37 | #endif
|
---|
38 |
|
---|
39 | // check for required Abstract_Interface_Class
|
---|
40 | #ifndef Abstract_Interface_Class
|
---|
41 | BOOST_PP_ASSERT_MSG( 0 , \
|
---|
42 | "std::cout << \"missing Abstract_Interface_Class for creating PrototypeFactory<T>\" << std::endl")
|
---|
43 | #define Abstract_Interface_Class NONE
|
---|
44 | #endif
|
---|
45 |
|
---|
46 | // check for required Abstract_Encapsulation_Class
|
---|
47 | #ifndef Abstract_Encapsulation_Class
|
---|
48 | BOOST_PP_ASSERT_MSG( 0 , \
|
---|
49 | "std::cout << \"missing Abstract_Encapsulation_Class for creating PrototypeFactory<T>\" << std::endl")
|
---|
50 | #define Abstract_Encapsulation_Class NONE
|
---|
51 | #endif
|
---|
52 |
|
---|
53 | // check for type_name_space, is only optional
|
---|
54 | #ifndef type_name_space
|
---|
55 | #define type_name_space BOOST_PP_EMPTY()
|
---|
56 | #endif
|
---|
57 |
|
---|
58 | #ifndef type_name_space_suffix
|
---|
59 | #define type_name_space_suffix BOOST_PP_EMPTY()
|
---|
60 | #endif
|
---|
61 |
|
---|
62 |
|
---|
63 | /** Functions that allows to print a given seq of elements in the way of
|
---|
64 | * std::map from strings to enums.
|
---|
65 | *
|
---|
66 | * e.g. let "seq" be defined as
|
---|
67 | * #define seq (one)(two)(three)(four)
|
---|
68 | *
|
---|
69 | * then we use
|
---|
70 | * #define BOOST_PP_LOCAL_MACRO(n) seqitems_as_enum_key_map(seqsize, n, seq, EnumMap, test::)
|
---|
71 | * #define BOOST_PP_LOCAL_LIMITS (0, BOOST_PP_SEQ_SIZE(seq)-1 )
|
---|
72 | * #include BOOST_PP_LOCAL_ITERATE()
|
---|
73 | *
|
---|
74 | * which expands by the preprocessor to:
|
---|
75 | * EnumMap[one] = test::one;
|
---|
76 | * EnumMap[two] = test::two;
|
---|
77 | * EnumMap[three] = test::three;
|
---|
78 | * EnumMap[four] = test::four;
|
---|
79 | *
|
---|
80 | */
|
---|
81 | #define seqitems_as_enum_key_map(z,n,_seq_with_elements, _map, _keytype, _key_name_space, _type_name_space, _type_suffix) \
|
---|
82 | _map [ _key_name_space \
|
---|
83 | BOOST_PP_SEQ_ELEM(n, _seq_with_elements) \
|
---|
84 | ] = _keytype< _type_name_space BOOST_PP_SEQ_ELEM(n, _seq_with_elements) _type_suffix > ();
|
---|
85 |
|
---|
86 | /** Functions that allows to print a given seq of elements in the way of
|
---|
87 | * std::map from strings to enums.
|
---|
88 | *
|
---|
89 | * e.g. let "seq" be defined as
|
---|
90 | * #define seq (one)(two)(three)(four)
|
---|
91 | *
|
---|
92 | * then we use
|
---|
93 | * #define BOOST_PP_LOCAL_MACRO(n) seqitems_as_enum_value_map(seqsize, n, seq, EnumMap, typid BOOST_PP_LPAREN() super:: , BOOST_PP_RPAREN().name() , test::)
|
---|
94 | * #define BOOST_PP_LOCAL_LIMITS (0, BOOST_PP_SEQ_SIZE(seq)-1 )
|
---|
95 | * #include BOOST_PP_LOCAL_ITERATE()
|
---|
96 | *
|
---|
97 | * which expands by the preprocessor to:
|
---|
98 | * EnumMap[ typeid ( super:: one ) .name() ] = test::one;
|
---|
99 | * EnumMap[ typeid ( super:: two ) .name() ] = test::two;
|
---|
100 | * EnumMap[ typeid ( super:: three ) .name() ] = test::three;
|
---|
101 | * EnumMap[ typeid ( super:: four ) .name() ] = test::four;
|
---|
102 | *
|
---|
103 | * or we use
|
---|
104 | * #define BOOST_PP_LOCAL_MACRO(n) seqitems_as_enum_value_map(seqsize, n, seq, EnumMap, BOOST_PP_STRINGIZE BOOST_PP_LPAREN() , BOOST_PP_RPAREN() , test::)
|
---|
105 | * #define BOOST_PP_LOCAL_LIMITS (0, BOOST_PP_SEQ_SIZE(seq)-1 )
|
---|
106 | * #include BOOST_PP_LOCAL_ITERATE()
|
---|
107 | *
|
---|
108 | * which expands by the preprocessor to:
|
---|
109 | * EnumMap[ "one" ] = test:: one;
|
---|
110 | * EnumMap[ "two" ] = test:: two;
|
---|
111 | * EnumMap[ "three" ] = test:: three;
|
---|
112 | * EnumMap[ "four" ] = test:: four;
|
---|
113 | */
|
---|
114 | #define seqitems_as_enum_value_map(z,n,_seq_with_elements, _map, _type_specifier, _type_suffix, _enum_name_space) \
|
---|
115 | _map [ _type_specifier \
|
---|
116 | BOOST_PP_SEQ_ELEM(n, _seq_with_elements) \
|
---|
117 | _type_suffix \
|
---|
118 | ] = _enum_name_space BOOST_PP_SEQ_ELEM(n, _seq_with_elements);
|
---|
119 |
|
---|
120 |
|
---|
121 | template <>
|
---|
122 | void PrototypeFactory<Abstract_Interface_Class>::FillEnumTable()
|
---|
123 | {
|
---|
124 | // insert all known (enum, string) keys
|
---|
125 | #define BOOST_PP_LOCAL_MACRO(n) seqitems_as_enum_value_map(~, n, type_seq, enums, BOOST_PP_STRINGIZE BOOST_PP_LPAREN() , BOOST_PP_RPAREN() , FactoryTypeList<Abstract_Interface_Class>::)
|
---|
126 | #define BOOST_PP_LOCAL_LIMITS (0, BOOST_PP_SEQ_SIZE(type_seq)-1 )
|
---|
127 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
128 |
|
---|
129 | // insert all known (type, enum) keys
|
---|
130 | #define BOOST_PP_LOCAL_MACRO(n) seqitems_as_enum_value_map(~, n, type_seq, types, typeid BOOST_PP_LPAREN() type_name_space , type_name_space_suffix BOOST_PP_RPAREN() .name(), FactoryTypeList<Abstract_Interface_Class>::)
|
---|
131 | #define BOOST_PP_LOCAL_LIMITS (0, BOOST_PP_SEQ_SIZE(type_seq)-1 )
|
---|
132 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
133 |
|
---|
134 | // create invert mapping (string, enum)
|
---|
135 | for (PrototypeFactory<Abstract_Interface_Class>::EnumMap::const_iterator iter = enums.begin();
|
---|
136 | iter != enums.end();
|
---|
137 | ++iter) {
|
---|
138 | names.insert(make_pair(iter->second, iter->first));
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | void FactoryNAME::FillPrototypeTable()
|
---|
143 | {
|
---|
144 | // fill PrototypeTable
|
---|
145 | #define BOOST_PP_LOCAL_MACRO(n) seqitems_as_enum_key_map(~, n, type_seq, PrototypeTable, new Abstract_Encapsulation_Class , FactoryTypeList<Abstract_Interface_Class>::, type_name_space, type_name_space_suffix )
|
---|
146 | #define BOOST_PP_LOCAL_LIMITS (0, BOOST_PP_SEQ_SIZE(type_seq)-1 )
|
---|
147 | #include BOOST_PP_LOCAL_ITERATE()
|
---|
148 | }
|
---|
149 |
|
---|
150 | void FactoryNAME::EmptyPrototypeTable()
|
---|
151 | {
|
---|
152 | for (InstanceTable::iterator iter = PrototypeTable.begin();
|
---|
153 | !PrototypeTable.empty();
|
---|
154 | iter = PrototypeTable.begin()) {
|
---|
155 | delete (iter->second);
|
---|
156 | PrototypeTable.erase(iter);
|
---|
157 | }
|
---|
158 | PrototypeTable.clear();
|
---|
159 | }
|
---|