Jpp  18.0.1-rc.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JFind_if.cc
Go to the documentation of this file.
1 
2 #include <iostream>
3 #include <iomanip>
4 #include <vector>
5 #include <algorithm>
6 #include <iterator>
7 
8 #include "JLang/JFind_if.hh"
9 
10 #include "Jeep/JParser.hh"
11 #include "Jeep/JMessage.hh"
12 
13 
14 namespace {
15 
16  class __A__ {
17  public:
18  __A__(const int a)
19  {
20  this->a = a;
21  }
22 
23  int get() const
24  {
25  return a;
26  }
27 
28  friend inline std::ostream& operator<<(std::ostream& out, const __A__& object) { return out << object.a; }
29 
30  int a;
31  };
32 
33  class JPredicate_t {
34  public:
35  JPredicate_t(const int value) :
36  value(value)
37  {}
38 
39  bool operator()(const int value) const
40  {
41  return this->value == value;
42  }
43 
44  int value;
45  };
46 
47  template<class T>
48  void print(std::ostream& out, int debug, const char* title, T __begin, T __end)
49  {
50  using namespace std;
51  using namespace JPP;
52 
53  if (debug >= debug_t) {
54  out << title << endl;
55  copy(__begin, __end, ostream_iterator<typename T::value_type>(out, " "));
56  out << endl;
57  }
58  }
59 }
60 
61 
62 /**
63  * \file
64  *
65  * Auxiliary program to test JLANG::JPredicate class and helper methods.
66  * \author mdejong
67  */
68 int main(int argc, char **argv)
69 {
70  using namespace std;
71  using namespace JPP;
72 
73  int debug;
74 
75  try {
76 
77  JParser<> zap("Auxiliary program to test object selection methods.");
78 
79  zap['d'] = make_field(debug) = 3;
80 
81  zap(argc, argv);
82  }
83  catch(const exception &error) {
84  FATAL(error.what() << endl);
85  }
86 
87  vector<__A__> buffer;
88 
89  buffer.push_back(__A__(3));
90  buffer.push_back(__A__(4));
91  buffer.push_back(__A__(2));
92  buffer.push_back(__A__(1));
93  buffer.push_back(__A__(5));
94 
95 
96  print(cout, debug, "data:", buffer.begin(), buffer.end());
97 
98  JPredicate_t predicate(3);
99 
100  {
101  vector<__A__>::const_iterator p = find_if(buffer.begin(), buffer.end(), make_find_if(&__A__::a, predicate));
102 
103  DEBUG("has " << predicate.value << "? " << (p != buffer.end() ? "yes" : "no") << endl);
104 
105  ASSERT(p != buffer.end() && p->a == predicate.value);
106  }
107 
108  {
109  vector<__A__>::const_iterator p = find_if(buffer.begin(), buffer.end(), make_find_if(&__A__::get, predicate));
110 
111  DEBUG("has " << predicate.value << "? " << (p != buffer.end() ? "yes" : "no") << endl);
112 
113  ASSERT(p != buffer.end() && p->get() == predicate.value);
114  }
115 
116  return 0;
117 }
Utility class to parse command line options.
Definition: JParser.hh:1514
debug
Definition: JMessage.hh:29
int main(int argc, char *argv[])
Definition: Main.cc:15
T get(const JHead &header)
Get object from header.
#define ASSERT(A,...)
Assert macro.
Definition: JMessage.hh:90
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1989
then JCalibrateToT a
Definition: JTuneHV.sh:116
do set_variable OUTPUT_DIRECTORY $WORKDIR T
print
Definition: JConvertDusj.sh:44
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
JFind_if< JResult_t T::*, JPredicate_t > make_find_if(JResult_t T::*member, const JPredicate_t &predicate)
Helper method to create find_if for data member.
Definition: JFind_if.hh:119
Utility class to parse command line options.
void copy(const Head &from, JHead &to)
Copy header from from to to.
Definition: JHead.cc:162
std::ostream & operator<<(std::ostream &stream, const CLBCommonHeader &header)
int debug
debug level
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62