Back to index

nux  3.0.0
Template.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Inalogic® Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License, as
00006  * published by the  Free Software Foundation; either version 2.1 or 3.0
00007  * of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00019  *
00020  */
00021 
00022 
00023 #ifndef NTEMPLATE_H
00024 #define NTEMPLATE_H
00025 
00026 namespace nux
00027 {
00028 
00029   class NString;
00030 
00031 // Number of elements in an array.
00032 #define NUX_ARRAY_COUNT( array ) \
00033     ( sizeof(array) / sizeof((array)[0]) )
00034 
00035 
00036 // -----------------------------------------------------------------------------
00037 // Type information.
00038 // -----------------------------------------------------------------------------
00039 
00040 // Is void
00041   template <typename U> struct VoidTraits
00042   {
00043     enum { result = 0 };
00044   };
00045 // Full specialization of template <typename T> NTraitIsVoid
00046   template <> struct VoidTraits<void>
00047   {
00048     enum { result = 1 };
00049   };
00050 
00051 // Is pointer
00052   template <typename U> struct PointerTraits
00053   {
00054     enum { result = 0 };
00055   };
00056 // Partial specialization
00057   template <typename U> struct PointerTraits<U *>
00058   {
00059     enum { result = 1 };
00060   };
00061 
00062   template <typename U> struct UnicodeCharTraits
00063   {
00064     enum { result = 0 };
00065   };
00066 
00067   template <> struct UnicodeCharTraits<wchar_t>
00068   {
00069     enum { result = 1 };
00070   };
00071 
00072   template <typename U> struct AnsiCharTraits
00073   {
00074     enum { result = 0 };
00075   };
00076 
00077   template <> struct AnsiCharTraits<char>
00078   {
00079     enum { result = 1 };
00080   };
00081 
00082 // Base type information for atomic types which pass by value.
00083   template<typename T>
00084   class TypeTraitsNoConstructor
00085   {
00086   public:
00087     typedef T ConstInitType;
00088     enum { HasConstructor = 0      };
00089     enum { HasDestructor = 0       };
00090   };
00091 
00092 // Base type information for constructed types which pass by reference.
00093   template<typename T>
00094   class TypeTraitsConstructor
00095   {
00096   public:
00097     typedef const T &ConstInitType;
00098     enum { HasConstructor = 1      };
00099     enum { HasDestructor = 1       };
00100   };
00101 
00102 
00103 // The default behaviour is for types to behave as constructed types.
00104   template<typename T>
00105   class ConstructorTraits : public TypeTraitsConstructor<T> {};
00106 
00107 // Pointers don't have a constructor.
00108   template<typename T>
00109   class ConstructorTraits<T *>: public TypeTraitsNoConstructor<T *> {};
00110 
00111   template <> class ConstructorTraits<bool>               : public TypeTraitsNoConstructor<bool>     {};
00112   template <> class ConstructorTraits<char>               : public TypeTraitsNoConstructor<char>     {};
00113   template <> class ConstructorTraits<unsigned char>      : public TypeTraitsNoConstructor<unsigned char>     {};
00114   template <> class ConstructorTraits<short>              : public TypeTraitsNoConstructor<short>     {};
00115   template <> class ConstructorTraits<unsigned short>     : public TypeTraitsNoConstructor<unsigned short>    {};
00116   template <> class ConstructorTraits<int>                : public TypeTraitsNoConstructor<int>      {};
00117   template <> class ConstructorTraits<unsigned int>       : public TypeTraitsNoConstructor<unsigned int>      {};
00118   template <> class ConstructorTraits<long>               : public TypeTraitsNoConstructor<long>    {};
00119   template <> class ConstructorTraits<unsigned long>      : public TypeTraitsNoConstructor<unsigned long>    {};
00120   template <> class ConstructorTraits<long long>          : public TypeTraitsNoConstructor<long long>    {};
00121   template <> class ConstructorTraits<unsigned long long> : public TypeTraitsNoConstructor<unsigned long long>    {};
00122   template <> class ConstructorTraits<float>              : public TypeTraitsNoConstructor<float>    {};
00123   template <> class ConstructorTraits<double>             : public TypeTraitsNoConstructor<double>   {};
00124 
00125   template<typename T>
00126   class NTypeTraits
00127   {
00128   private:
00129 
00130   public:
00131     enum { IsVoid = VoidTraits<T>::result };
00132     enum { IsPointer = PointerTraits<T>::result };
00133     enum { NeedsConstructor = ConstructorTraits<T>::HasConstructor };
00134     enum { NeedsDestructor = ConstructorTraits<T>::HasDestructor };
00135     enum { IsAnsiChar = AnsiCharTraits<T>::result };
00136     enum { IsUnicodeChar = UnicodeCharTraits<T>::result };
00137   };
00138 
00139   template <typename T>
00140   struct RemovePointerFromType
00141   {
00142     typedef T type;
00143   };
00144 
00145   template <typename T>
00146   struct RemovePointerFromType<T *>
00147   {
00148     typedef T type;
00149   };
00150   template <typename T>
00151   struct RemovePointerFromType<const T *>
00152   {
00153     typedef T type;
00154   };
00155   template <typename T>
00156   struct RemovePointerFromType<volatile T *>
00157   {
00158     typedef T type;
00159   };
00160   template <typename T>
00161   struct RemovePointerFromType<const volatile T *>
00162   {
00163     typedef T type;
00164   };
00165 
00166 
00167 }
00168 
00169 #endif // NTEMPLATE_H
00170