DUDS
Distributed Update of Data from Something
GenericValueVisitor.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of the DUDS project. It is subject to the BSD-style
3  * license terms in the LICENSE file found in the top-level directory of this
4  * distribution and at https://github.com/jjackowski/duds/blob/master/LICENSE.
5  * No part of DUDS, including this file, may be copied, modified, propagated,
6  * or distributed except according to the terms contained in the LICENSE file.
7  *
8  * Copyright (C) 2017 Jeff Jackowski
9  */
10 #include <boost/variant/static_visitor.hpp>
11 #include <boost/uuid/uuid.hpp>
12 #include <boost/uuid/uuid_io.hpp>
13 #include <boost/variant.hpp>
16 
18 
19 namespace duds { namespace data {
20 
21 class GenericValueArray;
22 class GenericValueTable;
23 
24 template <typename N = double>
25 class GenericValueNumericVisitor : public boost::static_visitor<N> {
26 public:
27  N operator() (const std::string &s) const {
28  throw boost::bad_visit();
29  }
31  throw boost::bad_visit();
32  }
34  const boost::recursive_wrapper<duds::general::LanguageTaggedStringMap> &) const {
35  throw boost::bad_visit();
36  }
37  N operator() (const N &n) const {
38  return n;
39  }
40  N operator() (const Quantity &vu) const {
41  return (N)vu.value;
42  }
43  N operator() (const QuantityNddArray &vu) const {
44  throw boost::bad_visit();
45  }
46  template <class V>
47  N operator() (const ExtendedQuantity<V> &vu) const {
48  return (N)vu.value;
49  }
50  N operator() (const int128_w &i) const {
51  return (N)i.value;
52  }
53  template<typename A, std::size_t S>
54  N operator() (const std::array<A,S> &a) const {
55  throw boost::bad_visit();
56  }
57  // !!! I sense trouble !!!
58  template<class S, class R, template<class, class> class Duration = std::chrono::duration>
59  N operator() (const Duration<S,R> &d) const {
60  return std::chrono::duration_cast< Duration< N, std::ratio<1,1> > >(d).count();
61  } /*
62  N operator() (const std::chrono::duration<S,R> &d) const {
63  return std::chrono::duration_cast< std::chrono::duration<N> >(d).count();
64  } */
65  N operator() (const boost::uuids::uuid &u) const {
66  throw boost::bad_visit();
67  }
68  N operator() (const GenericValueTable &) const {
69  throw boost::bad_visit();
70  }
71  /*
72  N operator() (const GenericValueArray &) const {
73  throw boost::bad_visit();
74  }
75  */
76  N operator() (const std::shared_ptr< std::vector<char> > &) const {
77  throw boost::bad_visit();
78  }
79 };
80 
81 class GenericValueStringVisitor : public boost::static_visitor<std::string> {
82 public:
83  std::string operator() (const std::string &s) const {
84  return s;
85  }
86  std::string operator() (
88  ) const {
89  return s.string;
90  }
91  std::string operator() (
92  const boost::recursive_wrapper<duds::general::LanguageTaggedStringMap> &) const {
93  throw boost::bad_visit();
94  }
95  std::string operator() (const int128_w &n) const;
96  std::string operator() (const double &n) const;
97  std::string operator() (const boost::uuids::uuid &u) const;
98  std::string operator() (const Quantity &q) const;
99  std::string operator() (const QuantityNddArray &vu) const {
100  throw boost::bad_visit();
101  }
102  template <class Q>
103  std::string operator() (const ExtendedQuantity<Q> &q) const {
104  std::ostringstream os;
106  os << q.value;
107  return os.str();
108  }
109  template<typename A, std::size_t S> //, template<typename, typename> class A = std::array>
110  std::string operator() (const std::array<A,S> &a) const {
111  std::ostringstream os;
112  int l = 0;
113  while (true) {
114  os << a[l++];
115  if (l < S) {
116  os << ", ";
117  } else {
118  break;
119  }
120  }
121  return os.str();
122  }
123  std::string operator() (const duds::time::interstellar::Femtoseconds &s) const;
124  std::string operator() (const duds::time::interstellar::Nanoseconds &s) const;
125  std::string operator() (const duds::time::interstellar::FemtoTime &t) const;
126  std::string operator() (const duds::time::interstellar::NanoTime &t) const;
127  std::string operator() (const GenericValueTable &) const {
128  throw boost::bad_visit();
129  }
130  /*
131  std::string operator() (const GenericValueArray &) const {
132  throw boost::bad_visit();
133  }
134  */
135  std::string operator() (const std::shared_ptr< std::vector<char> > &) const {
136  throw boost::bad_visit();
137  }
138 };
139 
140 template<class IST>
141 class GenericValueTimeVisitor : public boost::static_visitor<IST> {
142  template <class I>
143  IST conv(const I &in) {
144  return IST(typename IST::duration(
145  std::chrono::duration_cast<typename IST::period>(
146  in.time_since_epoch()
147  )
148  ) );
149  }
150  //template <>
151  IST conv(const IST &in) {
152  return in;
153  }
154 public:
155  IST operator() (const std::string &) const {
156  throw boost::bad_visit();
157  }
160  ) const {
161  throw boost::bad_visit();
162  }
164  const boost::recursive_wrapper<duds::general::LanguageTaggedStringMap> &) const {
165  throw boost::bad_visit();
166  }
167  IST operator() (const int128_w &) const {
168  throw boost::bad_visit();
169  }
170  IST operator() (const double &) const {
171  throw boost::bad_visit();
172  }
173  IST operator() (const boost::uuids::uuid &) const {
174  throw boost::bad_visit();
175  }
176  IST operator() (const Quantity &q) const {
177  throw boost::bad_visit();
178  }
179  IST operator() (const QuantityNddArray &) const {
180  throw boost::bad_visit();
181  }
182  template<typename A, std::size_t S> //, template<typename, typename> class A = std::array>
183  IST operator() (const std::array<A,S> &a) const {
184  throw boost::bad_visit();
185  }
187  throw boost::bad_visit();
188  }
190  throw boost::bad_visit();
191  }
193  return conv(t);
194  }
196  return conv(t);
197  }
198  IST operator() (const GenericValueTable &) const {
199  throw boost::bad_visit();
200  }
201  /*
202  IST operator() (const GenericValueArray &) const {
203  throw boost::bad_visit();
204  }
205  */
206  IST operator() (const std::shared_ptr< std::vector<char> > &) const {
207  throw boost::bad_visit();
208  }
209 };
210 
211 template<class IST>
212 class GenericValueDurationVisitor : public boost::static_visitor<IST> {
213  template <class I>
214  IST conv(const I &in) {
215  return IST(typename IST::rep(
216  std::chrono::duration_cast<typename IST::period>(in)
217  ) );
218  }
219  //template <>
220  IST conv(const IST &in) {
221  return in;
222  }
223 public:
224  IST operator() (const std::string &) const {
225  throw boost::bad_visit();
226  }
229  ) const {
230  throw boost::bad_visit();
231  }
233  const boost::recursive_wrapper<duds::general::LanguageTaggedStringMap> &) const {
234  throw boost::bad_visit();
235  }
236  IST operator() (const int128_w &) const {
237  throw boost::bad_visit();
238  }
239  IST operator() (const double &) const {
240  throw boost::bad_visit();
241  }
242  IST operator() (const boost::uuids::uuid &) const {
243  throw boost::bad_visit();
244  }
245  IST operator() (const Quantity &q) const {
246  // check for units of seconds
247  if (q.unit == duds::data::units::Second) {
248  return q.toDuration<IST>();
249  } else {
250  throw boost::bad_visit();
251  }
252  }
253  IST operator() (const QuantityNddArray &) const {
254  throw boost::bad_visit();
255  }
256  template<typename A, std::size_t S> //, template<typename, typename> class A = std::array>
257  IST operator() (const std::array<A,S> &) const {
258  throw boost::bad_visit();
259  }
261  return conv(s);
262  }
264  return conv(s);
265  }
267  throw boost::bad_visit();
268  }
270  throw boost::bad_visit();
271  }
272  IST operator() (const GenericValueTable &) const {
273  throw boost::bad_visit();
274  }
275  /*
276  IST operator() (const GenericValueArray &) const {
277  throw boost::bad_visit();
278  }
279  */
280  IST operator() (const std::shared_ptr< std::vector<char> > &) const {
281  throw boost::bad_visit();
282  }
283 };
284 
285 } }
A container for a value and a unit to better describe the value.
Definition: Quantity.hpp:37
Wraps an integer to allow the Boost multiprecision library to provide insertion and extraction operat...
Definition: Int128.hpp:85
std::string string
A string encoded in UTF-8.
Unit unit
The units describing the value.
Definition: Quantity.hpp:45
Holds a string and its associated language.
An array of quantites of dynamic size and number of dimensions.
void toDuration(Duration &d) const
Sets a duration to the seconds stored in this quantity.
Definition: Quantity.hpp:184
An extention to the C++ std::chrono::time_point template to make time points easier to work with...
static std::wstring_convert< std::codecvt_utf8< char32_t >, char32_t > conv
String converter; UTF-8 to/from UTF-32.
Definition: BppFont.cpp:165
std::chrono::duration< int128_t, std::femto > Femtoseconds
Stores a duration in femtoseconds.
N operator()(const std::string &s) const
An idea that is not yet implemented; DO NOT USE.
double value
Some value; probably something measured.
Definition: Quantity.hpp:41
A set of key-value pairs where the value can be one of several types.
std::chrono::duration< std::uint64_t, std::nano > Nanoseconds
Stores a duration in nanoseconds.
constexpr Unit Second(DUDS_UNIT_VALUE(0, 0, 0, 0, 0, 0, 1, 0, 0))