libpnicore
utils.hpp
1 //
2 // (c) Copyright 2015 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 12, 2015
22 // Author: Eugen Wintersberger <eugen.wintersberger@desy.de>
23 //
24 #pragma once
25 #include<iostream>
26 #include<memory>
27 
28 #include "../types/types.hpp"
29 #include "../types/convert.hpp"
30 #include "../types/type_conversion.hpp"
31 #include "../types/traits.hpp"
32 #include "value_holder.hpp"
33 
34 namespace pni{
35 namespace core{
36 
37 
38  //------------------------------------------------------------------------
56  template<
57  typename TT,
58  typename ST,
59  bool convertible=false
60  >
62  {
64  typedef TT target_type;
66  typedef ST source_type;
67 
77  static target_type convert(const source_type &)
78  {
79  throw type_error(EXCEPTION_RECORD,"Conversion not possible!");
80  return target_type();
81  }
82  };
83 
84 
85 
86  //------------------------------------------------------------------------
97  template<
98  typename TT,
99  typename ST
100  >
101  struct value_converter<TT,ST,true>
102  {
104  typedef TT target_type;
106  typedef ST source_type;
107 
120  static target_type convert(const source_type &v)
121  {
122  return pni::core::convert<target_type>(v);
123  }
124  };
125 
126  //------------------------------------------------------------------------
127  template<
128  typename TT,
129  typename ST
130  >
131  using strategy = value_converter<TT,ST,convertible<ST,TT>::value>;
132 
133  //------------------------------------------------------------------------
142  template<typename T> using ref_type = std::reference_wrapper<T>;
143 
144  //------------------------------------------------------------------------
161  template<
162  typename T,
163  typename PTR
164  >
166  {
167  typedef value_holder<T> holder_type;
168 
169  return dynamic_cast<holder_type*>(ptr.get());
170  }
171 
172  //------------------------------------------------------------------------
191  template<
192  typename T,
193  typename S,
194  typename PTR
195  >
196  T get_value(PTR holder_ptr)
197  {
198  typedef strategy<T,S> strategy_type;
199 
200  return strategy_type::convert(holder_ptr->as());
201  }
202 
203  //------------------------------------------------------------------------
221  template<
222  typename S,
223  typename T,
224  typename PTR
225  >
226  void set_value(PTR holder_ptr,const T &v)
227  {
228  typedef strategy<S,T> strategy_type;
229 
230  holder_ptr->as() = strategy_type::convert(v);
231  }
232 
233 
234 
235 //end of namespace
236 }
237 }
void set_value(PTR holder_ptr, const T &v)
set value to holder
Definition: utils.hpp:226
#define EXCEPTION_RECORD
macro creating an instance of ExceptionRecord
Definition: exceptions.hpp:48
data type error
Definition: exceptions.hpp:544
ST source_type
source type alias
Definition: utils.hpp:106
TT target_type
target type alias
Definition: utils.hpp:64
static target_type convert(const source_type &)
static conversion function
Definition: utils.hpp:77
Definition: add_op.hpp:29
TT target_type
target type alias
Definition: utils.hpp:104
implementation of the holder interface
Definition: value_holder.hpp:174
T get_value(PTR holder_ptr)
get value from holder
Definition: utils.hpp:196
T convert(const S &source)
type conversion function template
Definition: convert.hpp:272
static target_type convert(const source_type &v)
conversion function
Definition: utils.hpp:120
converter class
Definition: utils.hpp:61
value_holder< T > * get_holder_ptr(PTR &ptr)
get pointer to value holder
Definition: utils.hpp:165
ST source_type
source type alias
Definition: utils.hpp:66