Jpp  15.0.1-rc.2-highQE
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 /**
51  * \file
52  *
53  * Example program to test JTOOLS::JAutoMap class.
54  * \author mdejong
55  */
56 int main(int argc, char **argv)
57 {
58  using namespace std;
59 
60  int debug;
61 
62  try {
63 
64  JParser<> zap("Example program to test automatic generation of map based on data types.");
65 
66  zap['d'] = make_field(debug) = 0;
67 
68  zap(argc, argv);
69  }
70  catch(const exception &error) {
71  FATAL(error.what() << endl);
72  }
73 
74 
75  using namespace JPP;
76 
77  typedef
78  JTypeList<int,
79  JTypeList<char,
80  JTypeList<double,
81  JTypeList<float> > > > JDataTypes_t;
82 
83  {
84  JAutoMap_t zmap;
85 
86  zmap.insert<JDataTypes_t>();
87 
88  cout << "First method" << endl;
89 
90  for (JAutoMap_t::const_iterator i = zmap.begin(); i != zmap.end(); ++i) {
91  cout << setw(2) << i->first << ' ' << i->second << endl;
92  }
93  }
94 
95  {
96  JAutoMap_t zmap;
97 
98  zmap.insert<JDataTypes_t>(JAutomate<JElement_t>());
99 
100  cout << "Second method" << endl;
101 
102  for (JAutoMap_t::const_iterator i = zmap.begin(); i != zmap.end(); ++i) {
103  cout << setw(2) << i->first << ' ' << i->second << endl;
104  }
105  }
106 }
Utility class to parse command line options.
Definition: JParser.hh:1500
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:1961
int debug
debug level
Definition: JSirene.cc:63
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