utils.h revision c1d279fc0865b91a40b30eda02ed14f6533fe1a4
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync/** @file
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync * IPRT - C++ Utilities (useful templates, defines and such).
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync */
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync/*
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync * Copyright (C) 2006-2015 Oracle Corporation
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync *
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync * available from http://www.virtualbox.org. This file is free software;
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync * you can redistribute it and/or modify it under the terms of the GNU
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync * General Public License (GPL) as published by the Free Software
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync *
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync * The contents of this file may alternatively be used under the terms
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync * of the Common Development and Distribution License Version 1.0
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync * VirtualBox OSE distribution, in which case the provisions of the
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync * CDDL are applicable instead of those of the GPL.
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync *
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync * You may elect to license modified versions of this file under the
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync * terms and conditions of either the GPL or the CDDL or both.
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync */
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync#ifndef ___iprt_cpputils_h
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync#define ___iprt_cpputils_h
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync/** @defgroup grp_rt_cpp IPRT C++ APIs */
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync/** @defgroup grp_rt_cpp_util C++ Utilities
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync * @ingroup grp_rt_cpp
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync * @{
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync */
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync#define DPTR(CLASS) CLASS##Private *d = static_cast<CLASS##Private *>(d_ptr)
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync#define QPTR(CLASS) CLASS *q = static_cast<CLASS *>(q_ptr)
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync/**
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync * A simple class used to prevent copying and assignment.
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync *
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync * Inherit from this class in order to prevent automatic generation
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync * of the copy constructor and assignment operator in your class.
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync */
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsyncclass RTCNonCopyable
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync{
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsyncprotected:
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync RTCNonCopyable() {}
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync ~RTCNonCopyable() {}
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsyncprivate:
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync RTCNonCopyable(RTCNonCopyable const &);
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync RTCNonCopyable const &operator=(RTCNonCopyable const &);
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync};
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync/**
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync * Shortcut to |const_cast<C &>()| that automatically derives the correct
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync * type (class) for the const_cast template's argument from its own argument.
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync *
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync * Can be used to temporarily cancel the |const| modifier on the left-hand side
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync * of assignment expressions, like this:
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync * @code
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync * const Class That;
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync * ...
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync * unconst(That) = SomeValue;
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync * @endcode
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync *
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync * @todo What to do about the prefix here?
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync */
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsynctemplate <class C>
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsyncinline C &unconst(const C &that)
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync{
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync return const_cast<C &>(that);
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync}
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync/**
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync * Shortcut to |const_cast<C *>()| that automatically derives the correct
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync * type (class) for the const_cast template's argument from its own argument.
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync *
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync * Can be used to temporarily cancel the |const| modifier on the left-hand side
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync * of assignment expressions, like this:
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync * @code
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync * const Class *pThat;
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync * ...
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync * unconst(pThat) = SomeValue;
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync * @endcode
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync *
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync * @todo What to do about the prefix here?
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync */
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsynctemplate <class C>
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsyncinline C *unconst(const C *that)
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync{
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync return const_cast<C *>(that);
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync}
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync/**
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync * Macro for generating a non-const getter version from a const getter.
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync *
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync * @param a_RetType The getter return type.
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync * @param a_Class The class name.
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync * @param a_Getter The getter name.
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync * @param a_ArgDecls The argument declaration for the getter method.
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync * @param a_ArgList The argument list for the call.
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync */
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync#define RT_CPP_GETTER_UNCONST(a_RetType, a_Class, a_Getter, a_ArgDecls, a_ArgList) \
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync a_RetType a_Getter a_ArgDecls \
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync { \
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync return static_cast< a_Class const *>(this)-> a_Getter a_ArgList; \
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync }
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync
d606b96aa8a4be8f7d2da410f982889804c27b92vboxsync/**
* Macro for generating a non-const getter version from a const getter,
* unconsting the return value as well.
*
* @param a_RetType The getter return type.
* @param a_Class The class name.
* @param a_Getter The getter name.
* @param a_ArgDecls The argument declaration for the getter method.
* @param a_ArgList The argument list for the call.
*/
#define RT_CPP_GETTER_UNCONST_RET(a_RetType, a_Class, a_Getter, a_ArgDecls, a_ArgList) \
a_RetType a_Getter a_ArgDecls \
{ \
return const_cast<a_RetType>(static_cast< a_Class const *>(this)-> a_Getter a_ArgList); \
}
/** @} */
#endif