Jpp  master_rocky-40-g5f0272dcd
the software that should make you happy
JMultiPointer.cc
Go to the documentation of this file.
1 
2 #include <iostream>
3 #include <iomanip>
4 
5 #include "JLang/JMultiPointer.hh"
6 #include "JLang/JType.hh"
7 
8 #include "Jeep/JParser.hh"
9 #include "Jeep/JMessage.hh"
10 
11 
12 namespace {
13 
14  using namespace JPP;
15 
16 
17  /**
18  * Print object pointed to from given multi-pointer based on given data type.
19  * Note that the overloading of this method because partial specialisation
20  * of methods is not compliant with the C++ standard.
21  *
22  * \param out output stream
23  * \param ps multi-pointer
24  * \param type data type
25  */
26  template<class T, class JMultiPointer_t>
27  inline void print(std::ostream& out, JMultiPointer_t& ps, const JType<T>& type)
28  {
29  T* __p = ps.template get<T>();
30 
31  if (__p != NULL) {
32  out << *__p;
33  }
34  }
35 
36 
37  /**
38  * Print character string pointed to from given multi-pointer.
39  * Note that the overloading of this method because partial specialisation
40  * of methods is not compliant with the C++ standard.
41  *
42  * \param out output stream
43  * \param ps multi-pointer
44  * \param type const char type
45  */
46  template<class JMultiPointer_t>
47  inline void print(std::ostream& out, JMultiPointer_t& ps, const JType<const char>& type)
48  {
49  const char* __p = ps.template get<const char>();
50 
51  if (__p != NULL) {
52  out << __p;
53  }
54  }
55 
56 
57  /**
58  * Print object pointed to from given multi-pointer based on given template argument.
59  *
60  * \param out output stream
61  * \param ps multi-pointer
62  */
63  template<class T, class JMultiPointer_t>
64  inline void print(std::ostream& out, JMultiPointer_t& ps)
65  {
66  out << "JMultiPointer: <";
67 
68  print(out, ps, JType<T>());
69 
70  out << ">" << std::endl;
71  }
72 }
73 
74 
75 /**
76  * \file
77  *
78  * Example program to test JLANG::JMultiPointer class.
79  * \author mdejong
80  */
81 int main(int argc, char **argv)
82 {
83  using namespace std;
84 
85  int debug;
86 
87  try {
88 
89  JParser<> zap("Example program to test multi pointer.");
90 
91  zap['d'] = make_field(debug) = 3;
92 
93  zap(argc, argv);
94  }
95  catch(const exception &error) {
96  FATAL(error.what() << endl);
97  }
98 
99 
100  using namespace JPP;
101 
102  {
103  JMultiPointer< JTypeList<int> > ps(new int(-123));
104 
105  print<int>(cout, ps);
106 
107  delete ps.get<int>();
108  }
109 
110 
111  {
112  typedef
113  JTypeList<const char,
114  JTypeList<int,
115  JTypeList<double> > > typelist;
116 
118 
119  ps.reset("aap");
120  ps.reset(new int(1));
121  ps.reset(new double(123.456));
122 
123  *ps.get<int>() += 10;
124 
125  print<int> (cout, ps);
126  print<const char>(cout, ps);
127  print<double> (cout, ps);
128 
129  delete ps.get<int> ();
130  delete ps.get<double>();
131  }
132 }
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
int debug
debug level
Definition: JSirene.cc:69
int main(int argc, char **argv)
Utility class to parse command line options.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:2142
Utility class to parse command line options.
Definition: JParser.hh:1698
std::ostream & print(std::ostream &out, const JTestSummary &summary, const char delimiter=' ', const bool useColors=true)
Print test summary.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JSTDTypes.hh:14
General purpose class for multiple pointers.
T * get() const
Get single pointer.
void reset(const JMultiPointer< JClass_t > &pointer)
Reset multi-pointer.
Type list.
Definition: JTypeList.hh:23
Auxiliary class for a type holder.
Definition: JType.hh:19