Wolframe, 0.0.3

printValue.hpp
Go to the documentation of this file.
1 /************************************************************************
2 Copyright (C) 2011 - 2014 Project Wolframe.
3 All rights reserved.
4 
5 This file is part of Project Wolframe.
6 
7 Commercial Usage
8 Licensees holding valid Project Wolframe Commercial licenses may
9 use this file in accordance with the Project Wolframe
10 Commercial License Agreement provided with the Software or,
11 alternatively, in accordance with the terms contained
12 in a written agreement between the licensee and Project Wolframe.
13 
14 GNU General Public License Usage
15 Alternatively, you can redistribute this file and/or modify it
16 under the terms of the GNU General Public License as published by
17 the Free Software Foundation, either version 3 of the License, or
18 (at your option) any later version.
19 
20 Wolframe is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
24 
25 You should have received a copy of the GNU General Public License
26 along with Wolframe. If not, see <http://www.gnu.org/licenses/>.
27 
28 If you have questions regarding the use of this file, please contact
29 Project Wolframe.
30 
31 ************************************************************************/
34 #ifndef _Wolframe_SERIALIZE_STRUCT_PRINT_VALUE_HPP_INCLUDED
35 #define _Wolframe_SERIALIZE_STRUCT_PRINT_VALUE_HPP_INCLUDED
36 #include "types/datetime.hpp"
37 #include "types/bignumber.hpp"
38 #include <boost/numeric/conversion/cast.hpp>
39 #include <boost/utility/enable_if.hpp>
40 #include <boost/type_traits.hpp>
41 #include <string>
42 
43 namespace {
46 struct PrintValueType
47 {
48  struct String {};
49  struct Bool {};
50  struct Double {};
51  struct UInt {};
52  struct Int {};
53  struct DateTime {};
54  struct BigNumber {};
55 
58  template <typename T>
59  static typename boost::enable_if_c<
60  boost::is_same<T,std::string>::value
61  ,const String&>::type get( const T&) { static String rt; return rt;}
62 
65  template <typename T>
66  static typename boost::enable_if_c<
67  boost::is_same<T,bool>::value
68  ,const Bool&>::type get( const T&) { static Bool rt; return rt;}
69 
72  template <typename T>
73  static typename boost::enable_if_c<
74  boost::is_floating_point<T>::value
75  ,const Double&>::type get( const T&) { static Double rt; return rt;}
76 
79  template <typename T>
80  static typename boost::enable_if_c<
81  boost::is_arithmetic<T>::value && boost::is_signed<T>::value && !boost::is_floating_point<T>::value && !boost::is_same<T,bool>::value
82  ,const Int&>::type get( const T&) { static Int rt; return rt;}
83 
86  template <typename T>
87  static typename boost::enable_if_c<
88  boost::is_arithmetic<T>::value && boost::is_unsigned<T>::value && !boost::is_floating_point<T>::value && !boost::is_same<T,bool>::value
89  ,const UInt&>::type get( const T&) { static UInt rt; return rt;}
90 
93  template <typename T>
94  static typename boost::enable_if_c<
95  boost::is_same<T,_Wolframe::types::DateTime>::value
96  ,const DateTime&>::type get( const T&) { static DateTime rt; return rt;}
97 
100  template <typename T>
101  static typename boost::enable_if_c<
102  boost::is_same<T,_Wolframe::types::BigNumber>::value
103  ,const BigNumber&>::type get( const T&) { static BigNumber rt; return rt;}
104 };
105 
106 
107 
108 template <typename ValueType>
109 bool printValue_( const ValueType& val, const PrintValueType::Bool&, _Wolframe::types::VariantConst& element)
110 {
111  element = val;
112  return true;
113 }
114 
115 template <typename ValueType>
116 bool printValue_( const ValueType& val, const PrintValueType::String&, _Wolframe::types::VariantConst& element)
117 {
118  element.init( val.c_str(), val.size());
119  return true;
120 }
121 
122 template <typename ValueType>
123 bool printValue_( const ValueType& val, const PrintValueType::Double&, _Wolframe::types::VariantConst& element)
124 {
125  try
126  {
127  element = boost::numeric_cast<double>( val);
128  return true;
129  }
130  catch (boost::bad_numeric_cast&){}
131  return false;
132 }
133 
134 template <typename ValueType>
135 bool printValue_( const ValueType& val, const PrintValueType::Int&, _Wolframe::types::VariantConst& element)
136 {
137  try
138  {
139  element = boost::numeric_cast<int>( val);
140  return true;
141  }
142  catch (boost::bad_numeric_cast&){}
143  return false;
144 }
145 
146 
147 template <typename ValueType>
148 bool printValue_( const ValueType& val, const PrintValueType::UInt&, _Wolframe::types::VariantConst& element)
149 {
150  try
151  {
152  element = boost::numeric_cast<unsigned int>( val);
153  return true;
154  }
155  catch (boost::bad_numeric_cast&){}
156  return false;
157 }
158 
159 template <typename ValueType>
160 bool printValue_( const ValueType& val, const PrintValueType::DateTime&, _Wolframe::types::VariantConst& element)
161 {
162  element = val;
163  return true;
164 }
165 
166 template <typename ValueType>
167 bool printValue_( const ValueType& val, const PrintValueType::BigNumber&, _Wolframe::types::VariantConst& element)
168 {
169  element = val;
170  return true;
171 }
172 
173 } //anonymous namespace
174 namespace _Wolframe {
175 namespace serialize {
176 
177 template <typename ValueType>
178 bool printValue( const ValueType& val, types::VariantConst& element)
179 {
180  return printValue_( val, PrintValueType::get(val), element);
181 }
182 
183 }}//namespace
184 #endif
185 
Type for representing arbitrary precision fixed point numbers and big integers as binary coded decima...
bool printValue(const ValueType &val, types::VariantConst &element)
Definition: printValue.hpp:178
Variant value type that represents a variant copy without content ownership.
Definition: variant.hpp:286
Date and datetime value type.
void init(const char *o, std::size_t len)
Initialization as string constant.
Definition: variant.hpp:332