| 1 |
|
/*========================================================================= |
| 2 |
|
|
| 3 |
|
Program: Insight Segmentation & Registration Toolkit |
| 4 |
|
Module: $RCSfile: itkConceptChecking.h.html,v $ |
| 5 |
|
Language: C++ |
| 6 |
|
Date: $Date: 2006/01/17 19:15:34 $ |
| 7 |
|
Version: $Revision: 1.4 $ |
| 8 |
|
|
| 9 |
|
Copyright (c) Insight Software Consortium. All rights reserved. |
| 10 |
|
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details. |
| 11 |
|
|
| 12 |
|
Portions of this code are covered under the VTK copyright. |
| 13 |
|
See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm for details. |
| 14 |
|
|
| 15 |
|
This software is distributed WITHOUT ANY WARRANTY; without even |
| 16 |
|
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 17 |
IND |
*****PURPOSE. See the above copyright notices for more information. |
| 18 |
|
|
| 19 |
|
=========================================================================*/ |
| 20 |
DEF |
#ifndef _itkConceptChecking_h |
| 21 |
DEF |
#define _itkConceptChecking_h |
| 22 |
|
|
| 23 |
|
/** Choose a concept checking implementation based on compiler abilities. */ |
| 24 |
|
#ifndef ITK_CONCEPT_NO_CHECKING |
| 25 |
|
# if defined(_MSC_VER) && !defined(__ICL) |
| 26 |
|
# define ITK_CONCEPT_IMPLEMENTATION_VTABLE |
| 27 |
|
# elif defined(__BORLANDC__) && (__BORLANDC__ <= 0x551) |
| 28 |
|
# define ITK_CONCEPT_IMPLEMENTATION_VTABLE |
| 29 |
|
# elif defined(__MWERKS__) && (__MWERKS__ <= 0x3002) |
| 30 |
|
# define ITK_CONCEPT_IMPLEMENTATION_VTABLE |
| 31 |
|
# elif defined(__SUNPRO_CC) |
| 32 |
|
# define ITK_CONCEPT_IMPLEMENTATION_VTABLE |
| 33 |
|
# else |
| 34 |
|
# define ITK_CONCEPT_IMPLEMENTATION_STANDARD |
| 35 |
|
# endif |
| 36 |
|
#endif |
| 37 |
|
|
| 38 |
|
/** Define the concept checking implementation chosen above. */ |
| 39 |
|
#if defined(ITK_CONCEPT_IMPLEMENTATION_STANDARD) |
| 40 |
|
|
| 41 |
|
/** |
| 42 |
|
* Standard instantiation-time concept check. No run-time overhead |
| 43 |
|
* introduced. This implementation is based on "Concept Checking: |
| 44 |
|
* Binding Parametric Polymorphism in C++" by Jeremy Siek and Andrew |
| 45 |
|
* Lumsdaine, University of Notre Dame. |
| 46 |
|
*/ |
| 47 |
|
# define itkConceptConstraintsMacro() \ |
| 48 |
|
template <void (Constraints::*)()> struct Enforcer {}; \ |
| 49 |
IND |
****typedef Enforcer<&Constraints::constraints> EnforcerInstantiation |
| 50 |
|
# define itkConceptMacro(name, concept) enum { name = sizeof concept } |
| 51 |
|
|
| 52 |
|
#elif defined(ITK_CONCEPT_IMPLEMENTATION_VTABLE) |
| 53 |
|
|
| 54 |
|
/** |
| 55 |
|
* Alternate implementation for some compilers. This introduces no |
| 56 |
|
* run-time overhead. The "vtable" approach was invented for this |
| 57 |
|
* project by Brad King at Kitware. |
| 58 |
|
*/ |
| 59 |
|
# define itkConceptConstraintsMacro() \ |
| 60 |
IND |
****virtual void Enforcer() { &Constraints::constraints; } |
| 61 |
|
# define itkConceptMacro(name, concept) enum { name = sizeof concept } |
| 62 |
|
|
| 63 |
|
#elif defined(ITK_CONCEPT_IMPLEMENTATION_CALL) |
| 64 |
|
|
| 65 |
|
/** Not implemented. */ |
| 66 |
|
# define itkConceptConstraintsMacro() |
| 67 |
|
# define itkConceptMacro(name, concept) enum { name = 0 } |
| 68 |
|
|
| 69 |
|
#else |
| 70 |
|
|
| 71 |
|
/** Disable concept checking. */ |
| 72 |
|
# define itkConceptConstraintsMacro() |
| 73 |
|
# define itkConceptMacro(name, concept) enum { name = 0 } |
| 74 |
|
|
| 75 |
|
#endif |
| 76 |
|
|
| 77 |
|
namespace itk |
| 78 |
|
{ |
| 79 |
|
|
| 80 |
|
/** All concept class definitions are contained in the "itk::Concept" |
| 81 |
IND |
****namespace. */ |
| 82 |
|
namespace Concept |
| 83 |
|
{ |
| 84 |
|
|
| 85 |
|
/** |
| 86 |
|
* Some concept implementation details are adapted from the BOOST C++ |
| 87 |
|
* libraries (www.boost.org). These are marked with "(BOOST)" in the |
| 88 |
|
* corresponding comment. |
| 89 |
|
*/ |
| 90 |
|
|
| 91 |
|
/** Namespace containing concept check implementation details. */ |
| 92 |
|
namespace Detail |
| 93 |
|
{ |
| 94 |
|
|
| 95 |
|
template <typename T> struct UniqueType {}; |
| 96 |
|
template <int> struct UniqueType_int {}; |
| 97 |
|
template <unsigned int> struct UniqueType_unsigned_int {}; |
| 98 |
|
template <bool> struct UniqueType_bool {}; |
| 99 |
|
|
| 100 |
|
|
| 101 |
|
/** |
| 102 |
|
* Concept checks may require a variable to be declared but not used. |
| 103 |
|
* This function can be called with the variable to prevent the compiler |
| 104 |
|
* warning. (BOOST) |
| 105 |
|
*/ |
| 106 |
|
template <typename T> inline void IgnoreUnusedVariable(T) {} |
| 107 |
|
|
| 108 |
|
/** |
| 109 |
|
* Concept checks may require that an expression be convertible to bool. |
| 110 |
|
* Passing the expression to this function will enforce this requirement. |
| 111 |
|
* (BOOST) |
| 112 |
|
*/ |
| 113 |
|
template <class T> |
| 114 |
|
void RequireBooleanExpression(const T& t) |
| 115 |
|
{ |
| 116 |
|
bool x = t; |
| 117 |
|
IgnoreUnusedVariable(x); |
| 118 |
|
} |
| 119 |
|
|
| 120 |
|
} // namespace Detail |
| 121 |
|
|
| 122 |
|
|
| 123 |
|
/** Concept requiring T to have a default constructor. (BOOST) */ |
| 124 |
|
template <typename T> |
| 125 |
|
struct DefaultConstructible |
| 126 |
|
{ |
| 127 |
|
struct Constraints |
| 128 |
IND |
**{ |
| 129 |
|
void constraints() |
| 130 |
|
{ |
| 131 |
|
T a; |
| 132 |
|
Detail::IgnoreUnusedVariable(a); |
| 133 |
|
} |
| 134 |
IND |
**}; |
| 135 |
|
|
| 136 |
|
itkConceptConstraintsMacro(); |
| 137 |
|
}; |
| 138 |
|
|
| 139 |
|
/** Concept requiring T to have a copy constructor. (BOOST) */ |
| 140 |
|
template <typename T> |
| 141 |
|
struct CopyConstructible |
| 142 |
|
{ |
| 143 |
|
struct Constraints |
| 144 |
IND |
**{ |
| 145 |
|
void constraints() |
| 146 |
|
{ |
| 147 |
|
T a(b); |
| 148 |
|
T* p = &a; |
| 149 |
|
const_constraints(a); |
| 150 |
|
Detail::IgnoreUnusedVariable(p); |
| 151 |
|
} |
| 152 |
|
void const_constraints(const T& a) |
| 153 |
|
{ |
| 154 |
|
T c(a); |
| 155 |
|
const T* p = &a; |
| 156 |
|
Detail::IgnoreUnusedVariable(c); |
| 157 |
|
Detail::IgnoreUnusedVariable(p); |
| 158 |
|
} |
| 159 |
|
T b; |
| 160 |
IND |
**}; |
| 161 |
|
|
| 162 |
|
itkConceptConstraintsMacro(); |
| 163 |
|
}; |
| 164 |
|
|
| 165 |
|
/** Concept requiring T1 to be convertible to T2. (BOOST) */ |
| 166 |
|
template <typename T1, typename T2> |
| 167 |
|
struct Convertible |
| 168 |
|
{ |
| 169 |
|
struct Constraints |
| 170 |
IND |
**{ |
| 171 |
|
void constraints() |
| 172 |
|
{ |
| 173 |
|
T2 b = a; |
| 174 |
|
Detail::IgnoreUnusedVariable(b); |
| 175 |
|
} |
| 176 |
|
T1 a; |
| 177 |
IND |
**}; |
| 178 |
|
itkConceptConstraintsMacro(); |
| 179 |
|
}; |
| 180 |
|
|
| 181 |
|
/** Concept requiring T to have operator =. (BOOST) */ |
| 182 |
|
template <typename T> |
| 183 |
|
struct Assignable |
| 184 |
|
{ |
| 185 |
|
struct Constraints |
| 186 |
IND |
**{ |
| 187 |
|
void constraints() |
| 188 |
|
{ |
| 189 |
|
a = a; |
| 190 |
|
const_constraints(a); |
| 191 |
|
} |
| 192 |
|
void const_constraints(const T& b) |
| 193 |
|
{ |
| 194 |
|
a = b; |
| 195 |
|
} |
| 196 |
|
T a; |
| 197 |
IND |
**}; |
| 198 |
|
|
| 199 |
|
itkConceptConstraintsMacro(); |
| 200 |
|
}; |
| 201 |
|
|
| 202 |
|
/** Concept requiring T to have operator <. (BOOST) */ |
| 203 |
|
template <typename T> |
| 204 |
|
struct LessThanComparable |
| 205 |
|
{ |
| 206 |
|
struct Constraints |
| 207 |
IND |
**{ |
| 208 |
|
void constraints() |
| 209 |
|
{ |
| 210 |
|
Detail::RequireBooleanExpression(a < b); |
| 211 |
|
} |
| 212 |
|
T a, b; |
| 213 |
|
}; |
| 214 |
|
|
| 215 |
|
itkConceptConstraintsMacro(); |
| 216 |
|
}; |
| 217 |
|
|
| 218 |
|
/** Concept requiring T to have operators == and != (BOOST) */ |
| 219 |
|
template <typename T> |
| 220 |
|
struct EqualityComparable |
| 221 |
|
{ |
| 222 |
|
struct Constraints |
| 223 |
IND |
**{ |
| 224 |
|
void constraints() |
| 225 |
|
{ |
| 226 |
|
Detail::RequireBooleanExpression(a == b); |
| 227 |
|
Detail::RequireBooleanExpression(a != b); |
| 228 |
|
} |
| 229 |
|
T a, b; |
| 230 |
IND |
**}; |
| 231 |
|
|
| 232 |
|
itkConceptConstraintsMacro(); |
| 233 |
|
}; |
| 234 |
|
|
| 235 |
|
/** Concept requiring T to have operators <, >, <=, >=, ==, !=. (BOOST) */ |
| 236 |
|
template <typename T> |
| 237 |
|
struct Comparable |
| 238 |
|
{ |
| 239 |
|
struct Constraints |
| 240 |
IND |
**{ |
| 241 |
|
void constraints() |
| 242 |
|
{ |
| 243 |
|
Detail::RequireBooleanExpression(a < b); |
| 244 |
|
Detail::RequireBooleanExpression(a > b); |
| 245 |
|
Detail::RequireBooleanExpression(a <= b); |
| 246 |
|
Detail::RequireBooleanExpression(a >= b); |
| 247 |
|
Detail::RequireBooleanExpression(a == b); |
| 248 |
|
Detail::RequireBooleanExpression(a != b); |
| 249 |
|
} |
| 250 |
|
T a, b; |
| 251 |
IND |
**}; |
| 252 |
|
|
| 253 |
|
itkConceptConstraintsMacro(); |
| 254 |
|
}; |
| 255 |
|
|
| 256 |
|
/** Concept requiring T to have operators +, -, +=, -=. */ |
| 257 |
|
template <typename T> |
| 258 |
|
struct AdditiveOperators |
| 259 |
|
{ |
| 260 |
|
struct Constraints |
| 261 |
IND |
**{ |
| 262 |
|
void constraints() |
| 263 |
|
{ |
| 264 |
|
a = b + b; |
| 265 |
|
a = b - b; |
| 266 |
|
a += b; |
| 267 |
|
a -= b; |
| 268 |
|
const_constraints(b); |
| 269 |
|
} |
| 270 |
|
void const_constraints(const T& c) |
| 271 |
|
{ |
| 272 |
|
a = c + c; |
| 273 |
|
a = c - c; |
| 274 |
|
a += c; |
| 275 |
|
a -= c; |
| 276 |
|
} |
| 277 |
|
T a, b; |
| 278 |
IND |
**}; |
| 279 |
|
|
| 280 |
|
itkConceptConstraintsMacro(); |
| 281 |
|
}; |
| 282 |
|
|
| 283 |
|
/** Concept requiring T to have operators *, /, *=, /=. */ |
| 284 |
|
template <typename T> |
| 285 |
|
struct MultiplicativeOperators |
| 286 |
|
{ |
| 287 |
|
struct Constraints |
| 288 |
IND |
**{ |
| 289 |
|
void constraints() |
| 290 |
|
{ |
| 291 |
|
a = b * b; |
| 292 |
|
a = b / b; |
| 293 |
|
a *= b; |
| 294 |
|
a /= b; |
| 295 |
|
const_constraints(b); |
| 296 |
|
} |
| 297 |
|
void const_constraints(const T& c) |
| 298 |
|
{ |
| 299 |
|
a = c * c; |
| 300 |
|
a = c / c; |
| 301 |
|
a *= c; |
| 302 |
|
a /= c; |
| 303 |
|
} |
| 304 |
|
T a, b; |
| 305 |
IND |
**}; |
| 306 |
|
|
| 307 |
|
itkConceptConstraintsMacro(); |
| 308 |
|
}; |
| 309 |
|
|
| 310 |
|
/** Concept requiring T to be signed. */ |
| 311 |
|
template <typename T> |
| 312 |
|
struct Signed |
| 313 |
|
{ |
| 314 |
|
typedef Signed Self; |
| 315 |
|
itkStaticConstMacro(IsSigned, bool, NumericTraits<T>::is_signed); |
| 316 |
|
struct Constraints |
| 317 |
IND |
**{ |
| 318 |
|
typedef Detail::UniqueType_bool<true> TrueT; |
| 319 |
TDA |
typedef Detail::UniqueType_bool<itkGetStaticConstMacro(IsSigned)> SignedT; |
| 320 |
|
void constraints() |
| 321 |
|
{ |
| 322 |
IND |
********SignedT a = TrueT(); |
| 323 |
IND |
********Detail::IgnoreUnusedVariable(a); |
| 324 |
|
} |
| 325 |
IND |
**}; |
| 326 |
|
|
| 327 |
|
itkConceptConstraintsMacro(); |
| 328 |
|
}; |
| 329 |
|
|
| 330 |
|
/** Concept requiring T1 and T2 to be the same type. */ |
| 331 |
|
template <typename T1, typename T2> |
| 332 |
|
struct SameType |
| 333 |
|
{ |
| 334 |
|
struct Constraints |
| 335 |
IND |
**{ |
| 336 |
|
void constraints() |
| 337 |
|
{ |
| 338 |
IND |
********Detail::UniqueType<T1> a = Detail::UniqueType<T2>(); |
| 339 |
IND |
********Detail::IgnoreUnusedVariable(a); |
| 340 |
|
} |
| 341 |
IND |
**}; |
| 342 |
|
itkConceptConstraintsMacro(); |
| 343 |
|
}; |
| 344 |
|
|
| 345 |
|
/** Concept requiring D1 and D2 to be the same dimension. */ |
| 346 |
|
template <unsigned int D1, unsigned int D2> |
| 347 |
|
struct SameDimension |
| 348 |
|
{ |
| 349 |
|
struct Constraints |
| 350 |
IND |
**{ |
| 351 |
|
typedef Detail::UniqueType_unsigned_int<D1> DT1; |
| 352 |
|
typedef Detail::UniqueType_unsigned_int<D2> DT2; |
| 353 |
|
void constraints() |
| 354 |
|
{ |
| 355 |
IND |
********DT1 a = DT2(); |
| 356 |
IND |
********Detail::IgnoreUnusedVariable(a); |
| 357 |
|
} |
| 358 |
IND |
**}; |
| 359 |
|
itkConceptConstraintsMacro(); |
| 360 |
|
}; |
| 361 |
|
|
| 362 |
|
} // namespace Concept |
| 363 |
|
|
| 364 |
|
} // namespace itk |
| 365 |
|
|
| 366 |
|
#endif |
| 367 |
|
|