libpnicore
value_holder.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 
26 #include "../types/type_id_map.hpp"
27 #include "value_holder_interface.hpp"
28 
29 namespace pni{
30 namespace core{
31 
43  template<typename T>
44  T& get_ref(T &v) noexcept
45  {
46  return v;
47  }
48 
49  //------------------------------------------------------------------------
60  template<typename T>
61  const T& get_const_ref(const T &v) noexcept
62  {
63  return v;
64  }
65 
66  //------------------------------------------------------------------------
77  template<typename T>
78  T& get_ref(std::reference_wrapper<T> &v) noexcept
79  {
80  return v.get();
81  }
82 
83  //------------------------------------------------------------------------
94  template<typename T>
95  const T& get_const_ref(const std::reference_wrapper<T> &v) noexcept
96  {
97  return v.get();
98  }
99 
100 
101  //-------------------------------------------------------------------------
110  template<typename T> struct is_reference_holder
111  {
113  static const bool value = false;
114  };
115 
116  //-------------------------------------------------------------------------
124  template<typename T> struct is_reference_holder<std::reference_wrapper<T> >
125  {
127  static const bool value = true;
128  };
129 
130  //-------------------------------------------------------------------------
137  template<typename T> struct get_reference_type
138  {
140  typedef T value_type;
142  typedef T &reference_type;
144  typedef const T &const_reference_type;
145  };
146 
147  //-------------------------------------------------------------------------
155  template<typename T> struct get_reference_type<std::reference_wrapper<T> >
156  {
158  typedef T value_type;
160  typedef T &reference_type;
162  typedef const T &const_reference_type;
163  };
164 
165  //-------------------------------------------------------------------------
173  template<typename T>
175  {
176  private:
177  T _value;
178 
179  public:
180  //----------------------------------------------------------------
184  value_holder():_value(T(0)) {}
185 
186  //----------------------------------------------------------------
195  value_holder(T v):_value(v) {}
196 
197 
198  //====================public inerface implementation==============
204  virtual type_id_t type_id() const noexcept
205  {
206  typedef typename get_reference_type<T>::value_type value_type;
208  }
209 
210  //----------------------------------------------------------------
216  virtual value_holder_interface *clone() const
217  {
218  return new value_holder<T>(_value);
219  }
220 
221  //----------------------------------------------------------------
230  {
231  return get_const_ref(_value);
232  }
233 
234  //----------------------------------------------------------------
241  {
242  return get_ref(_value);
243  }
244 
245  //----------------------------------------------------------------
251  virtual bool holds_reference() const
252  {
254  }
255  };
256 
257 
258 //end of namespace
259 }
260 }
const T & const_reference_type
const reference type
Definition: value_holder.hpp:162
T & reference_type
reference type
Definition: value_holder.hpp:142
get_reference_type< T >::reference_type as()
get reference
Definition: value_holder.hpp:240
check if type is reference
Definition: value_holder.hpp:110
STL namespace.
virtual value_holder_interface * clone() const
clone holder instance
Definition: value_holder.hpp:216
const T & get_const_ref(const std::reference_wrapper< T > &v) noexcept
return const reference
Definition: value_holder.hpp:95
Definition: add_op.hpp:29
T value_type
original reference type
Definition: value_holder.hpp:140
get_reference_type< T >::value_type as() const
return value
Definition: value_holder.hpp:229
get original type of reference
Definition: value_holder.hpp:137
virtual type_id_t type_id() const noexcept
get type id of data
Definition: value_holder.hpp:204
value_holder(T v)
constructor
Definition: value_holder.hpp:195
abstract interface for data holder
Definition: value_holder_interface.hpp:39
const T & const_reference_type
const reference type
Definition: value_holder.hpp:144
implementation of the holder interface
Definition: value_holder.hpp:174
map from a type to type_id
Definition: type_id_map.hpp:55
T value_type
original reference type
Definition: value_holder.hpp:158
T & get_ref(std::reference_wrapper< T > &v) noexcept
return reference
Definition: value_holder.hpp:78
virtual bool holds_reference() const
check if holder stores a reference
Definition: value_holder.hpp:251
type_id_t
type codes for PNI data types
Definition: types/types.hpp:148
T _value
the data value
Definition: value_holder.hpp:177
type erasure for POD data
Definition: value.hpp:46
value_holder()
Definition: value_holder.hpp:184
T & reference_type
reference type
Definition: value_holder.hpp:160