ztsdb
conversion_funcs.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 CONVERSION_FUNCS_HPP
20 #define CONVERSION_FUNCS_HPP
21 
22 #include "valuevar.hpp"
23 
24 
25 namespace funcs {
26 
27  template<typename T, typename U>
28  arr::cow_ptr<arr::Array<T>> array_convert(const arr::cow_ptr<arr::Array<U>>& u) {
29  throw std::range_error(std::string("conversion not defined for array of ") +
30  TypeName<U>::s + " to array of " + TypeName<T>::s);
31  }
32 
33  template<typename T, typename U>
34  arr::cow_ptr<arr::Array<T>> array_convert_from_scalar(const U& u) {
35  throw std::range_error(std::string("conversion not defined for ") +
36  TypeName<U>::s + " to array of " + TypeName<T>::s);
37  }
38 
39  // to array of double:
40  template<>
41  val::SpVAD array_convert(const val::SpVAD& u);
42  template<>
43  val::SpVAD array_convert(const val::SpVAB& u);
44  template<>
45  val::SpVAD array_convert(const val::SpVADUR& u);
46  template<>
47  val::SpVAD array_convert_from_scalar(const val::VNull& u);
48 
49  // to array of bool:
50  template<>
51  val::SpVAB array_convert(const val::SpVAB& u);
52  template<>
53  val::SpVAB array_convert(const val::SpVAD& u);
54  template<>
55  val::SpVAB array_convert_from_scalar(const val::VNull& u);
56 
57  // to array of duration:
58  template<>
59  val::SpVADUR array_convert(const val::SpVADUR& u);
60  template<>
61  val::SpVADUR array_convert(const val::SpVAD& u);
62  template<>
63  val::SpVADUR array_convert(const val::SpVAS& u);
64  template<>
65  val::SpVADUR array_convert_from_scalar(const val::VNull& u);
66 
67  // to array of period:
68  template<>
69  val::SpVAPRD array_convert(const val::SpVAPRD& u);
70  template<>
71  val::SpVAPRD array_convert(const val::SpVAS& u);
72  template<>
73  val::SpVAPRD array_convert_from_scalar(const val::VNull& u);
74 
75  // to array of time:
76  template<>
77  val::SpVADT array_convert(const val::SpVADT& u);
78  template<>
79  val::SpVADT array_convert(const val::SpVAS& u);
80  template<>
81  val::SpVADT array_convert_from_scalar(const val::VNull& u);
82 
83  // to array of interval:
84  template<>
85  val::SpVAIVL array_convert(const val::SpVAIVL& u);
86  template<>
87  val::SpVAIVL array_convert(const val::SpVAS& u);
88  template<>
89  val::SpVAIVL array_convert_from_scalar(const val::VNull& u);
90 
91  // to array of zstring:
92  template<>
93  val::SpVAS array_convert(const val::SpVAS& u);
94  template<>
95  val::SpVAS array_convert(const val::SpVAD& u);
96  template<>
97  val::SpVAS array_convert(const val::SpVAB& u);
98  template<>
99  val::SpVAS array_convert(const val::SpVADT& u);
100  template<>
101  val::SpVAS array_convert(const val::SpVADUR& u);
102  template<>
103  val::SpVAS array_convert(const val::SpVAIVL& u);
104  template<>
105  val::SpVAS array_convert(const val::SpVAPRD& u);
106  template<>
107  val::SpVAS array_convert_from_scalar(const val::VNull& u);
108 
109 
110  template<typename T>
111  val::Value value_convert(const val::Value& x) {
112  switch (x.which()) {
113  case val::vt_null:
114  return array_convert_from_scalar<T, val::VNull>(get<val::VNull>(x));
115  case val::vt_double:
116  return array_convert<T, double>(get<val::SpVAD>(x));
117  case val::vt_bool:
118  return array_convert<T, bool>(get<val::SpVAB>(x));
119  case val::vt_time:
120  return array_convert<T, Global::dtime>(get<val::SpVADT>(x));
121  case val::vt_duration:
122  return array_convert<T, Global::duration>(get<val::SpVADUR>(x));
123  case val::vt_period:
124  return array_convert<T, tz::period>(get<val::SpVAPRD>(x));
125  case val::vt_interval:
126  return array_convert<T, tz::interval>(get<val::SpVAIVL>(x));
127  case val::vt_string: {
128  return array_convert<T, arr::zstring>(get<val::SpVAS>(x));
129  }
130  default:
131  throw std::range_error(string("cannot convert to ") + TypeName<T>::s);
132  }
133  }
134 
135  // template<>
136  // val::Value value_convert<arr::zstring>(val::Value x);
137 
138  // for the following functions, conversions of simple types are
139  // defined in 'base_types.hpp' because they are needed by the type
140  // 'Array<T>'. 'Array<T>' conversions are handled in the definition
141  // file of this header.
142  const auto convert_logical = value_convert<bool>;
143  const auto convert_numeric = value_convert<double>;
144  const auto convert_character = value_convert<arr::zstring>;
145  const auto convert_duration = value_convert<Global::duration>;
146  const auto convert_period = value_convert<tz::period>;
147  const auto convert_time = value_convert<Global::dtime>;
148  const auto convert_interval = value_convert<tz::interval>;
149 
150  bool operator<(val::ValType vt1, val::ValType vt2);
151  bool operator>(val::ValType vt1, val::ValType vt2);
152  bool operator==(val::ValType vt1, val::ValType vt2);
153 
154  bool isTrue(val::Value x);
155 
156 }
157 
158 
159 
160 
161 
162 #endif
val::VNull
Definition: valuevar.hpp:75
arr::cow_ptr
Definition: cow_ptr.hpp:42
arr::TypeName
Lets us define a compile time mapping from type to string.
Definition: type_utils.hpp:21
arr::Array
Definition: array.hpp:109