1 // To generate typewrapping.h from typewrapping.h.pump, execute:
2 // /home/build/google3/third_party/gtest/scripts/pump.py typewrapping.h.pump
4 // Copyright 2009 Google Inc.
5 // Author: tschmelcher@google.com (Tristan Schmelcher)
7 // A template meta-programming framework for customizable rule-based
8 // type-checking of type wrappers and wrapper functions.
10 // This framework is useful in a scenario where there are a set of types that
11 // you choose to "wrap" by implementing new preferred types such that the new
12 // and the old can be converted back and forth in some way, but you already have
13 // a library of functions that expect the original types. Example:
19 // And function X Foo(Y, Z) exists.
21 // Since A, B, and C are preferred, you choose to implement a wrapper function
22 // with this interface:
26 // However, this can lead to subtle discrepancies, because if the interface to
27 // Foo ever changes then Foo2 may become out-of-sync. e.g., Foo might have
28 // originally returned void, but later is changed to return an error code. If
29 // the programmer forgets to change Foo2, the code will probably still work, but
30 // with an implicit cast to void inserted by the compiler, potentially leading
31 // to run-time errors or errors in usage.
33 // The purpose of this library is to prevent these discrepancies from occurring.
34 // You use it as follows:
36 // First, declare a new wrapping ruleset:
38 // DECLARE_WRAPPING_RULESET(ruleset_name)
40 // Then declare rules on what types wrap which other types and how to convert
43 // DECLARE_WRAPPER(ruleset_name, A, X, variable_name, wrapping_code,
46 // Where wrapping_code and unwrapping_code are expressions giving the code to
47 // use to wrap and unwrap a variable with the name "variable_name". There are
48 // also some helper macros to declare common wrapping schemes.
50 // Then implement your wrapped functions like this:
52 // A Foo_Wrapped(B b, C c) {
53 // return WRAP_CALL2(ruleset_name, A, Foo, B, b, C, c);
56 // WRAP_CALL2 will unwrap b and c (if B and C are wrapped types) and call Foo,
57 // then wrap the result to type A if different from the return type. More
58 // importantly, if the types in Foo's interface do not _exactly_ match the
59 // unwrapped forms of A, B, and C (after typedef-equivalence), then you will get
60 // a compile-time error for a static_cast from the real function type to the
61 // expected one (except on Mac where this check is infeasible), and with no icky
62 // template instantiation errors either!
64 // There are also macros to wrap/unwrap individual values according to whichever
65 // rule applies to their types:
67 // WRAP(ruleset_name, A, X, value) // Compile-time error if no associated rule.
69 // UNWRAP(ruleset_name, A, value) // Infers X. If A is not a wrapper, no change.
71 // UNWRAP_TYPE(ruleset_name, A) // Evaluates to X.
74 // Essentially, the library works by "storing" the DECLARE_WRAPPER calls in
75 // template specializations. When the wrapper or unwrapper is invoked, the
76 // normal C++ template system essentially "looks up" the rule for the given
79 // All of the auto-generated code can be inlined to produce zero impact on
80 // run-time performance and code size (though some compilers may require
81 // gentle encouragement in order for them to do so).
83 #ifndef TALK_SESSION_PHONE_TYPEWRAPPING_H_
84 #define TALK_SESSION_PHONE_TYPEWRAPPING_H_
86 #include "webrtc/base/common.h"
89 // XCode's GCC doesn't respect typedef-equivalence when casting function pointer
90 // types, so we can't enforce that the wrapped function signatures strictly
91 // match the expected types. Instead we have to forego the nice user-friendly
92 // static_cast check (because it will spuriously fail) and make the Call()
93 // function into a member template below.
94 #define CAST_FUNCTION_(function, ...) \
97 #define CAST_FUNCTION_(function, ...) \
98 static_cast<__VA_ARGS__>(function)
101 // Internal helper macros.
102 #define SMART_WRAPPER_(wrapper, toType, fromType, from) \
103 (wrapper<toType, fromType>::Wrap(from))
105 #define SMART_UNWRAPPER_(unwrapper, fromType, from) \
106 (unwrapper<fromType>::Unwrap(from))
108 #define SMART_UNWRAPPER_TYPE_(unwrapper, fromType) \
109 typename unwrapper<fromType>::ToType
117 // The code that follows wraps calls to $i-argument functions, unwrapping the
118 // arguments and wrapping the return value as needed.
122 template <typename ToType, typename FromType> class Wrapper,
123 template <typename FromType> class Unwrapper,
124 typename ReturnType$for j [[,
125 typename ArgType$j]]>
126 class SmartFunctionWrapper$i {
128 typedef SMART_UNWRAPPER_TYPE_(Unwrapper, ReturnType) OriginalReturnType;
131 typedef SMART_UNWRAPPER_TYPE_(Unwrapper, ArgType$j) OriginalArgType$j;
134 typedef OriginalReturnType (*OriginalFunctionType)($for j , [[
136 OriginalArgType$j]]);
139 template <typename F>
140 static FORCE_INLINE ReturnType Call(F function
142 static FORCE_INLINE ReturnType Call(OriginalFunctionType function
146 return SMART_WRAPPER_(Wrapper, ReturnType, OriginalReturnType,
147 (*function)($for j , [[
149 SMART_UNWRAPPER_(Unwrapper, ArgType$j, v$j)]]));
153 // Special case for functions that return void. (SMART_WRAPPER_ involves
154 // passing the unwrapped value in a function call, which is not a legal thing to
155 // do with void, so we need a special case here that doesn't call
156 // SMART_WRAPPER_()).
158 template <typename ToType, typename FromType> class Wrapper,
159 template <typename FromType> class Unwrapper$for j [[,
160 typename ArgType$j]]>
161 class SmartFunctionWrapper$i<
167 typedef void OriginalReturnType;
170 typedef SMART_UNWRAPPER_TYPE_(Unwrapper, ArgType$j) OriginalArgType$j;
173 typedef OriginalReturnType (*OriginalFunctionType)($for j , [[
175 OriginalArgType$j]]);
178 template <typename F>
179 static FORCE_INLINE void Call(F function
181 static FORCE_INLINE void Call(OriginalFunctionType function
185 (*function)($for j , [[
187 SMART_UNWRAPPER_(Unwrapper, ArgType$j, v$j)]]);
193 // Programmer interface follows. Only macros below here should be used outside
196 #define DECLARE_WRAPPING_RULESET(ruleSet) \
197 namespace ruleSet { \
199 /* SmartWrapper is for wrapping values. */ \
200 template<typename ToType, typename FromType> \
201 class SmartWrapper; \
203 /* Special case where the types are the same. */ \
204 template<typename T1> \
205 class SmartWrapper<T1, T1> { \
207 static FORCE_INLINE T1 Wrap(T1 from) { \
212 /* Class for unwrapping (i.e., going to the original value). This is done
213 function-style rather than predicate-style. The default rule is to leave
214 the type unchanged. */ \
215 template<typename FromType> \
216 class SmartUnwrapper { \
218 typedef FromType ToType; \
219 static FORCE_INLINE ToType Unwrap(FromType from) { \
226 // Declares a wrapping rule.
227 #define DECLARE_WRAPPER(ruleSet, wrappedType, unwrappedType, var, wrapCode, unwrapCode) \
228 namespace ruleSet { \
231 class SmartWrapper<wrappedType, unwrappedType> { \
233 static FORCE_INLINE wrappedType Wrap(unwrappedType var) { \
239 class SmartUnwrapper<wrappedType> { \
241 typedef unwrappedType ToType; \
242 static FORCE_INLINE unwrappedType Unwrap(wrappedType var) { \
249 // Helper macro for declaring a wrapper that wraps/unwraps with reinterpret_cast<>.
250 #define DECLARE_WRAPPER_BY_REINTERPRET_CAST(ruleSet, wrappedType, unwrappedType) \
251 DECLARE_WRAPPER(ruleSet, wrappedType, unwrappedType, FROM, reinterpret_cast<wrappedType>(FROM), reinterpret_cast<unwrappedType>(FROM))
253 // Helper macro for declaring a wrapper that wraps/unwraps implicitly.
254 #define DECLARE_WRAPPER_BY_IMPLICIT_CAST(ruleSet, wrappedType, unwrappedType) \
255 DECLARE_WRAPPER(ruleSet, wrappedType, unwrappedType, FROM, FROM, FROM)
257 // Helper macro for declaring that the pointer types for one type wrap the pointer types for another type.
258 #define DECLARE_POINTER_WRAPPER(ruleSet, wrappedType, unwrappedType) \
259 DECLARE_WRAPPER_BY_REINTERPRET_CAST(ruleSet, wrappedType*, unwrappedType*) \
260 DECLARE_WRAPPER_BY_REINTERPRET_CAST(ruleSet, const wrappedType*, const unwrappedType*) \
261 DECLARE_WRAPPER_BY_REINTERPRET_CAST(ruleSet, wrappedType* const, unwrappedType* const) \
262 DECLARE_WRAPPER_BY_REINTERPRET_CAST(ruleSet, const wrappedType* const, const unwrappedType* const) \
264 // Macro to wrap a single value.
265 #define WRAP(ruleSet, toType, fromType, from) \
266 SMART_WRAPPER_(ruleSet::SmartWrapper, toType, fromType, from)
268 // Macro to unwrap a single value.
269 #define UNWRAP(ruleSet, fromType, from) \
270 SMART_UNWRAPPER_(ruleSet::SmartUnwrapper, fromType, from)
272 // Macro to get the unwrapped form of a type.
273 #define UNWRAP_TYPE(ruleSet, fromType) \
274 SMART_UNWRAPPER_TYPE_(ruleSet::SmartUnwrapper, from)
276 // Macros to wrap function calls.
280 #define WRAP_CALL$i(ruleSet, toType, function$for j [[, argType$j, arg$j]]) \
281 (SmartFunctionWrapper$i< \
282 ruleSet::SmartWrapper, \
283 ruleSet::SmartUnwrapper, \
285 argType$j]]>::Call( \
288 SmartFunctionWrapper$i< \
289 ruleSet::SmartWrapper, \
290 ruleSet::SmartUnwrapper, \
292 argType$j]]>::OriginalFunctionType)$for j [[, \
297 #endif // TALK_SESSION_PHONE_TYPEWRAPPINGHELPERS_H_