| OLD | NEW |
| (Empty) |
| 1 // Copyright 2005-2009 Google Inc. | |
| 2 // | |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 // you may not use this file except in compliance with the License. | |
| 5 // You may obtain a copy of the License at | |
| 6 // | |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 // | |
| 9 // Unless required by applicable law or agreed to in writing, software | |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 // See the License for the specific language governing permissions and | |
| 13 // limitations under the License. | |
| 14 // ======================================================================== | |
| 15 | |
| 16 #ifndef OMAHA_COMMON_TYPE_UTILS_H_ | |
| 17 #define OMAHA_COMMON_TYPE_UTILS_H_ | |
| 18 | |
| 19 namespace omaha { | |
| 20 | |
| 21 // | |
| 22 // Detecting convertibility and inheritence at compile time | |
| 23 // (Extracted from: Modern C++ Design) | |
| 24 // | |
| 25 | |
| 26 // Evaluates true if U inherites from T publically, or if T and U are same type | |
| 27 #define SUPERSUBCLASS(T, U) \ | |
| 28 (ConversionUtil<const U*, const T*>::exists && \ | |
| 29 !ConversionUtil<const T*, const void*>::same_type) | |
| 30 | |
| 31 // Evaluates true only if U inherites from T publically | |
| 32 #define SUPERSUBCLASS_STRICT(T, U) \ | |
| 33 (SUPERSUBCLASS(T, U) && \ | |
| 34 !ConversionUtil<const T, const U>::same_type) | |
| 35 | |
| 36 // Perform type test | |
| 37 template <class T, class U> | |
| 38 class ConversionUtil { | |
| 39 private: | |
| 40 typedef char Small; | |
| 41 class Big { | |
| 42 char dummy[2]; | |
| 43 }; | |
| 44 static Small Test(U); | |
| 45 static Big Test(...); | |
| 46 static T MakeT(); | |
| 47 | |
| 48 public: | |
| 49 // Tell whether there is ConversionUtil from T to U | |
| 50 enum { exists = sizeof(Test(MakeT())) == sizeof(Small) }; | |
| 51 | |
| 52 // Tells whether there are ConversionUtils between T and U in both directions | |
| 53 enum { exists_2way = exists && ConversionUtil<U, T>::exists }; | |
| 54 | |
| 55 // Tells whether there are same type | |
| 56 enum { same_type = false }; | |
| 57 }; | |
| 58 | |
| 59 // Perform same type test through partial template specialization | |
| 60 template<class T> | |
| 61 class ConversionUtil<T, T> { | |
| 62 public: | |
| 63 enum { exists = 1, exists_2way = 1, same_type = 1 }; | |
| 64 }; | |
| 65 | |
| 66 } // namespace omaha | |
| 67 | |
| 68 #endif // OMAHA_COMMON_TYPE_UTILS_H_ | |
| OLD | NEW |