Jpp
 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  * Forward declaration of class JAutoMap for specialisation of class JAutomate.
27  */
28  template<class JKey_t, class JValue_t>
29  class JAutoMap;
30 
31 
32  /**
33  * Auxiliary class for automatic element creation.
34  *
35  * The template argument corresponds to the data type of the elements in the map.
36  * There should be a corresponding constructor <tt>JElement_t::JElement_t(JType<T>)</tt>
37  * for each data type <tt>T</tt> to be inserted in the map.
38  */
39  template<class JElement_t>
40  struct JAutomate {
41  /**
42  * Get element.
43  *
44  * \param type data type
45  * \return element
46  */
47  template<class T>
48  static JElement_t getElement(JType<T> type)
49  {
50  return JElement_t(type);
51  }
52  };
53 
54 
55  /**
56  * Specialisation of class JAutomate for class JAutoMap.
57  *
58  * By default, the creation of elements is transfered back to the map
59  * via member method JAutoMap::getAutoElement.
60  */
61  template<class JKey_t, class JValue_t>
62  struct JAutomate< JAutoMap<JKey_t, JValue_t> > {
63 
66 
67  /**
68  * Get element.
69  *
70  * \param type data type
71  * \return element
72  */
73  template<class T>
75  {
76  return automap_type::getAutoElement(type);
77  }
78  };
79 
80 
81  /**
82  * Wrapper class around std::map for automatic insertion of elements based on data types.
83  */
84  template<class JKey_t, class JValue_t>
85  class JAutoMap :
86  public std::map<JKey_t, JValue_t>
87  {
88  public:
89 
91  typedef typename map_type::value_type value_type;
92 
93  using map_type::insert;
94 
95  /**
96  * Default constructor.
97  */
99  {}
100 
101 
102  /**
103  * Creation of a map element for given data type.
104  *
105  * This method should be overloaded for the requested data types
106  * if the method insert() is used without argument.
107  *
108  * \param type data type
109  * \return element
110  */
111  template<class T>
112  static value_type getAutoElement(JType<T> type);
113 
114 
115  /**
116  * Insert list of data types.
117  */
118  template<class T>
119  void insert()
120  {
122  }
123 
124 
125  /**
126  * Insert (list of) data type(s).
127  *
128  * \param automate type defined element
129  */
130  template<class T, class JElement_t>
131  void insert(const JAutomate<JElement_t>& automate)
132  {
133  insert(JType<T>(), automate);
134  }
135 
136  protected:
137  /**
138  * Insertion of single data type.
139  *
140  * \param type data type
141  * \param automate element automate by data type
142  */
143  template<class T, class JElement_t>
144  void insert(JType<T> type,
145  const JAutomate<JElement_t>& automate)
146  {
147  map_type::insert(automate.getElement(type));
148  }
149 
150 
151  /**
152  * Recursive insertion of data types.
153  *
154  * \param typelist type list
155  * \param automate element automate by data type
156  */
157  template<class JHead_t, class JTail_t, class JElement_t>
159  const JAutomate<JElement_t>& automate)
160  {
161  insert(JType<JHead_t>(), automate);
162  insert(JType<JTail_t>(), automate);
163  }
164 
165 
166  /**
167  * Template specialisation to terminate insertion of data types.
168  *
169  * \param type null type
170  * \param automate element automate by data type
171  */
172  template<class JElement_t>
174  const JAutomate<JElement_t>& automate)
175  {}
176  };
177 }
178 
179 #endif
std::map< JKey_t, JValue_t > map_type
Definition: JAutoMap.hh:90
map_type::value_type value_type
Definition: JAutoMap.hh:91
JAutoMap< JKey_t, JValue_t > automap_type
Definition: JAutoMap.hh:64
static JElement_t getElement(JType< T > type)
Get element.
Definition: JAutoMap.hh:48
Auxiliary class for automatic element creation.
Definition: JAutoMap.hh:40
Auxiliary class for a type holder.
Definition: JType.hh:19
void insert(const JAutomate< JElement_t > &automate)
Insert (list of) data type(s).
Definition: JAutoMap.hh:131
static value_type getElement(JType< T > type)
Get element.
Definition: JAutoMap.hh:74
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:158
void insert(JType< JNullType > type, const JAutomate< JElement_t > &automate)
Template specialisation to terminate insertion of data types.
Definition: JAutoMap.hh:173
Forward declaration of class JAutoMap for specialisation of class JAutomate.
Definition: JAutoMap.hh:29
void insert()
Insert list of data types.
Definition: JAutoMap.hh:119
JAutoMap()
Default constructor.
Definition: JAutoMap.hh:98
void insert(JType< T > type, const JAutomate< JElement_t > &automate)
Insertion of single data type.
Definition: JAutoMap.hh:144