libpnicore
value.hpp
1 //
2 // (c) Copyright 2013 DESY, Eugen Wintersberger <eugen.wintersberger@desy.de>
3 //
4 // This file is part of libpnicore.
5 //
6 // libpnicore is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // libpnicore is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with libpnicore. If not, see <http://www.gnu.org/licenses/>.
18 //
19 // ============================================================================
20 //
21 // Created on: Jan 11, 2013
22 // Author: Eugen Wintersberger <eugen.wintersberger@desy.de>
23 //
24 #pragma once
25 #include<iostream>
26 #include<memory>
27 
28 #include "../error/exceptions.hpp"
29 #include "../types/types.hpp"
30 #include "../arrays.hpp"
31 #include "value_holder.hpp"
32 #include "utils.hpp"
33 #include "../types/traits.hpp"
34 
35 namespace pni{
36 namespace core{
37 
38 
39  //forward declaration
40  class value_ref;
41 
46  class value
47  {
48  private:
50  typedef std::unique_ptr<value_holder_interface> pointer_type;
51 
52  template<typename T>
53  using enable_primitive = std::enable_if<is_primitive_type<T>::value>;
54 
55  //----------------------------------------------------------------
69  template<
70  typename T,
71  typename S
72  >
73  T _get() const
74  {
75  return get_value<T,S>(get_holder_ptr<S>(_ptr));
76  }
77 
78  //----------------------------------------------------------------
90  template<
91  typename S,
92  typename T
93  >
94  void _set(const T& v) const
95  {
96  return set_value<S,T>(get_holder_ptr<S>(_ptr),v);
97  }
98 
100  pointer_type _ptr;
101  public:
102  //================constructors and destructor======================
104  value();
105 
106  //-----------------------------------------------------------------
115  template<
116  typename T,
117  typename = typename enable_primitive<T>::type
118  >
119  explicit value(T v):_ptr(new value_holder<T>(v)){}
120 
121  //-----------------------------------------------------------------
125  value(const value &o);
126 
127  //-----------------------------------------------------------------
131  value(value &&o);
132 
133  //==================assignment operators===========================
140  template<typename VT> value &operator=(const VT &v);
141 
142  //-----------------------------------------------------------------
144  value &operator=(const value &o);
145 
146  //-----------------------------------------------------------------
148  value &operator=(value &&o);
149 
150  //----------------------------------------------------------------
152  value &operator=(const value_ref &v);
153 
154  //-----------------------------------------------------------------
170  template<typename T> T as() const;
171 
172  //-----------------------------------------------------------------
181  type_id_t type_id() const;
182  };
183 
184  //=====================implementation of template member functions=========
185  template<typename T> T value::as() const
186  {
187  type_id_t tid = type_id();
188 
189  switch(tid)
190  {
191  case type_id_t::UINT8: return _get<T,uint8>();
192  case type_id_t::INT8: return _get<T,int8>();
193  case type_id_t::UINT16: return _get<T,uint16>();
194  case type_id_t::INT16: return _get<T,int16>();
195  case type_id_t::UINT32: return _get<T,uint32>();
196  case type_id_t::INT32: return _get<T,int32>();
197  case type_id_t::UINT64: return _get<T,uint64>();
198  case type_id_t::INT64: return _get<T,int64>();
199  case type_id_t::FLOAT32: return _get<T,float32>();
200  case type_id_t::FLOAT64: return _get<T,float64>();
201  case type_id_t::FLOAT128: return _get<T,float128>();
202  case type_id_t::COMPLEX32: return _get<T,complex32>();
203  case type_id_t::COMPLEX64: return _get<T,complex64>();
204  case type_id_t::COMPLEX128: return _get<T,complex128>();
205  case type_id_t::STRING: return _get<T,string>();
206  case type_id_t::BINARY: return _get<T,binary>();
207  case type_id_t::BOOL: return _get<T,bool_t>();
208  default:
209  throw type_error(EXCEPTION_RECORD,"Uknown type!");
210  }
211 
212  }
213 
214  //-------------------------------------------------------------------------
215  template<typename VT> value &value::operator=(const VT &v)
216  {
217  type_id_t tid = type_id();
218 
219  switch(tid)
220  {
221  case type_id_t::UINT8: _set<uint8>(v); break;
222  case type_id_t::INT8: _set<int8>(v); break;
223  case type_id_t::UINT16: _set<uint16>(v); break;
224  case type_id_t::INT16: _set<int16>(v); break;
225  case type_id_t::UINT32: _set<uint32>(v); break;
226  case type_id_t::INT32: _set<int32>(v); break;
227  case type_id_t::UINT64: _set<uint64>(v); break;
228  case type_id_t::INT64: _set<int64>(v); break;
229  case type_id_t::FLOAT32: _set<float32>(v); break;
230  case type_id_t::FLOAT64: _set<float64>(v); break;
231  case type_id_t::FLOAT128: _set<float128>(v); break;
232  case type_id_t::COMPLEX32: _set<complex32>(v); break;
233  case type_id_t::COMPLEX64: _set<complex64>(v); break;
234  case type_id_t::COMPLEX128: _set<complex128>(v); break;
235  case type_id_t::BINARY: _set<binary>(v); break;
236  case type_id_t::BOOL: _set<bool_t>(v); break;
237  case type_id_t::STRING: _set<string>(v); break;
238  default:
240  "Value is of unkonw type!");
241  }
242 
243  return *this;
244  }
245 
246  //-------------------------------------------------------------------------
257  type_id_t type_id(const value &rv);
258 
259  //------------------------------------------------------------------------
269  template<typename T> value make_value()
270  {
271  return value(T{});
272  }
273 
274  //------------------------------------------------------------------------
286  value make_value(type_id_t tid);
287 
288 //end of namespace
289 }
290 }
291 
32Bit IEEE floating point
std::unique_ptr< value_holder_interface > pointer_type
internal pointer type
Definition: value.hpp:50
#define EXCEPTION_RECORD
macro creating an instance of ExceptionRecord
Definition: exceptions.hpp:48
void _set(const T &v) const
set value
Definition: value.hpp:94
data type error
Definition: exceptions.hpp:544
signed 8Bit integer
T _get() const
return value
Definition: value.hpp:73
unsigned 32Bit integer
unsigned 64Bit integer
unsigned 16Bit integer
128Bit IEEE floating point complex
type_id_t type_id() const
get type id
64Bit IEEE floating point
Definition: add_op.hpp:29
unsigned 8Bit integer
implementation of the holder interface
Definition: value_holder.hpp:174
32Bit IEEE floating point complex
128Bit IEEE floating point
value make_value(type_id_t tid)
create value
value & operator=(const VT &v)
copy assignment from value
Definition: value.hpp:215
type_id_t type_id(const value &rv)
get type ID
type_id_t
type codes for PNI data types
Definition: types/types.hpp:148
64Bit IEEE floating point complex
type erasure for references to POD data
Definition: value_ref.hpp:43
type erasure for POD data
Definition: value.hpp:46
signed 64Bit integer
value()
default constructor
signed 32Bit integer
signed 16Bit integer
pointer_type _ptr
pointer holding the value stored
Definition: value.hpp:100
value(T v)
template constructor from value
Definition: value.hpp:119
T as() const
get the stored value
Definition: value.hpp:185