PVData C++
misc/pv/anyscalar.h
00001 #ifndef PV_ANYSCALAR_H
00002 #define PV_ANYSCALAR_H
00003 
00004 #if __cplusplus>=201103L
00005 #  include <type_traits>
00006 #endif
00007 
00008 #include <ostream>
00009 #include <exception>
00010 #include <map>
00011 
00012 #include <epicsAssert.h>
00013 
00014 #include <pv/templateMeta.h>
00015 #include <pv/typeCast.h>
00016 #include <pv/pvIntrospect.h> /* for ScalarType enum */
00017 
00018 namespace epics{namespace pvData{
00019 namespace detail {
00020 
00021 // special mangling for AnyScalar ctor to map from argument type to storage type.
00022 // allow construction from constants.
00023 template <typename T>
00024 struct any_storage_type { typedef T type; };
00025 template<> struct any_storage_type<int> { typedef int32 type; };
00026 template<> struct any_storage_type<unsigned> { typedef uint32 type; };
00027 template<> struct any_storage_type<char*> { typedef std::string type; };
00028 template<> struct any_storage_type<const char*> { typedef std::string type; };
00029 
00030 #if __cplusplus>=201103L
00031 // std::max() isn't constexpr until c++14 :(
00032 constexpr size_t cmax(size_t A, size_t B) {
00033     return A>B ? A : B;
00034 }
00035 #endif
00036 
00037 }// namespace detail
00038 
00051 class epicsShareClass AnyScalar {
00052 public:
00053     struct bad_cast : public std::exception {
00054 #if __cplusplus>=201103L
00055         bad_cast() noexcept {}
00056         virtual ~bad_cast() noexcept {}
00057         virtual const char* what() const noexcept
00058 #else
00059         bad_cast() throw() {}
00060         virtual ~bad_cast() throw() {}
00061         virtual const char* what() const throw()
00062 #endif
00063         { return "bad_cast() type mis-match"; }
00064     };
00065 
00066 private:
00067     ScalarType _stype;
00068 
00069     // always reserve enough storage for std::string or double (assumed worst case)
00070 #if __cplusplus>=201103L
00071 
00072     struct wrap_t {
00073         typename std::aligned_storage<detail::cmax(sizeof(std::string), sizeof(double)),
00074                                       detail::cmax(alignof(std::string), alignof(double))
00075         >::type blob[1];
00076     } _wrap;
00077 #else
00078     struct wrap_t {
00079         union blob_t {
00080             char data[sizeof(std::string)];
00081             double align_f; // assume std::string alignment <= 8
00082         } blob[1];
00083     } _wrap;
00084 #endif
00085 
00086     // assumed largest non-string type
00087     typedef double _largest_blob;
00088 
00089     template<typename T>
00090     inline T& _as() {
00091         return *reinterpret_cast<T*>(_wrap.blob);
00092     }
00093     template<typename T>
00094     inline const T& _as() const {
00095         return *reinterpret_cast<const T*>(_wrap.blob);
00096     }
00097 public:
00100     AnyScalar() : _stype((ScalarType)-1) {}
00101 
00103     template<typename T>
00104     explicit AnyScalar(T v)
00105     {
00106         typedef typename meta::strip_const<T>::type T2;
00107         typedef typename detail::any_storage_type<T2>::type TT;
00108 
00109         STATIC_ASSERT(sizeof(TT)<=sizeof(_wrap.blob));
00110 
00111         new (_wrap.blob) TT(v);
00112 
00113         // this line fails to compile when type T can't be mapped to one of
00114         // the PVD scalar types.
00115         _stype = (ScalarType)ScalarTypeID<TT>::value;
00116     }
00117 
00120     AnyScalar(ScalarType type, const void *buf);
00121 
00122     AnyScalar(const AnyScalar& o);
00123 
00124 #if __cplusplus>=201103L
00125     AnyScalar(AnyScalar&& o);
00126 #endif
00127 
00128     inline ~AnyScalar() {clear();}
00129 
00130     inline AnyScalar& operator=(const AnyScalar& o) {
00131         AnyScalar(o).swap(*this);
00132         return *this;
00133     }
00134 
00135     template<typename T>
00136     inline AnyScalar& operator=(T v) {
00137         AnyScalar(v).swap(*this);
00138         return *this;
00139     }
00140 
00141 #if __cplusplus>=201103L
00142     inline AnyScalar& operator=(AnyScalar&& o) {
00143         clear();
00144         swap(o);
00145         return *this;
00146     }
00147 #endif
00148 
00152     void clear();
00153 
00154     void swap(AnyScalar& o);
00155 
00157     inline ScalarType type() const {
00158         return _stype;
00159     }
00160 
00161     inline       void* unsafe()       { return _wrap.blob; }
00162     inline const void* unsafe() const { return _wrap.blob; }
00163 
00164     inline bool empty() const { return _stype==(ScalarType)-1; }
00165 
00166 #if __cplusplus>=201103L
00167     explicit operator bool() const { return !empty(); }
00168 #else
00169 private:
00170     typedef void (AnyScalar::*bool_type)(AnyScalar&);
00171 public:
00172     operator bool_type() const { return !empty() ? &AnyScalar::swap : 0; }
00173 #endif
00174 
00177     const void* bufferUnsafe() const;
00178 
00187     template<typename T>
00188     inline
00189     // T -> strip_const -> map to storage type -> add reference
00190     typename detail::any_storage_type<typename meta::strip_const<T>::type>::type&
00191     ref() {
00192         typedef typename meta::strip_const<T>::type T2;
00193         typedef typename detail::any_storage_type<T2>::type TT;
00194 
00195         if(_stype!=(ScalarType)ScalarTypeID<TT>::value)
00196             throw bad_cast();
00197         return reinterpret_cast<TT&>(_wrap.blob);
00198     }
00199 
00207     template<typename T>
00208     inline
00209     // T -> strip_const -> map to storage type -> add const reference
00210     typename meta::decorate_const<typename detail::any_storage_type<typename meta::strip_const<T>::type>::type>::type&
00211     ref() const {
00212         typedef typename meta::strip_const<T>::type T2;
00213         typedef typename detail::any_storage_type<T2>::type TT;
00214 
00215         if(_stype!=(ScalarType)ScalarTypeID<TT>::value)
00216             throw bad_cast();
00217         return reinterpret_cast<typename meta::decorate_const<TT>::type&>(_wrap.blob);
00218     }
00219 
00221     template<typename T>
00222     inline
00223     T as() const {
00224         typedef typename meta::strip_const<T>::type T2;
00225         typedef typename detail::any_storage_type<T2>::type TT;
00226 
00227         if(_stype==(ScalarType)-1)
00228             throw bad_cast();
00229         TT ret;
00230         castUnsafeV(1, (ScalarType)ScalarTypeID<T2>::value, &ret,
00231                                    _stype, _wrap.blob);
00232         return ret;
00233     }
00234 
00235 private:
00236     friend epicsShareFunc std::ostream& operator<<(std::ostream& strm, const AnyScalar& v);
00237 };
00238 
00239 epicsShareExtern
00240 std::ostream& operator<<(std::ostream& strm, const AnyScalar& v);
00241 
00242 }} // namespace epics::pvData
00243 
00244 #endif // PV_ANYSCALAR_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator