1 | /** @file
|
---|
2 | * IPRT - C++ Utilities (useful templates, defines and such).
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2015 Oracle Corporation
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License (GPL) as published by the Free Software
|
---|
12 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
13 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
14 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | *
|
---|
16 | * The contents of this file may alternatively be used under the terms
|
---|
17 | * of the Common Development and Distribution License Version 1.0
|
---|
18 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
19 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
20 | * CDDL are applicable instead of those of the GPL.
|
---|
21 | *
|
---|
22 | * You may elect to license modified versions of this file under the
|
---|
23 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #ifndef ___iprt_cpputils_h
|
---|
27 | #define ___iprt_cpputils_h
|
---|
28 |
|
---|
29 | /** @defgroup grp_rt_cpp IPRT C++ APIs */
|
---|
30 |
|
---|
31 | /** @defgroup grp_rt_cpp_util C++ Utilities
|
---|
32 | * @ingroup grp_rt_cpp
|
---|
33 | * @{
|
---|
34 | */
|
---|
35 |
|
---|
36 | #define DPTR(CLASS) CLASS##Private *d = static_cast<CLASS##Private *>(d_ptr)
|
---|
37 | #define QPTR(CLASS) CLASS *q = static_cast<CLASS *>(q_ptr)
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * A simple class used to prevent copying and assignment.
|
---|
41 | *
|
---|
42 | * Inherit from this class in order to prevent automatic generation
|
---|
43 | * of the copy constructor and assignment operator in your class.
|
---|
44 | */
|
---|
45 | class RTCNonCopyable
|
---|
46 | {
|
---|
47 | protected:
|
---|
48 | RTCNonCopyable() {}
|
---|
49 | ~RTCNonCopyable() {}
|
---|
50 | private:
|
---|
51 | RTCNonCopyable(RTCNonCopyable const &);
|
---|
52 | RTCNonCopyable const &operator=(RTCNonCopyable const &);
|
---|
53 | };
|
---|
54 |
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Shortcut to |const_cast<C &>()| that automatically derives the correct
|
---|
58 | * type (class) for the const_cast template's argument from its own argument.
|
---|
59 | *
|
---|
60 | * Can be used to temporarily cancel the |const| modifier on the left-hand side
|
---|
61 | * of assignment expressions, like this:
|
---|
62 | * @code
|
---|
63 | * const Class That;
|
---|
64 | * ...
|
---|
65 | * unconst(That) = SomeValue;
|
---|
66 | * @endcode
|
---|
67 | *
|
---|
68 | * @todo What to do about the prefix here?
|
---|
69 | */
|
---|
70 | template <class C>
|
---|
71 | inline C &unconst(const C &that)
|
---|
72 | {
|
---|
73 | return const_cast<C &>(that);
|
---|
74 | }
|
---|
75 |
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Shortcut to |const_cast<C *>()| that automatically derives the correct
|
---|
79 | * type (class) for the const_cast template's argument from its own argument.
|
---|
80 | *
|
---|
81 | * Can be used to temporarily cancel the |const| modifier on the left-hand side
|
---|
82 | * of assignment expressions, like this:
|
---|
83 | * @code
|
---|
84 | * const Class *pThat;
|
---|
85 | * ...
|
---|
86 | * unconst(pThat) = SomeValue;
|
---|
87 | * @endcode
|
---|
88 | *
|
---|
89 | * @todo What to do about the prefix here?
|
---|
90 | */
|
---|
91 | template <class C>
|
---|
92 | inline C *unconst(const C *that)
|
---|
93 | {
|
---|
94 | return const_cast<C *>(that);
|
---|
95 | }
|
---|
96 |
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Macro for generating a non-const getter version from a const getter.
|
---|
100 | *
|
---|
101 | * @param a_RetType The getter return type.
|
---|
102 | * @param a_Class The class name.
|
---|
103 | * @param a_Getter The getter name.
|
---|
104 | * @param a_ArgDecls The argument declaration for the getter method.
|
---|
105 | * @param a_ArgList The argument list for the call.
|
---|
106 | */
|
---|
107 | #define RT_CPP_GETTER_UNCONST(a_RetType, a_Class, a_Getter, a_ArgDecls, a_ArgList) \
|
---|
108 | a_RetType a_Getter a_ArgDecls \
|
---|
109 | { \
|
---|
110 | return static_cast< a_Class const *>(this)-> a_Getter a_ArgList; \
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Macro for generating a non-const getter version from a const getter,
|
---|
116 | * unconsting the return value as well.
|
---|
117 | *
|
---|
118 | * @param a_RetType The getter return type.
|
---|
119 | * @param a_Class The class name.
|
---|
120 | * @param a_Getter The getter name.
|
---|
121 | * @param a_ArgDecls The argument declaration for the getter method.
|
---|
122 | * @param a_ArgList The argument list for the call.
|
---|
123 | */
|
---|
124 | #define RT_CPP_GETTER_UNCONST_RET(a_RetType, a_Class, a_Getter, a_ArgDecls, a_ArgList) \
|
---|
125 | a_RetType a_Getter a_ArgDecls \
|
---|
126 | { \
|
---|
127 | return const_cast<a_RetType>(static_cast< a_Class const *>(this)-> a_Getter a_ArgList); \
|
---|
128 | }
|
---|
129 |
|
---|
130 |
|
---|
131 | /** @} */
|
---|
132 |
|
---|
133 | #endif
|
---|
134 |
|
---|