Jpp
JMultiComparable.cc
Go to the documentation of this file.
1 
2 
3 #include <iostream>
4 #include <iomanip>
5 #include <vector>
6 #include <set>
7 #include <algorithm>
8 
9 #include "JLang/JComparable.hh"
11 
12 #include "Jeep/JParser.hh"
13 #include "Jeep/JMessage.hh"
14 
15 
16 namespace {
17 
18  using namespace JPP;
19 
20 
21  struct __A__ :
22  public JComparable<__A__>
23  {
24  __A__() :
25  value(0)
26  {}
27 
28  bool less(const __A__& object) const
29  {
30  return this->value < object.value;
31  }
32 
33  friend std::istream& operator>>(std::istream& in, __A__& object)
34  {
35  return in >> object.value;
36  }
37 
38  friend std::ostream& operator<<(std::ostream& out, const __A__& object)
39  {
40  return out << object.value;
41  }
42 
43  int value;
44  };
45 
46 
47  struct __B__ :
48  public JComparable<__B__>
49  {
50  __B__() :
51  value(0)
52  {}
53 
54  bool less(const __B__& object) const
55  {
56  return this->value < object.value;
57  }
58 
59  friend std::istream& operator>>(std::istream& in, __B__& object)
60  {
61  return in >> object.value;
62  }
63 
64  friend std::ostream& operator<<(std::ostream& out, const __B__& object)
65  {
66  return out << object.value;
67  }
68 
69  int value;
70  };
71 
72 
73  struct __C__ :
74  public __A__,
75  public __B__,
76  public JMultiComparable<__C__, JTYPELIST<__A__, __B__>::typelist>
77  {
78  __C__()
79  {}
80 
81  friend std::istream& operator>>(std::istream& in, __C__& object)
82  {
83  return in >> static_cast<__A__&>(object)
84  >> static_cast<__B__&>(object);
85  }
86 
87  friend std::ostream& operator<<(std::ostream& out, const __C__& object)
88  {
89  return out << static_cast<const __A__&>(object) << ' '
90  << static_cast<const __B__&>(object);
91  }
92  };
93 }
94 
95 /**
96  * \file
97  *
98  * Example program to test JLANG::JComparible class.
99  * \author mdejong
100  */
101 int main(int argc, char **argv)
102 {
103  using namespace std;
104  using namespace JPP;
105 
106  typedef vector<__C__> buffer_type;
107 
108  buffer_type buffer;
109  int debug;
110 
111  try {
112 
113  JParser<> zap("Example program to test hierarchical comparisons of objects.");
114 
115  zap['i'] = make_field(buffer);
116  zap['d'] = make_field(debug) = 0;
117 
118  zap(argc, argv);
119  }
120  catch(const exception &error) {
121  FATAL(error.what() << endl);
122  }
123 
124  DEBUG("before:" << endl);
125  for (buffer_type::const_iterator i = buffer.begin(); i != buffer.end(); ++i) {
126  DEBUG(*i << endl);
127  }
128 
129  sort(buffer.begin(), buffer.end());
130 
131  DEBUG("after:" << endl);
132  for (buffer_type::const_iterator i = buffer.begin(); i != buffer.end(); ++i) {
133  DEBUG(*i << endl);
134  }
135 
136  ASSERT(buffer.size() > 1);
137 
138  for (buffer_type::const_iterator q = buffer.begin(), p = q++; q != buffer.end(); ++p, ++q) {
139  ASSERT((static_cast<const __A__&>(*p) < static_cast<const __A__&>(*q)) ||
140  (static_cast<const __A__&>(*p) == static_cast<const __A__&>(*q) &&
141  static_cast<const __B__&>(*p) < static_cast<const __B__&>(*q)));
142  }
143 }
operator>>
std::istream & operator>>(std::istream &in, JAANET::JHead &header)
Read header from input.
Definition: JHead.hh:1265
JMessage.hh
ASSERT
#define ASSERT(A,...)
Assert macro.
Definition: JMessage.hh:90
std::vector
Definition: JSTDTypes.hh:12
JPARSER::JParser
Utility class to parse command line options.
Definition: JParser.hh:1493
JMultiComparable.hh
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
JComparable.hh
debug
int debug
debug level
Definition: JSirene.cc:59
operator<<
std::ostream & operator<<(std::ostream &stream, const CLBCommonHeader &header)
Definition: clb_common_header.hh:72
JParser.hh
JLANG::JComparable
Template definition of auxiliary base class for comparison of data structures.
Definition: JComparable.hh:24
make_field
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1954
DEBUG
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
std
Definition: jaanetDictionary.h:36
FATAL
#define FATAL(A)
Definition: JMessage.hh:67
JLANG::JMultiComparable
Template definition of auxiliary base class for composite data structures composed of base classes wi...
Definition: JMultiComparable.hh:32
main
int main(int argc, char **argv)
Definition: JMultiComparable.cc:101