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

details vigra/metaprogramming.hxx VIGRA

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.4.0, Dec 21 2005 )                                    */
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 /*        koethe@informatik.uni-hamburg.de          or                  */
00012 /*        vigra@kogs1.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 
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 #define VIGRA_TYPE_TRAITS(type) \
00138 template<> \
00139 class TypeTraits<type> \
00140 { \
00141   public: \
00142     typedef VigraFalseType isConst; \
00143     typedef VigraTrueType isPOD; \
00144     typedef VigraTrueType isBuiltinType; \
00145 };
00146 
00147 VIGRA_TYPE_TRAITS(char)
00148 VIGRA_TYPE_TRAITS(signed char)
00149 VIGRA_TYPE_TRAITS(unsigned char)
00150 VIGRA_TYPE_TRAITS(short)
00151 VIGRA_TYPE_TRAITS(unsigned short)
00152 VIGRA_TYPE_TRAITS(int)
00153 VIGRA_TYPE_TRAITS(unsigned int)
00154 VIGRA_TYPE_TRAITS(long)
00155 VIGRA_TYPE_TRAITS(unsigned long)
00156 VIGRA_TYPE_TRAITS(float)
00157 VIGRA_TYPE_TRAITS(double)
00158 VIGRA_TYPE_TRAITS(long double)
00159 
00160 #undef VIGRA_TYPE_TRAITS
00161 
00162 //@}
00163 
00164 template <class L, class R>
00165 struct And;
00166 
00167 template <>
00168 struct And<VigraFalseType, VigraFalseType>
00169 {
00170     typedef VigraFalseType result;
00171     static const bool boolResult = false;
00172 };
00173 
00174 template <>
00175 struct And<VigraFalseType, VigraTrueType>
00176 {
00177     typedef VigraFalseType result;
00178     static const bool boolResult = false;
00179 };
00180 
00181 template <>
00182 struct And<VigraTrueType, VigraFalseType>
00183 {
00184     typedef VigraFalseType result;
00185     static const bool boolResult = false;
00186 };
00187 
00188 template <>
00189 struct And<VigraTrueType, VigraTrueType>
00190 {
00191     typedef VigraTrueType result;
00192     static const bool boolResult = true;
00193 };
00194 
00195 template <class L, class R>
00196 struct Or;
00197 
00198 template <>
00199 struct Or<VigraFalseType, VigraFalseType>
00200 {
00201     typedef VigraFalseType result;
00202     static const bool boolResult = false;
00203 };
00204 
00205 template <>
00206 struct Or<VigraTrueType, VigraFalseType>
00207 {
00208     typedef VigraTrueType result;
00209     static const bool boolResult = true;
00210 };
00211 
00212 template <>
00213 struct Or<VigraFalseType, VigraTrueType>
00214 {
00215     typedef VigraTrueType result;
00216     static const bool boolResult = true;
00217 };
00218 
00219 template <>
00220 struct Or<VigraTrueType, VigraTrueType>
00221 {
00222     typedef VigraTrueType result;
00223     static const bool boolResult = true;
00224 };
00225 
00226 #ifndef NO_PARTIAL_TEMPLATE_SPECIALIZATION
00227 
00228 template <class PREDICATE, class TRUECASE, class FALSECASE>
00229 struct If;
00230 
00231 template <class TRUECASE, class FALSECASE>
00232 struct If<VigraTrueType, TRUECASE, FALSECASE>
00233 {
00234     typedef TRUECASE type;
00235 };
00236 
00237 template <class TRUECASE, class FALSECASE>
00238 struct If<VigraFalseType, TRUECASE, FALSECASE>
00239 {
00240     typedef FALSECASE type;
00241 };
00242 
00243 template <bool PREDICATE, class TRUECASE, class FALSECASE>
00244 struct IfBool;
00245 
00246 template <class TRUECASE, class FALSECASE>
00247 struct IfBool<true, TRUECASE, FALSECASE>
00248 {
00249     typedef TRUECASE type;
00250 };
00251 
00252 template <class TRUECASE, class FALSECASE>
00253 struct IfBool<false, TRUECASE, FALSECASE>
00254 {
00255     typedef FALSECASE type;
00256 };
00257 
00258 template <class L, class R>
00259 struct IsSameType
00260 {
00261     typedef VigraFalseType result;
00262     static const bool boolResult = false;
00263 };
00264 
00265 template <class T>
00266 struct IsSameType<T, T>
00267 {
00268     typedef VigraTrueType result;
00269     static const bool boolResult = true;
00270 };
00271 
00272 template <class DERIVED, class BASE>
00273 struct IsDerivedFrom
00274 {
00275     typedef char falseResult[1];
00276     typedef char trueResult[2];
00277     
00278     static falseResult * testIsDerivedFrom(...);
00279     static trueResult * testIsDerivedFrom(BASE const *);
00280     
00281     enum { resultSize = sizeof(*testIsDerivedFrom((DERIVED const *)0)) };
00282     
00283     static const bool boolResult = (resultSize == 2);
00284     typedef typename 
00285         IfBool<boolResult, VigraTrueType, VigraFalseType>::type
00286         result;
00287 };
00288 
00289 #endif // NO_PARTIAL_TEMPLATE_SPECIALIZATION
00290 
00291 } // namespace vigra
00292 
00293 #endif /* VIGRA_METAPROGRAMMING_HXX */

© Ullrich Köthe (koethe@informatik.uni-hamburg.de)
Cognitive Systems Group, University of Hamburg, Germany

html generated using doxygen and Python
VIGRA 1.4.0 (21 Dec 2005)