references.hpp
Go to the documentation of this file.
1 
11 /*****************************************************************************
12 ** Ifdefs
13 *****************************************************************************/
14 
15 #ifndef ECL_UTILITIES_REFERENCE_WRAPPER_HPP_
16 #define ECL_UTILITIES_REFERENCE_WRAPPER_HPP_
17 
18 /*****************************************************************************
19 ** Includes
20 *****************************************************************************/
21 
22 #include <ecl/mpl/bool.hpp>
23 
24 /*****************************************************************************
25 ** Wrappers
26 *****************************************************************************/
27 
28 namespace ecl {
29 
38 template <typename T>
39 T* addressOf( T & v ) {
40  return reinterpret_cast<T*>(
41  &const_cast<char&>(reinterpret_cast<const volatile char &>(v)));
42 }
84 template<typename T>
85 class ReferenceWrapper {
86 
87 public:
88  typedef T type;
97  ReferenceWrapper(T& t): obj_ptr(addressOf(t)) {}
98 
99  virtual ~ReferenceWrapper() {}
100 
109  operator T& () const { return *obj_ptr; }
110 
118  T& reference() const { return *obj_ptr; }
119 
127  T* pointer() const { return *obj_ptr; }
128 
129 private:
130  T* obj_ptr;
131 };
132 
133 /*****************************************************************************
134 ** Utility Functions
135 *****************************************************************************/
136 
144 template <typename T>
145 ReferenceWrapper<T> ref(T &wrapped_object) {
146  return ReferenceWrapper<T>(wrapped_object);
147 }
148 
156 template <typename T>
157 ReferenceWrapper<T const> cref(T const &wrapped_object) {
158  return ReferenceWrapper<T const>(wrapped_object);
159 }
160 
169 template <typename T>
170 class is_reference_wrapper : public False {};
171 
179 template <typename T>
180 class is_reference_wrapper< ReferenceWrapper<T> > : public True {};
181 
182 } // namespace ecl
183 
184 
185 #endif /* ECL_UTILITIES_REFERENCE_WRAPPER_HPP_ */
ecl::addressOf
T * addressOf(T &v)
Returns the address of an object (uses correct c++ syntactics).
Definition: references.hpp:45
ecl::ReferenceWrapper::reference
T & reference() const
Accessor to the reference of the original instance.
Definition: references.hpp:124
ecl::ReferenceWrapper::~ReferenceWrapper
virtual ~ReferenceWrapper()
Definition: references.hpp:105
ecl::cref
ReferenceWrapper< T const > cref(T const &wrapped_object)
Definition: references.hpp:163
ecl::ReferenceWrapper::type
T type
Definition: references.hpp:94
ecl::ReferenceWrapper::obj_ptr
T * obj_ptr
Definition: references.hpp:136
ecl::ReferenceWrapper::pointer
T * pointer() const
Accessor to a pointer to the original instance.
Definition: references.hpp:133
True
Bool< true > True
ecl::ReferenceWrapper
Provides a wrapper which allows the original object to be passed like a reference.
Definition: references.hpp:91
ecl::ref
ReferenceWrapper< T > ref(T &wrapped_object)
Definition: references.hpp:151
bool.hpp
False
Bool< false > False
ecl::ReferenceWrapper::ReferenceWrapper
ReferenceWrapper(T &t)
Constructs the wrapper around the supplied object instance.
Definition: references.hpp:103
ecl
Embedded control libraries.


ecl_utilities
Author(s): Daniel Stonier
autogenerated on Sun Aug 2 2020 03:12:13