to_tupleTypes_test.cc
Go to the documentation of this file.
1 // ======================================================================
2 //
3 // test of tuple-type decoding
4 //
5 // template<typename T,std::size_t SIZE>
6 // void decode(std::any const& a, std::array<T,SIZE>& result);
7 //
8 // template<typename KEY,typename VALUE>
9 // void decode(std::any const& a, std::pair<KEY,VALUE>& result);
10 //
11 // template<typename ... ARGS>
12 // void decode(std::any const& a, std::tuple<ARGS...>& result);
13 //
14 // ======================================================================
15 
16 #include "cetlib_except/demangle.h"
17 #include "fhiclcpp/ParameterSet.h"
18 
19 #include <array>
20 #include <iomanip>
21 #include <iostream>
22 #include <string>
23 #include <tuple>
24 #include <typeinfo>
25 #include <utility>
26 #include <vector>
27 
28 using namespace fhicl;
29 
30 namespace {
31  template <typename T>
32  void
33  print(T const& t)
34  {
35  std::cout << std::setw(15) << std::left
36  << cet::demangle_symbol(typeid(t).name()) << " with value \"" << t
37  << "\"\n";
38  }
39 }
40 
41 int
43 {
44  cet::filepath_lookup policy("FHICL_FILE_PATH");
45  std::string const in("to_tupleTypes_test.fcl");
46  auto const pset = ParameterSet::make(in, policy);
47 
48  //======================================================================
49  // array checking
50  //======================================================================
51 
52  std::cout << '\n'
53  << "=================================\n"
54  << " ARRAY CHECK\n"
55  << "=================================\n\n";
56 
57  // warmup
58  auto const vs0 = pset.get<std::vector<std::string>>("vec0");
59 
60  // to array
61  auto const array = pset.get<std::array<std::string, 4>>("vec0");
62  auto const vec_arrays =
63  pset.get<std::vector<std::array<std::string, 3>>>("vec_arrays");
64 
65  // test error
66  try {
67  auto const err0 = pset.get<std::array<std::string, 3>>("vec0");
68  }
69  catch (const fhicl::exception& e) {
70  std::cout << e.what() << std::endl;
71  }
72 
73  //======================================================================
74  // pair checking
75  //======================================================================
76 
77  std::cout << '\n'
78  << "=================================\n"
79  << " PAIR CHECK\n"
80  << "=================================\n\n";
81 
82  // warmup
83  auto const vs1 = pset.get<std::vector<std::string>>("vec1");
84  auto const pr_as_strings = pset.get<std::vector<std::string>>("pair1");
85 
86  // to pair
87  auto const pr = pset.get<std::pair<unsigned, std::string>>("pair1");
88 
89  // to vector of pairs
90  auto const vofp =
91  pset.get<std::vector<std::pair<std::string, unsigned>>>("pair2");
92  std::cout << std::endl;
93  for (auto const& entry : vofp) {
94  std::cout << entry.first << " " << entry.second << std::endl;
95  }
96 
97  // to pair of pairs
98  auto const pofp =
99  pset.get<std::pair<std::pair<std::string, unsigned>,
100  std::pair<std::string, unsigned>>>("pair2");
101  std::cout << std::endl;
102  std::cout << pofp.first.first << " " << pofp.first.second << std::endl
103  << pofp.second.first << " " << pofp.second.second << std::endl;
104 
105  // test error
106  try {
107  auto const err1 = pset.get<std::pair<std::string, std::string>>("pair3");
108  }
109  catch (const fhicl::exception& e) {
110  std::cout << e.what() << std::endl;
111  }
112 
113  //======================================================================
114  // tuple checking
115  //======================================================================
116 
117  std::cout << '\n'
118  << "=================================\n"
119  << " TUPLE CHECK\n"
120  << "=================================\n\n";
121 
122  // warmup
123  auto const vs2 = pset.get<std::vector<std::string>>("vec2");
124  auto const tup_as_strings = pset.get<std::vector<std::string>>("tup1");
125 
126  // to tuple
127  auto const tup1 = pset.get<std::tuple<int, std::string, double>>("tup1");
128 
129  print(std::get<0>(tup1));
130  print(std::get<1>(tup1));
131  print(std::get<2>(tup1));
132 
133  // vector of tuples
134  auto const composers =
135  pset
136  .get<std::vector<std::tuple<std::size_t, std::string, unsigned, double>>>(
137  "tup2");
138 
139  for (auto const& composer : composers) {
140  std::cout << std::endl;
141  print(std::get<0>(composer));
142  print(std::get<1>(composer));
143  print(std::get<2>(composer));
144  print(std::get<3>(composer));
145  }
146 
147  // test error
148  try {
149  auto const err1 =
150  pset.get<std::vector<std::tuple<std::size_t, std::string, unsigned>>>(
151  "tup2");
152  }
153  catch (const fhicl::exception& e) {
154  std::cout << e.what() << std::endl;
155  }
156 
157  // tuple of tuples
158  auto const factoids = pset.get<
159  std::tuple<std::tuple<std::string, std::string, std::string, std::string>,
160  std::tuple<std::string, std::string, std::string, std::string>,
161  std::tuple<std::string,
162  std::string,
163  std::string,
164  std::string,
165  unsigned,
166  unsigned,
167  unsigned>>>("tup3");
168 
169  auto const nine_symphonies = std::get<0>(factoids);
170  std::cout << std::endl;
171  print(std::get<0>(nine_symphonies));
172  print(std::get<1>(nine_symphonies));
173  print(std::get<2>(nine_symphonies));
174  print(std::get<3>(nine_symphonies));
175 
176  auto const less_than_9_symphonies = std::get<1>(factoids);
177  std::cout << std::endl;
178  print(std::get<0>(less_than_9_symphonies));
179  print(std::get<1>(less_than_9_symphonies));
180  print(std::get<2>(less_than_9_symphonies));
181 
182  auto const piano_sonatas = std::get<2>(factoids);
183  std::cout << std::endl;
184  print(std::get<0>(piano_sonatas));
185  print(std::get<1>(piano_sonatas));
186  print(std::get<2>(piano_sonatas));
187  print(std::get<3>(piano_sonatas));
188  print(std::get<4>(piano_sonatas));
189  print(std::get<5>(piano_sonatas));
190  print(std::get<6>(piano_sonatas));
191 }
static QCString name
Definition: declinfo.cpp:673
QList< Entry > entry
std::string string
Definition: nybbler.cc:12
static ParameterSet make(intermediate_table const &tbl)
Definition: ParameterSet.cc:68
QCollection::Item first()
Definition: qglist.cpp:807
int main()
const double e
Q_EXPORT QTSManip setw(int w)
Definition: qtextstream.h:331
auto array(Array const &a)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:228
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
QTextStream & endl(QTextStream &s)