Jpp  17.3.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JAutoMap.cc
Go to the documentation of this file.
1 
2 #include <iostream>
3 #include <iomanip>
4 #include <string>
5 
6 #include "JTools/JAutoMap.hh"
7 
8 #include "Jeep/JParser.hh"
9 #include "Jeep/JMessage.hh"
10 
11 
12 namespace {
13 
14  using namespace JPP;
15 
16  // type definition of map
17 
18  typedef JAutoMap<int, std::string> JAutoMap_t;
19  typedef JAutoMap_t::value_type value_type;
20 
21 
22  using JLANG::JType;
23 
24  struct JElement_t : public value_type
25  {
26  template<class T>
27  JElement_t(JType<T>);
28  };
29 
30  // specialiation of constructor of auxiliary class
31 
32  template<> JElement_t::JElement_t(JType<char>) : value_type(1, "char") {}
33  template<> JElement_t::JElement_t(JType<int>) : value_type(2, "int") {}
34  template<> JElement_t::JElement_t(JType<float>) : value_type(3, "float") {}
35  template<> JElement_t::JElement_t(JType<double>) : value_type(4, "double") {}
36 }
37 
38 namespace JTOOLS {
39 
40  // specialiation of member method JAutoMap_t::getAutoElement
41 
42  template<> template<> value_type JAutoMap_t::getAutoElement(JType<char> ) { return value_type(1, "char"); }
43  template<> template<> value_type JAutoMap_t::getAutoElement(JType<int> ) { return value_type(2, "int"); }
44  template<> template<> value_type JAutoMap_t::getAutoElement(JType<float> ) { return value_type(3, "float"); }
45  template<> template<> value_type JAutoMap_t::getAutoElement(JType<double>) { return value_type(4, "double"); }
46 }
47 
48 
49 /**
50  * \file
51  *
52  * Example program to test JTOOLS::JAutoMap class.
53  * \author mdejong
54  */
55 int main(int argc, char **argv)
56 {
57  using namespace std;
58 
59  int debug;
60 
61  try {
62 
63  JParser<> zap("Example program to test automatic generation of map based on data types.");
64 
65  zap['d'] = make_field(debug) = 0;
66 
67  zap(argc, argv);
68  }
69  catch(const exception &error) {
70  FATAL(error.what() << endl);
71  }
72 
73 
74  using namespace JPP;
75 
76  typedef
77  JTypeList<int,
78  JTypeList<char,
79  JTypeList<double,
80  JTypeList<float> > > > JDataTypes_t;
81 
82  {
83  JAutoMap_t zmap;
84 
85  zmap.insert<JDataTypes_t>();
86 
87  cout << "First method" << endl;
88 
89  for (JAutoMap_t::const_iterator i = zmap.begin(); i != zmap.end(); ++i) {
90  cout << setw(2) << i->first << ' ' << i->second << endl;
91  }
92  }
93 
94  {
95  JAutoMap_t zmap;
96 
97  zmap.insert<JDataTypes_t>(JAutomate<JElement_t>());
98 
99  cout << "Second method" << endl;
100 
101  for (JAutoMap_t::const_iterator i = zmap.begin(); i != zmap.end(); ++i) {
102  cout << setw(2) << i->first << ' ' << i->second << endl;
103  }
104  }
105 }
Utility class to parse command line options.
Definition: JParser.hh:1517
int main(int argc, char *argv[])
Definition: Main.cc:15
Auxiliary class for automatic element creation.
Definition: JAutoMap.hh:46
Auxiliary class for a type holder.
Definition: JType.hh:19
Type list.
Definition: JTypeList.hh:22
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1993
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
Utility class to parse command line options.
Wrapper class around std::map for automatic insertion of elements based on data types.
Definition: JAutoMap.hh:31
int debug
debug level