Jpp - the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JAutoMap.hh
Go to the documentation of this file.
1 #ifndef __JTOOLS__JAUTOMAP__
2 #define __JTOOLS__JAUTOMAP__
3 
4 #include <map>
5 
6 #include "JLang/JTypeList.hh"
7 #include "JLang/JType.hh"
8 #include "JLang/JNullType.hh"
9 
10 
11 /**
12  * \author mdejong
13  */
14 
15 namespace JTOOLS {}
16 namespace JPP { using namespace JTOOLS; }
17 
18 namespace JTOOLS {
19 
20  using JLANG::JTypeList;
21  using JLANG::JType;
22  using JLANG::JNullType;
23 
24 
25  /**
26  * \cond NEVER
27  * Forward declaration for specialisation of class JAutomate.
28  * \endcond
29  */
30  template<class JKey_t, class JValue_t>
31  class JAutoMap;
32 
33 
34 
35  /**
36  * Auxiliary class for automatic element creation.
37  *
38  * The template argument corresponds to the data type of the elements in the map.\n
39  * There should be a corresponding constructor
40  * <pre>
41  * JElement_t::JElement_t(JType<T>);
42  * </pre>
43  * for each data type <tt>T</tt> to be inserted in the map.
44  */
45  template<class JElement_t>
46  struct JAutomate {
47  /**
48  * Get element.
49  *
50  * \param type data type
51  * \return element
52  */
53  template<class T>
54  static JElement_t getElement(JType<T> type)
55  {
56  return JElement_t(type);
57  }
58  };
59 
60 
61  /**
62  * Specialisation of class JAutomate for class JAutoMap.
63  *
64  * By default, the creation of elements is transfered back to the map
65  * by calling member method JAutoMap::getAutoElement.
66  */
67  template<class JKey_t, class JValue_t>
68  struct JAutomate< JAutoMap<JKey_t, JValue_t> > {
69 
72 
73  /**
74  * Get element.
75  *
76  * \param type data type
77  * \return element
78  */
79  template<class T>
81  {
82  return automap_type::getAutoElement(type);
83  }
84  };
85 
86 
87  /**
88  * Wrapper class around std::map for automatic insertion of elements based on data types.
89  */
90  template<class JKey_t, class JValue_t>
91  class JAutoMap :
92  public std::map<JKey_t, JValue_t>
93  {
94  public:
95 
97  typedef typename map_type::value_type value_type;
98 
99  using map_type::insert;
100 
101  /**
102  * Default constructor.
103  */
105  {}
106 
107 
108  /**
109  * Creation of a map element for given data type.
110  *
111  * This method should be overloaded for the requested data types
112  * if the method insert() is used without argument.
113  *
114  * \param type data type
115  * \return element
116  */
117  template<class T>
118  static value_type getAutoElement(JType<T> type);
119 
120 
121  /**
122  * Insert list of data types.
123  */
124  template<class T>
125  void insert()
126  {
128  }
129 
130 
131  /**
132  * Insert list of data types.
133  *
134  * \param automate type defined element
135  */
136  template<class T, class JElement_t>
137  void insert(const JAutomate<JElement_t>& automate)
138  {
139  insert(JType<T>(), automate);
140  }
141 
142  protected:
143  /**
144  * Insertion of single data type.
145  *
146  * \param type data type
147  * \param automate element automate by data type
148  */
149  template<class T, class JElement_t>
150  void insert(JType<T> type,
151  const JAutomate<JElement_t>& automate)
152  {
153  map_type::insert(automate.getElement(type));
154  }
155 
156 
157  /**
158  * Recursive insertion of data types.
159  *
160  * \param typelist type list
161  * \param automate element automate by data type
162  */
163  template<class JHead_t, class JTail_t, class JElement_t>
165  const JAutomate<JElement_t>& automate)
166  {
167  insert(JType<JHead_t>(), automate);
168  insert(JType<JTail_t>(), automate);
169  }
170 
171 
172  /**
173  * Template specialisation to terminate insertion of data types.
174  *
175  * \param type null type
176  * \param automate element automate by data type
177  */
178  template<class JElement_t>
180  const JAutomate<JElement_t>& automate)
181  {}
182  };
183 }
184 
185 #endif
std::map< JKey_t, JValue_t > map_type
Definition: JAutoMap.hh:96
map_type::value_type value_type
Definition: JAutoMap.hh:97
JAutoMap< JKey_t, JValue_t > automap_type
Definition: JAutoMap.hh:70
static JElement_t getElement(JType< T > type)
Get element.
Definition: JAutoMap.hh:54
Auxiliary class for automatic element creation.
Definition: JAutoMap.hh:46
Auxiliary class for a type holder.
Definition: JType.hh:19
void insert(const JAutomate< JElement_t > &automate)
Insert list of data types.
Definition: JAutoMap.hh:137
static value_type getElement(JType< T > type)
Get element.
Definition: JAutoMap.hh:80
Type list.
Definition: JTypeList.hh:22
static value_type getAutoElement(JType< T > type)
Creation of a map element for given data type.
Auxiliary class for no type definition.
Definition: JNullType.hh:19
void insert(JType< JTypeList< JHead_t, JTail_t > > typelist, const JAutomate< JElement_t > &automate)
Recursive insertion of data types.
Definition: JAutoMap.hh:164
void insert(JType< JNullType > type, const JAutomate< JElement_t > &automate)
Template specialisation to terminate insertion of data types.
Definition: JAutoMap.hh:179
Wrapper class around std::map for automatic insertion of elements based on data types.
Definition: JAutoMap.hh:31
void insert()
Insert list of data types.
Definition: JAutoMap.hh:125
JAutoMap()
Default constructor.
Definition: JAutoMap.hh:104
void insert(JType< T > type, const JAutomate< JElement_t > &automate)
Insertion of single data type.
Definition: JAutoMap.hh:150