Jpp  pmt_effective_area_update
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JVectorize.cc
Go to the documentation of this file.
1 
2 #include <iostream>
3 #include <iomanip>
4 #include <vector>
5 
6 #include "JLang/JVectorize.hh"
7 
8 #include "Jeep/JParser.hh"
9 #include "Jeep/JMessage.hh"
10 
11 
12 namespace {
13 
14  struct __A__
15  {
16  __A__() :
17  i(0)
18  {}
19 
20  __A__(const int __i) :
21  i(__i)
22  {}
23 
24  int get() const { return i; }
25 
26  friend inline std::ostream& operator<<(std::ostream& out, const __A__& object)
27  {
28  return out << object.i;
29  }
30 
31  int i;
32  };
33 
34 
35  /**
36  * Print data.
37  *
38  * \param out output stream
39  * \param N number of elements
40  * \param p pointer to data
41  */
42  template<class T>
43  inline void print(std::ostream& out, int N, T* p)
44  {
45  using namespace std;
46 
47  for (int i = 0; i != N; ++i) {
48  out << ' ' << setw(2) << p[i];
49  }
50 
51  cout << endl;
52  }
53 }
54 
55 
56 /**
57  * \file
58  *
59  * Example program to test JLANG::make_array method.
60  * \author mdejong
61  */
62 int main(int argc, char **argv)
63 {
64  using namespace std;
65 
66  int debug;
67 
68  try {
69 
70  JParser<> zap("Example program to test converions of array of objects to array of data members.");
71 
72  zap['d'] = make_field(debug) = 3;
73 
74  zap(argc, argv);
75  }
76  catch(const exception &error) {
77  FATAL(error.what() << endl);
78  }
79 
80 
81  using namespace JPP;
82 
83  vector<__A__> buffer;
84 
85  int numberOfElements = 5;
86 
87  for (int i = 0; i != numberOfElements; ++i) {
88  buffer.push_back(__A__(i));
89  }
90 
91  if (debug >= debug_t) {
92  print(cout, numberOfElements, buffer.data());
93  print(cout, numberOfElements, make_array(buffer.begin(), buffer.end(), &__A__::get).data());
94  print(cout, numberOfElements, make_array(buffer.begin(), buffer.end(), &__A__::i) .data());
95  }
96 
97  {
98  const int* p = make_array(buffer.begin(), buffer.end(), &__A__::get).data();
99 
100  for (int i = 0; i != numberOfElements; ++i, ++p) {
101  ASSERT(buffer[i].get() == *p);
102  }
103  }
104 
105  {
106  const int* p = make_array(buffer.begin(), buffer.end(), &__A__::i).data();
107 
108  for (int i = 0; i != numberOfElements; ++i, ++p) {
109  ASSERT(buffer[i].i == *p);
110  }
111  }
112 
113  return 0;
114 }
Utility class to parse command line options.
Definition: JParser.hh:1500
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:1961
const array_type< JValue_t > & make_array(const JValue_t(&array)[N])
Method to create array of values.
Definition: JVectorize.hh:54
do set_variable OUTPUT_DIRECTORY $WORKDIR T
int debug
debug level
Definition: JSirene.cc:63
Auxiliary methods to convert data members or return values of member methods of a set of objects to a...
print
Definition: JConvertDusj.sh:44
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
Utility class to parse command line options.
std::ostream & operator<<(std::ostream &stream, const CLBCommonHeader &header)
then usage $script[input file[working directory[option]]] nWhere option can be N
Definition: JMuonPostfit.sh:36