VirtualBox

source: vbox/trunk/include/iprt/cpp/utils.h@ 78286

Last change on this file since 78286 was 76585, checked in by vboxsync, 6 years ago

*: scm --fix-header-guard-endif

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 4.1 KB
Line 
1/** @file
2 * IPRT - C++ Utilities (useful templates, defines and such).
3 */
4
5/*
6 * Copyright (C) 2006-2019 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_INCLUDED_cpp_utils_h
27#define IPRT_INCLUDED_cpp_utils_h
28#ifndef RT_WITHOUT_PRAGMA_ONCE
29# pragma once
30#endif
31
32#include <iprt/types.h>
33
34/** @defgroup grp_rt_cpp IPRT C++ APIs */
35
36/** @defgroup grp_rt_cpp_util C++ Utilities
37 * @ingroup grp_rt_cpp
38 * @{
39 */
40
41#define DPTR(CLASS) CLASS##Private *d = static_cast<CLASS##Private *>(d_ptr)
42#define QPTR(CLASS) CLASS *q = static_cast<CLASS *>(q_ptr)
43
44/**
45 * A simple class used to prevent copying and assignment.
46 *
47 * Inherit from this class in order to prevent automatic generation
48 * of the copy constructor and assignment operator in your class.
49 */
50class RTCNonCopyable
51{
52protected:
53 RTCNonCopyable() {}
54 ~RTCNonCopyable() {}
55private:
56 RTCNonCopyable(RTCNonCopyable const &);
57 RTCNonCopyable &operator=(RTCNonCopyable const &);
58};
59
60
61/**
62 * Shortcut to |const_cast<C &>()| that automatically derives the correct
63 * type (class) for the const_cast template's argument from its own argument.
64 *
65 * Can be used to temporarily cancel the |const| modifier on the left-hand side
66 * of assignment expressions, like this:
67 * @code
68 * const Class That;
69 * ...
70 * unconst(That) = SomeValue;
71 * @endcode
72 *
73 * @todo What to do about the prefix here?
74 */
75template <class C>
76inline C &unconst(const C &that)
77{
78 return const_cast<C &>(that);
79}
80
81
82/**
83 * Shortcut to |const_cast<C *>()| that automatically derives the correct
84 * type (class) for the const_cast template's argument from its own argument.
85 *
86 * Can be used to temporarily cancel the |const| modifier on the left-hand side
87 * of assignment expressions, like this:
88 * @code
89 * const Class *pThat;
90 * ...
91 * unconst(pThat) = SomeValue;
92 * @endcode
93 *
94 * @todo What to do about the prefix here?
95 */
96template <class C>
97inline C *unconst(const C *that)
98{
99 return const_cast<C *>(that);
100}
101
102
103/**
104 * Macro for generating a non-const getter version from a const getter.
105 *
106 * @param a_RetType The getter return type.
107 * @param a_Class The class name.
108 * @param a_Getter The getter name.
109 * @param a_ArgDecls The argument declaration for the getter method.
110 * @param a_ArgList The argument list for the call.
111 */
112#define RT_CPP_GETTER_UNCONST(a_RetType, a_Class, a_Getter, a_ArgDecls, a_ArgList) \
113 a_RetType a_Getter a_ArgDecls \
114 { \
115 return static_cast< a_Class const *>(this)-> a_Getter a_ArgList; \
116 }
117
118
119/**
120 * Macro for generating a non-const getter version from a const getter,
121 * unconsting the return value as well.
122 *
123 * @param a_RetType The getter return type.
124 * @param a_Class The class name.
125 * @param a_Getter The getter name.
126 * @param a_ArgDecls The argument declaration for the getter method.
127 * @param a_ArgList The argument list for the call.
128 */
129#define RT_CPP_GETTER_UNCONST_RET(a_RetType, a_Class, a_Getter, a_ArgDecls, a_ArgList) \
130 a_RetType a_Getter a_ArgDecls \
131 { \
132 return const_cast<a_RetType>(static_cast< a_Class const *>(this)-> a_Getter a_ArgList); \
133 }
134
135
136/** @} */
137
138#endif /* !IPRT_INCLUDED_cpp_utils_h */
139
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette