X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/a6680ce3d763763a6010c81c8a5a8f7a1ce052db..8c6fa3fbaccc22127280bf77a48fab5a3ee0716e:/include/stg/ref.h diff --git a/include/stg/ref.h b/include/stg/ref.h new file mode 100644 index 00000000..e1bdb54d --- /dev/null +++ b/include/stg/ref.h @@ -0,0 +1,54 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Author : Maxim Mamontov + */ + +namespace STG +{ + +// The implementation is derived from +// http://en.cppreference.com/w/cpp/utility/functional/reference_wrapper +// and +// http://en.cppreference.com/w/cpp/memory/addressof + +template< class T > +T* AddressOf(T& arg) +{ + return reinterpret_cast(&const_cast(reinterpret_cast(arg))); +} + +template +class RefWrapper { + public: + typedef T type; + RefWrapper(T& ref) throw() : m_ptr(AddressOf(ref)) {} + RefWrapper(const RefWrapper& rhs) : m_ptr(rhs.m_ptr) {} + RefWrapper& operator=(const RefWrapper& rhs) throw() { m_ptr = rhs.m_ptr; } + operator T& () const throw() { return *m_ptr; } + T& get() const throw() { return *m_ptr; } + + void operator()() { (*m_ptr)(); } + template + void operator()(A a) { (*m_ptr)(a); } + template + void operator()(A1 a1, A2 a2) { (*m_ptr)(a1, a2); } + private: + T* m_ptr; +}; + +} // namespace STG