[ VIGRA Homepage | Function Index | Class Index | Namespaces | File List | Main Page ]

vigra/metaprogramming.hxx

00001 /************************************************************************/
00002 /*                                                                      */
00003 /*               Copyright 1998-2002 by Ullrich Koethe                  */
00004 /*       Cognitive Systems Group, University of Hamburg, Germany        */
00005 /*                                                                      */
00006 /*    This file is part of the VIGRA computer vision library.           */
00007 /*    The VIGRA Website is                                              */
00008 /*        http://kogs-www.informatik.uni-hamburg.de/~koethe/vigra/      */
00009 /*    Please direct questions, bug reports, and contributions to        */
00010 /*        ullrich.koethe@iwr.uni-heidelberg.de    or                    */
00011 /*        vigra@informatik.uni-hamburg.de                               */
00012 /*                                                                      */
00013 /*    Permission is hereby granted, free of charge, to any person       */
00014 /*    obtaining a copy of this software and associated documentation    */
00015 /*    files (the "Software"), to deal in the Software without           */
00016 /*    restriction, including without limitation the rights to use,      */
00017 /*    copy, modify, merge, publish, distribute, sublicense, and/or      */
00018 /*    sell copies of the Software, and to permit persons to whom the    */
00019 /*    Software is furnished to do so, subject to the following          */
00020 /*    conditions:                                                       */
00021 /*                                                                      */
00022 /*    The above copyright notice and this permission notice shall be    */
00023 /*    included in all copies or substantial portions of the             */
00024 /*    Software.                                                         */
00025 /*                                                                      */
00026 /*    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND    */
00027 /*    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES   */
00028 /*    OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND          */
00029 /*    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT       */
00030 /*    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,      */
00031 /*    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING      */
00032 /*    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR     */
00033 /*    OTHER DEALINGS IN THE SOFTWARE.                                   */                
00034 /*                                                                      */
00035 /************************************************************************/
00036 
00037 #ifndef VIGRA_METAPROGRAMMING_HXX
00038 #define VIGRA_METAPROGRAMMING_HXX
00039 
00040 #include "config.hxx"
00041 #include <limits.h>
00042 
00043 namespace vigra {
00044 
00045 template <int N>
00046 class MetaInt
00047 {
00048   public:
00049     enum { value = N };
00050 };
00051 
00052 struct VigraTrueType
00053 {
00054    enum { asBool = true };
00055 };
00056 
00057 struct VigraFalseType
00058 {
00059     enum { asBool = false };
00060 };
00061 
00062 /**  \addtogroup MultiArrayTags Multi-dimensional Array Tags
00063       Meta-programming tags to mark array's as strided or unstrided.
00064 */
00065 
00066 //@{
00067 
00068 /********************************************************/
00069 /*                                                      */
00070 /*                   StridedArrayTag                    */
00071 /*                                                      */
00072 /********************************************************/
00073 
00074 /** tag for marking a MultiArray strided.
00075 
00076 <b>\#include</b>
00077 <<a href="multi__array_8hxx-source.html">vigra/multi_array.hxx</a>>
00078 
00079 Namespace: vigra
00080 */
00081 struct StridedArrayTag {};
00082 
00083 /********************************************************/
00084 /*                                                      */
00085 /*                  UnstridedArrayTag                   */
00086 /*                                                      */
00087 /********************************************************/
00088 
00089 /** tag for marking a MultiArray unstrided.
00090 
00091 <b>\#include</b>
00092 <<a href="multi__array_8hxx-source.html">vigra/multi_array.hxx</a>>
00093 
00094 Namespace: vigra
00095 */
00096 struct UnstridedArrayTag {};
00097 
00098 template<class T>
00099 class TypeTraits
00100 {
00101   public:
00102     typedef VigraFalseType isConst;
00103     typedef VigraFalseType isPOD;
00104     typedef VigraFalseType isBuiltinType;
00105 };
00106 
00107 #ifndef NO_PARTIAL_TEMPLATE_SPECIALIZATION
00108 
00109 template<class T>
00110 class TypeTraits<T const>
00111 : public TypeTraits<T>
00112 {
00113   public:
00114     typedef VigraTrueType isConst;
00115 };
00116 
00117 template<class T> 
00118 class TypeTraits<T *>
00119 {
00120   public:
00121     typedef VigraFalseType isConst;
00122     typedef VigraTrueType isPOD;
00123     typedef VigraTrueType isBuiltinType;
00124 };
00125 
00126 template<class T> 
00127 class TypeTraits<T const *>
00128 {
00129   public:
00130     typedef VigraFalseType isConst;
00131     typedef VigraTrueType isPOD;
00132     typedef VigraTrueType isBuiltinType;
00133 };
00134 
00135 #endif // NO_PARTIAL_TEMPLATE_SPECIALIZATION
00136 
00137 namespace detail {
00138 
00139 template <int size>
00140 struct SizeToType;
00141 
00142 } // namespace detail 
00143 
00144 #define VIGRA_TYPE_TRAITS(type, size) \
00145 template<> \
00146 class TypeTraits<type> \
00147 { \
00148   public: \
00149     typedef VigraFalseType isConst; \
00150     typedef VigraTrueType isPOD; \
00151     typedef VigraTrueType isBuiltinType; \
00152     typedef char TypeToSize[size]; \
00153 }; \
00154  \
00155 namespace detail { \
00156   TypeTraits<type>::TypeToSize * typeToSize(type); \
00157   \
00158   template <> \
00159   struct SizeToType<size> \
00160   { \
00161       typedef type result; \
00162   }; \
00163 } 
00164 
00165 VIGRA_TYPE_TRAITS(char, 1)
00166 VIGRA_TYPE_TRAITS(signed char, 2)
00167 VIGRA_TYPE_TRAITS(unsigned char, 3)
00168 VIGRA_TYPE_TRAITS(short, 4)
00169 VIGRA_TYPE_TRAITS(unsigned short, 5)
00170 VIGRA_TYPE_TRAITS(int, 6)
00171 VIGRA_TYPE_TRAITS(unsigned int, 7)
00172 VIGRA_TYPE_TRAITS(long, 8)
00173 VIGRA_TYPE_TRAITS(unsigned long, 9)
00174 VIGRA_TYPE_TRAITS(float, 10)
00175 VIGRA_TYPE_TRAITS(double, 11)
00176 VIGRA_TYPE_TRAITS(long double, 12)
00177 #ifdef LLONG_MAX
00178 VIGRA_TYPE_TRAITS(long long, 13)
00179 VIGRA_TYPE_TRAITS(unsigned long long, 14)
00180 #endif
00181 
00182 #undef VIGRA_TYPE_TRAITS
00183 
00184 //@}
00185 
00186 template <class A>
00187 struct Not;
00188 
00189 template <>
00190 struct Not<VigraTrueType>
00191 {
00192     typedef VigraFalseType result;
00193     static const bool boolResult = false;
00194 };
00195 
00196 template <>
00197 struct Not<VigraFalseType>
00198 {
00199     typedef VigraTrueType result;
00200     static const bool boolResult = true;
00201 };
00202 
00203 template <class L, class R>
00204 struct And;
00205 
00206 template <>
00207 struct And<VigraFalseType, VigraFalseType>
00208 {
00209     typedef VigraFalseType result;
00210     static const bool boolResult = false;
00211 };
00212 
00213 template <>
00214 struct And<VigraFalseType, VigraTrueType>
00215 {
00216     typedef VigraFalseType result;
00217     static const bool boolResult = false;
00218 };
00219 
00220 template <>
00221 struct And<VigraTrueType, VigraFalseType>
00222 {
00223     typedef VigraFalseType result;
00224     static const bool boolResult = false;
00225 };
00226 
00227 template <>
00228 struct And<VigraTrueType, VigraTrueType>
00229 {
00230     typedef VigraTrueType result;
00231     static const bool boolResult = true;
00232 };
00233 
00234 template <class L, class R>
00235 struct Or;
00236 
00237 template <>
00238 struct Or<VigraFalseType, VigraFalseType>
00239 {
00240     typedef VigraFalseType result;
00241     static const bool boolResult = false;
00242 };
00243 
00244 template <>
00245 struct Or<VigraTrueType, VigraFalseType>
00246 {
00247     typedef VigraTrueType result;
00248     static const bool boolResult = true;
00249 };
00250 
00251 template <>
00252 struct Or<VigraFalseType, VigraTrueType>
00253 {
00254     typedef VigraTrueType result;
00255     static const bool boolResult = true;
00256 };
00257 
00258 template <>
00259 struct Or<VigraTrueType, VigraTrueType>
00260 {
00261     typedef VigraTrueType result;
00262     static const bool boolResult = true;
00263 };
00264 
00265 #ifndef NO_PARTIAL_TEMPLATE_SPECIALIZATION
00266 
00267 template <class PREDICATE, class TRUECASE, class FALSECASE>
00268 struct If;
00269 
00270 template <class TRUECASE, class FALSECASE>
00271 struct If<VigraTrueType, TRUECASE, FALSECASE>
00272 {
00273     typedef TRUECASE type;
00274 };
00275 
00276 template <class TRUECASE, class FALSECASE>
00277 struct If<VigraFalseType, TRUECASE, FALSECASE>
00278 {
00279     typedef FALSECASE type;
00280 };
00281 
00282 template <bool PREDICATE, class TRUECASE, class FALSECASE>
00283 struct IfBool;
00284 
00285 template <class TRUECASE, class FALSECASE>
00286 struct IfBool<true, TRUECASE, FALSECASE>
00287 {
00288     typedef TRUECASE type;
00289 };
00290 
00291 template <class TRUECASE, class FALSECASE>
00292 struct IfBool<false, TRUECASE, FALSECASE>
00293 {
00294     typedef FALSECASE type;
00295 };
00296 
00297 template <class L, class R>
00298 struct IsSameType
00299 {
00300     typedef VigraFalseType result;
00301     static const bool boolResult = false;
00302 };
00303 
00304 template <class T>
00305 struct IsSameType<T, T>
00306 {
00307     typedef VigraTrueType result;
00308     static const bool boolResult = true;
00309 };
00310 
00311 template <class DERIVED, class BASE>
00312 struct IsDerivedFrom
00313 {
00314     typedef char falseResult[1];
00315     typedef char trueResult[2];
00316     
00317     static falseResult * testIsDerivedFrom(...);
00318     static trueResult * testIsDerivedFrom(BASE const *);
00319     
00320     enum { resultSize = sizeof(*testIsDerivedFrom((DERIVED const *)0)) };
00321     
00322     static const bool boolResult = (resultSize == 2);
00323     typedef typename 
00324         IfBool<boolResult, VigraTrueType, VigraFalseType>::type
00325         result;
00326 };
00327 
00328 #endif // NO_PARTIAL_TEMPLATE_SPECIALIZATION
00329 
00330 } // namespace vigra
00331 
00332 #endif /* VIGRA_METAPROGRAMMING_HXX */

© Ullrich Köthe (ullrich.koethe@iwr.uni-heidelberg.de)
Heidelberg Collaboratory for Image Processing, University of Heidelberg, Germany

html generated using doxygen and Python
VIGRA 1.6.0 (5 Nov 2009)