diff options
Diffstat (limited to 'ml/dlib/dlib/ref.h')
-rw-r--r-- | ml/dlib/dlib/ref.h | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/ml/dlib/dlib/ref.h b/ml/dlib/dlib/ref.h new file mode 100644 index 000000000..53a37fbf8 --- /dev/null +++ b/ml/dlib/dlib/ref.h @@ -0,0 +1,84 @@ +// Copyright (C) 2010 Davis E. King (davis@dlib.net) +// License: Boost Software License See LICENSE.txt for the full license. +#ifndef DLIB_REFERENCE_WRAPpER_H_ +#define DLIB_REFERENCE_WRAPpER_H_ + +namespace dlib +{ + +// ---------------------------------------------------------------------------------------- + + template< + typename T + > + class reference_wrapper + { + /*! + WHAT THIS OBJECT REPRESENTS + This is a simple object that just holds a reference to another object. + It is useful because it can serve as a kind of "copyable reference". + !*/ + + public: + typedef T type; + + explicit reference_wrapper(T& o) : obj(&o) {} + + operator T&() const { return *obj; } + T& get() const { return *obj; } + + private: + T* obj; + }; + +// ---------------------------------------------------------------------------------------- + + template <typename T> + reference_wrapper<T> ref( + T& obj + ) { return reference_wrapper<T>(obj); } + /*! + ensures + - returns a reference_wrapper that contains a reference to obj. + !*/ + +// ---------------------------------------------------------------------------------------- + + template <typename T> + reference_wrapper<T> ref( + reference_wrapper<T> obj + ) { return obj; } + /*! + ensures + - returns the given reference_wrapper object without modification + !*/ + +// ---------------------------------------------------------------------------------------- + + template <typename T> + reference_wrapper<const T> cref( + const T& obj + ) { return reference_wrapper<const T>(obj); } + /*! + ensures + - returns a reference_wrapper that contains a constant reference to obj. + !*/ + +// ---------------------------------------------------------------------------------------- + + template <typename T> + reference_wrapper<const T> cref( + reference_wrapper<T> obj + ) { return cref(obj.get()); } + /*! + ensures + - converts the given reference_wrapper into a reference_wrapper that contains a + constant reference. + !*/ + +// ---------------------------------------------------------------------------------------- + +} + +#endif // DLIB_REFERENCE_WRAPpER_H_ + |