conversion.h
Go to the documentation of this file.
1 //=================================================================================================
2 // Copyright (c) 2011, Johannes Meyer, TU Darmstadt
3 // All rights reserved.
4 
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above copyright
10 // notice, this list of conditions and the following disclaimer in the
11 // documentation and/or other materials provided with the distribution.
12 // * Neither the name of the Flight Systems and Automatic Control group,
13 // TU Darmstadt, nor the names of its contributors may be used to
14 // endorse or promote products derived from this software without
15 // specific prior written permission.
16 
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //=================================================================================================
28 
29 #ifndef CPP_INTROSPECTION_CONVERSION_H
30 #define CPP_INTROSPECTION_CONVERSION_H
31 
32 #include <boost/type_traits.hpp>
33 #include <boost/utility/enable_if.hpp>
34 #include <introspection/type.h>
35 
36 namespace cpp_introspection {
37 
38  template <
39  typename OtherT,
40  typename Enabled = void
41  >
42  struct converter {
43  static OtherT to(const Type& type, const boost::any& value) { return OtherT(); }
44  static boost::any from(const Type& type, const OtherT& value) { return boost::any(); }
45  };
46 
47  // floating_point
48  template <typename OtherT>
49  struct converter<OtherT, typename boost::enable_if<boost::is_floating_point<OtherT> >::type>
50  {
51  static OtherT to(const Type& type, const boost::any& value) { return OtherT(type.as_double(value)); }
52  static boost::any from(const Type& type, const OtherT& value) { return type.from_double(value); }
53  };
54 
55  // signed
56  template <typename OtherT>
57  struct converter<OtherT, typename boost::enable_if<boost::is_signed<OtherT> >::type> {
58  static OtherT to(const Type& type, const boost::any& value) { return OtherT(type.as_int(value)); }
59  static boost::any from(const Type& type, const OtherT& value) { return type.from_int(value); }
60  };
61 
62  // unsigned
63  template <typename OtherT>
64  struct converter<OtherT, typename boost::enable_if<boost::is_unsigned<OtherT> >::type> {
65  static OtherT to(const Type& type, const boost::any& value) { return OtherT(type.as_unsigned(value)); }
66  static boost::any from(const Type& type, const OtherT& value) { return type.from_unsigned(value); }
67  };
68 
69  // string
70  template <>
71  struct converter<std::string> {
72  static std::string to(const Type& type, const boost::any& value) { return type.as_string(value); }
73  static boost::any from(const Type& type, const std::string& value) { return type.from_string(value); }
74  };
75 
76  template <typename TargetType> TargetType Type::as(const boost::any& value) const { return converter<TargetType>::to(*this, value); }
77  template <typename SourceType> boost::any Type::from(const SourceType& value) const { return converter<SourceType>::from(*this, value); }
78 
79 } // namespace cpp_introspection
80 
81 #endif // CPP_INTROSPECTION_CONVERSION_H
TargetType as(const boost::any &value) const
Definition: conversion.h:76
virtual int as_int(const boost::any &value) const =0
virtual std::string as_string(const boost::any &value) const =0
boost::any from(const SourceType &value) const
Definition: conversion.h:77
static boost::any from(const Type &type, const std::string &value)
Definition: conversion.h:73
static std::string to(const Type &type, const boost::any &value)
Definition: conversion.h:72
virtual double as_double(const boost::any &value) const =0
virtual boost::any from_int(int value) const =0
TypePtr type(const std::string &name)
Definition: types.cpp:82
virtual boost::any from_string(const std::string &value) const =0
virtual unsigned as_unsigned(const boost::any &value) const =0
virtual boost::any from_double(double value) const =0
virtual boost::any from_unsigned(unsigned value) const =0
static OtherT to(const Type &type, const boost::any &value)
Definition: conversion.h:43
static boost::any from(const Type &type, const OtherT &value)
Definition: conversion.h:44


cpp_introspection
Author(s): Johannes Meyer
autogenerated on Mon Jun 10 2019 12:56:17