ztsdb
base_types.hpp
1 // (C) 2016 Leonardo Silvestri
2 //
3 // This file is part of ztsdb.
4 //
5 // ztsdb is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // ztsdb is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with ztsdb. If not, see <http://www.gnu.org/licenses/>.
17 
18 
19 #ifndef BASE_TYPES_HPP
20 #define BASE_TYPES_HPP
21 
22 
23 #include <cstdint>
24 #include <chrono>
25 #include <exception>
26 #include <string>
27 #include "globals.hpp"
28 #include "type_utils.hpp"
29 #include "timezone/interval.hpp"
30 #include "timezone/zone.hpp"
31 #include "timezone/ztime.hpp"
32 
33 
34 extern tz::Zones tzones;
35 
36 
37 namespace arr {
38 
39  // probably want to centralize type definition here instead of in Globals... LLL
40 
41  // using zdouble = double;
42  // using zint = int64_t;
43  // using zbool = bool;
44  // using zstring = ZString<64>;
45  // using dtime = std::chrono::system_clock::time_point;
46 
47  template<>
48  inline arr::zstring getInitValue() {
49  return "";
50  }
51  template<>
52  inline Global::dtime getInitValue() {
53  return Global::dtime(Global::dtime::duration::zero());
54  }
55  template<>
56  inline Global::duration getInitValue() {
57  return Global::dtime::duration::zero();
58  }
59  template<>
60  inline tz::interval getInitValue() {
61  return tz::interval();
62  }
63  template<>
64  inline tz::period getInitValue() {
65  return tz::period();
66  }
67 
69  template<typename T, typename U>
70  T convert(const U& u) {
71  throw std::range_error(std::string("conversion not defined for ") + TypeName<U>::s +
72  " to " + TypeName<T>::s);
73  }
74 
75  // because the template instantiation depends on the return value,
76  // it cannot be automatically done by the compiler. So the calls to
77  // convert are have full specification of the two types. This means
78  // that we have to use function specialization rather than
79  // overloading. So we need to be careful here.
80 
82  template<>
83  inline double convert(const double& u) {
84  return u;
85  }
86  template<>
87  inline zstring convert(const zstring& u) {
88  return u;
89  }
90  template<>
91  inline bool convert(const bool& u) {
92  return u;
93  }
94  template<>
95  inline Global::dtime convert(const Global::dtime& u) {
96  return u;
97  }
98  template<>
99  inline Global::duration convert(const Global::duration& u) {
100  return u;
101  }
102  template<>
103  inline tz::period convert(const tz::period& u) {
104  return u;
105  }
106  template<>
107  inline tz::interval convert(const tz::interval& u) {
108  return u;
109  }
110 
112  template<>
113  arr::zstring convert(const double& u);
114  template<>
115  arr::zstring convert(const bool& u);
116  template<>
117  arr::zstring convert(const Global::dtime& u);
118  template<>
119  arr::zstring convert(const Global::duration& u);
120  template<>
121  arr::zstring convert(const tz::period& u);
122  template<>
124  template<>
125  inline arr::zstring convert(const std::string& u) {
126  return u;
127  }
128 
130  template<>
131  std::string convert(const double& u);
132  template<>
133  std::string convert(const bool& u);
134  template<>
135  std::string convert(const Global::dtime& u);
136  template<>
137  std::string convert(const Global::duration& u);
138  template<>
139  std::string convert(const tz::period& u);
140  template<>
141  std::string convert(const tz::interval& u);
142  template<>
143  inline std::string convert(const std::string& u) {
144  return u;
145  }
146 
148  template<>
149  inline double convert(const bool& u) {
150  return u;
151  }
152  // for now don't propose this conversion:
153  // template<>
154  // inline double convert(const Global::dtime& u) {
155  // return u.time_since_epoch().count();
156  // }
157  template<>
158  inline double convert(const Global::duration& u) {
159  return u.count();
160  }
161 
162 
164  template<>
165  inline bool convert(const double& u) {
166  return u;
167  }
168  template<>
169  inline bool convert(const Global::duration& u) {
170  return u.count();
171  }
172 
173 
175  template<>
176  inline Global::duration convert(const double& u) {
177  return Global::duration(static_cast<const uint64_t>(u));
178  }
179  template<>
180  inline Global::duration convert(const arr::zstring& u) {
181  return tz::duration_from_string2(u);
182  }
183 
185  template<>
186  inline tz::period convert(const arr::zstring& u) {
187  return tz::period(u);
188  }
189 
191  template<>
192  inline Global::dtime convert(const arr::zstring& u) {
193  return tz::dtime_from_string(u, tzones);
194  }
195 
197  template<>
198  inline tz::interval convert(const arr::zstring& u) {
199  return tz::interval_from_string(u, tzones);
200  }
201 }
202 
203 
204 #endif
tz::period
Definition: period.hpp:30
arr::ZString
Definition: string.hpp:33
tz::interval
Definition: interval.hpp:31
arr::TypeName
Lets us define a compile time mapping from type to string.
Definition: type_utils.hpp:21
arr
Contains the classes and functions that implement a multidimentional array type.
Definition: allocator.hpp:29
arr::convert
val::Value convert(const double &u)
these are the set of "conversions" of a type to itself
Definition: base_funcs_array.cpp:61
tz::Zones
Definition: zone.hpp:66