libpnicore
convert.hpp
1 //
2 // Declaration of template class TypeInfo
3 //
4 // (c) Copyright 2011 DESY, Eugen Wintersberger <eugen.wintersberger@desy.de>
5 //
6 // This file is part of libpnicore.
7 //
8 // libpnicore is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation, either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // libpnicore is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with libpnicore. If not, see <http://www.gnu.org/licenses/>.
20 //
21 // ============================================================================
22 //
23 // Created on: Dec 10, 2011
24 // Author: Eugen Wintersberger
25 //
26 #pragma once
27 
28 #include "type_id_map.hpp"
29 #include <boost/static_assert.hpp>
30 #include <boost/numeric/conversion/cast.hpp>
31 
32 #include "../error/exceptions.hpp"
33 #include "../error/exception_utils.hpp"
34 #include "type_info.hpp"
35 #include "unchecked_convertible.hpp"
36 #include "checked_convertible.hpp"
37 #include "convertible.hpp"
38 #include "type_conversion.hpp"
39 
40 
41 namespace pni{
42 namespace core{
43 
44  using namespace boost::numeric;
45 
55  template<
56  typename TT,
57  typename ST
58  >
59  struct converter
60  {
67  static TT convert(const ST &value)
68  {
69  return boost::numeric_cast<TT>(value);
70  }
71  };
72 
82  template<typename TT>
83  struct converter<TT,TT>
84  {
94  static TT convert(const TT &value)
95  {
96  return value;
97  }
98  };
99 
100  //------------------------------------------------------------------------
110  template<
111  typename BT,
112  typename ST
113  >
114  struct converter<std::complex<BT>,ST>
115  {
125  static std::complex<BT> convert(const ST &value)
126  {
127  BT real = boost::numeric_cast<BT>(value);
128 
129  return std::complex<BT>(real,0);
130  }
131  };
132 
133  //------------------------------------------------------------------------
143  template<
144  typename BTT,
145  typename BST
146  >
147  struct converter<std::complex<BTT>,std::complex<BST>>
148  {
157  static std::complex<BTT> convert(const std::complex<BST> &value)
158  {
159  BTT real = boost::numeric_cast<BTT>(value.real());
160  BTT imag = boost::numeric_cast<BTT>(value.imag());
161 
162  return std::complex<BTT>(real,imag);
163  }
164  };
165 
166  //------------------------------------------------------------------------
177  template<
178  typename T,
179  typename S,
180  bool unchecked_convertible=true
181  >
183  {
191  static T convert(const S &value)
192  {
193  return T(value);
194  }
195  };
196 
197  //------------------------------------------------------------------------
208  template<
209  typename T,
210  typename S
211  >
212  struct conversion_strategy<T,S,false>
213  {
223  static T convert(const S &value)
224  {
225  try
226  {
227  return converter<T,S>::convert(value);
228  }
229  catch(const boost::numeric::positive_overflow &error)
230  {
232  "Source value exceeded range of target type!");
233  }
234  catch(const boost::numeric::negative_overflow &error)
235  {
237  "Source value exceeded range of target type!");
238  }
239  catch(...)
240  {
242  "Unknown error during type conversion!");
243  }
244  }
245 
246  };
247 
248  //-------------------------------------------------------------------------
268  template<
269  typename T,
270  typename S
271  >
272  T convert(const S &source)
273  {
274  static_assert(convertible<S,T>::value,
275  "Types are in no way convertible!");
276 
278  return strategy::convert(source);
279 
280  }
281 
282 //end of namespace
283 }
284 }
data range error
Definition: exceptions.hpp:616
#define EXCEPTION_RECORD
macro creating an instance of ExceptionRecord
Definition: exceptions.hpp:48
static TT convert(const TT &value)
perform conversion
Definition: convert.hpp:94
data type error
Definition: exceptions.hpp:544
STL namespace.
static target_type convert(const source_type &)
static conversion function
Definition: utils.hpp:77
static std::complex< BTT > convert(const std::complex< BST > &value)
perform conversion
Definition: convert.hpp:157
Definition: add_op.hpp:29
static T convert(const S &value)
perform conversion
Definition: convert.hpp:191
static std::complex< BT > convert(const ST &value)
perform conversion
Definition: convert.hpp:125
simple unchecked strategy
Definition: convert.hpp:182
static T convert(const S &value)
perform conversion
Definition: convert.hpp:223
T convert(const S &source)
type conversion function template
Definition: convert.hpp:272
converter class
Definition: utils.hpp:61
check if two types are convertible
Definition: convertible.hpp:46
simple numeric converter
Definition: convert.hpp:59
type erasure for POD data
Definition: value.hpp:46
static TT convert(const ST &value)
perform conversion
Definition: convert.hpp:67