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_ */
T * pointer() const
T * addressOf(T &v)
Returns the address of an object (uses correct c++ syntactics).
Definition: references.hpp:39
ReferenceWrapper< T const > cref(T const &wrapped_object)
Definition: references.hpp:157
virtual ~ReferenceWrapper()
Provides a wrapper which allows the original object to be passed like a reference.
Definition: references.hpp:85
T & reference() const
Default action for detection of the reference wrapper type trait (false).
Definition: references.hpp:170
ReferenceWrapper< T > ref(T &wrapped_object)
Definition: references.hpp:145
Integral constant wrapper for boolean values.
Definition: bool.hpp:32


xbot_node
Author(s): Roc, wangpeng@droid.ac.cn
autogenerated on Sat Oct 10 2020 03:28:13