lean cpp library
A lean C++ library providing efficient utility classes for high-performance C++ applications.
type_traits.h
00001 /*****************************************************/
00002 /* lean Meta                    (c) Tobias Zirr 2011 */
00003 /*****************************************************/
00004 
00005 #ifndef LEAN_META_TYPE_TRAITS
00006 #define LEAN_META_TYPE_TRAITS
00007 
00008 namespace lean
00009 {
00010 namespace meta
00011 {
00012 
00014 template <class Type1, class Type2>
00015 struct is_equal
00016 {
00018     static const bool value = false;
00019 };
00020 
00021 #ifndef DOXYGEN_SKIP_THIS
00022 
00023 template <class Type>
00024 struct is_equal<Type, Type>
00025 {
00026     static const bool value = true;
00027 };
00028 
00029 #endif
00030 
00032 template <class Integer>
00033 struct is_unsigned
00034 {
00035     static const Integer min = Integer(0);
00036     static const Integer max = Integer(-1);
00037 
00039     static const bool value = (max > min);
00040 };
00041 
00043 template <class Type, class Base>
00044 struct is_derived
00045 {
00046 private:
00047     typedef char yes[1];
00048     typedef char no[2];
00049 
00050     static yes& sfinae_check(Base*);
00051     static no& sfinae_check(void*);
00052 
00053 public:
00055     static const bool value = (
00056         sizeof( is_derived::sfinae_check( static_cast<Type*>(nullptr) ) )
00057         ==
00058         sizeof(yes) );
00059 };
00060 
00061 } // namespace
00062 
00063 using meta::is_equal;
00064 using meta::is_unsigned;
00065 using meta::is_derived;
00066 
00068 #define LEAN_DEFINE_HAS_TYPE(TypeName)                                                  \
00069     template <class Type>                                                               \
00070     class has_type_##TypeName                                                           \
00071     {                                                                                   \
00072     private:                                                                            \
00073         typedef char yes[1];                                                            \
00074         typedef char no[2];                                                             \
00075                                                                                         \
00076         template <class T>                                                              \
00077         static yes& sfinae_check(T*, typename T::TypeName* = nullptr);                  \
00078         static no& sfinae_check(...);                                                   \
00079                                                                                         \
00080     public:                                                                             \
00081         static const bool value =                                                       \
00082             sizeof( has_type_##TypeName::sfinae_check( static_cast<Type*>(nullptr) ) )  \
00083             ==                                                                          \
00084             sizeof(yes);                                                                \
00085     };
00086 
00087 } // namespace
00088 
00089 #endif