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