ITK  4.8.0
Insight Segmentation and Registration Toolkit
itkConceptChecking.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 /*=========================================================================
19  *
20  * Portions of this file are subject to the VTK Toolkit Version 3 copyright.
21  *
22  * Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
23  *
24  * For complete copyright, license and disclaimer of warranty information
25  * please refer to the NOTICE file at the top of the ITK source tree.
26  *
27  *=========================================================================*/
28 #ifndef itkConceptChecking_h
29 #define itkConceptChecking_h
30 
31 #include "itkPixelTraits.h"
32 #include "itkNumericTraits.h"
33 #include <iostream>
34 
36 #ifndef ITK_CONCEPT_NO_CHECKING
37 #if defined( _MSC_VER ) && !defined( __ICL )
38 #define ITK_CONCEPT_IMPLEMENTATION_VTABLE
39 //TST_RMV_20100730 #elif defined(__SUNPRO_CC)
40 //TST_RMV_20100730 #define ITK_CONCEPT_IMPLEMENTATION_VTABLE
41 #else
42 #define ITK_CONCEPT_IMPLEMENTATION_STANDARD
43 #endif
44 #endif
45 
47 #if defined( ITK_CONCEPT_IMPLEMENTATION_STANDARD )
48 
56 // Leave ()'s off the sizeof to force the caller to pass them in the
57 // concept argument of the itkConceptMacro. This is necessary because
58 // the argument may contain commas.
59 #define itkConceptConstraintsMacro() \
60  template< void (Constraints::*) ( ) > \
61  struct Enforcer {}; \
62  typedef Enforcer< & Constraints::constraints > EnforcerInstantiation
63 #define itkConceptMacro(name, concept) enum { name = sizeof concept }
64 
65 #elif defined( ITK_CONCEPT_IMPLEMENTATION_VTABLE )
66 
72 #define itkConceptConstraintsMacro() \
73  virtual void Enforcer() { &Constraints::constraints; }
74 #define itkConceptMacro(name, concept) enum { name = sizeof concept }
75 
76 
77 #elif defined( ITK_CONCEPT_IMPLEMENTATION_CALL )
78 
80 #define itkConceptConstraintsMacro()
81 #define itkConceptMacro(name, concept) enum { name = 0 }
82 
83 #else
84 
86 #define itkConceptConstraintsMacro()
87 #define itkConceptMacro(name, concept) enum { name = 0 }
88 
89 #endif
90 
91 namespace itk
92 {
95 namespace Concept
96 {
97 
105 namespace Detail
106 {
107 template< typename T >
108 struct UniqueType {};
109 template< int >
110 struct UniqueType_int {};
111 template< unsigned int >
113 template< bool >
114 struct UniqueType_bool {};
115 
121 template< typename T >
122 inline void IgnoreUnusedVariable(T) {}
123 
129 template< typename T >
130 void RequireBooleanExpression(const T & t)
131 {
132  bool x = t;
133 
135 }
136 } // namespace Detail
137 
139 template< typename T >
141  struct Constraints {
142  void constraints()
143  {
144  T a;
145 
147  }
148  };
149 
151 };
152 
154 template< typename T >
156  struct Constraints {
157  void constraints()
158  {
159  T a(b);
160  T *p = &a;
162 
165  }
166 
167  void const_constraints(const T & a)
168  {
169  T c(a);
170  const T *p = &a;
171 
174  }
175 
176  T b;
177  };
178 
180 };
181 
183 template< typename T1, typename T2 >
184 struct Convertible {
185  struct Constraints {
186  void constraints()
187  {
188  T2 b = static_cast< T2 >( a );
189 
191  }
192 
193  T1 a;
194  };
196 };
197 
199 template< typename T >
200 struct Assignable {
201  struct Constraints {
202  void constraints()
203  {
204  a = a;
206  }
208 
209  void const_constraints(const T & b)
210  {
211  a = b;
212  }
213 
214  T a;
215  };
216 
218 };
219 
222 template< typename T1, typename T2 = T1 >
224  struct Constraints {
225  void constraints()
226  {
229  }
231 
232  T1 a;
233  T2 b;
234  };
235 
237 };
238 
241 template< typename T1, typename T2 = T1 >
243  struct Constraints {
244  void constraints()
245  {
248  }
250 
251  T1 a;
252  T2 b;
253  };
254 
256 };
257 
260 template< typename T1, typename T2 = T1 >
262  struct Constraints {
263  void constraints()
264  {
267  }
269 
270  T1 a;
271  T2 b;
272  };
273 
275 };
276 
279 template< typename T1, typename T2 = T1 >
280 struct Comparable {
281  struct Constraints {
282  void constraints()
283  {
290  }
292 
293  T1 a;
294  T2 b;
295  };
296 
298 };
299 
302 template< typename T1, typename T2 = T1, typename T3 = T1 >
304  struct Constraints {
305  void constraints()
306  {
307  a = static_cast< T3 >( b + c );
308  a = static_cast< T3 >( b - c );
310  }
312 
313  void const_constraints(const T1 & d, const T2 & e)
314  {
315  a = static_cast< T3 >( d + e );
316  a = static_cast< T3 >( d - e );
317  }
318 
319  T3 a;
320  T1 b;
321  T2 c;
322  };
323 
325 };
326 
327 
330 template< typename T1, typename T2 = T1>
332  struct Constraints {
333  void constraints()
334  {
335  a += c;
336  a -= c;
338  }
340 
341  void const_constraints(const T1 & d)
342  {
343  a += d;
344  a -= d;
345  }
346 
347  T2 a;
348  T1 c;
349  };
350 
352 };
353 
355 template< typename T1, typename T2 = T1, typename T3 = T1 >
357  struct Constraints {
358  void constraints()
359  {
360  a = static_cast< T3 >( b * c );
362  }
364 
365  void const_constraints(const T1 & d, const T2 & e)
366  {
367  a = static_cast< T3 >( d * e );
368  }
369 
370  T3 a;
371  T1 b;
372  T2 c;
373  };
375 };
376 
378 template< typename T1, typename T2 = T1 >
380  struct Constraints {
381  void constraints()
382  {
383  a *= b;
385  }
387 
388  void const_constraints(const T1 & d)
389  {
390  a *= d;
391  }
392 
393  T2 a;
394  T1 b;
395  };
396 
398 };
399 
401 template< typename T1, typename T2 = T1, typename T3 = T1 >
403  struct Constraints {
404  void constraints()
405  {
406  a = static_cast< T3 >( b / c );
408  }
410 
411  void const_constraints(const T1 & d, const T2 & e)
412  {
413  a = static_cast< T3 >( d / e );
414  }
415 
416  T3 a;
417  T1 b;
418  T2 c;
419  };
420 
422 };
423 
424 
426 template< typename T1, typename T2 = T1 >
428  struct Constraints {
429  void constraints()
430  {
431  a /= c;
433  }
435 
436  void const_constraints(const T1 & d)
437  {
438  a /= d;
439  }
440 
441  T1 c;
442  T2 a;
443  };
444 
446 };
447 
448 
451 template< typename T1, typename T2 = T1, typename T3 = T1 >
453  struct Constraints {
454  void constraints()
455  {
456  a = static_cast< T3 >( b & c );
457  a = static_cast< T3 >( b | c );
458  a = static_cast< T3 >( b ^ c );
459  a &= static_cast< T3 >( c );
460  a |= static_cast< T3 >( c );
461  a ^= static_cast< T3 >( c );
463  }
465 
466  void const_constraints(const T1 & d, const T2 & e)
467  {
468  a = static_cast< T3 >( d & e );
469  a = static_cast< T3 >( d | e );
470  a = static_cast< T3 >( d ^ e );
471  a &= static_cast< T3 >( e );
472  a |= static_cast< T3 >( e );
473  a ^= static_cast< T3 >( e );
474  }
475 
476  T3 a;
477  T1 b;
478  T2 c;
479  };
480 
482 };
483 
485 template< typename T1, typename T2 = T1, typename T3 = T1 >
487  struct Constraints {
488  void constraints()
489  {
490  a = static_cast< T3 >( b[c] );
492  }
494 
495  void const_constraints(const T1 & d, const T2 & e)
496  {
497  a = static_cast< T3 >( d[e] );
498  }
499 
500  T3 a;
501  T1 b;
502  T2 c;
503  };
504 
506 };
507 
509 template< typename T >
510 struct NotOperator {
511  struct Constraints {
512  void constraints()
513  {
514  a = !a;
515  }
516 
517  T a;
518  };
519 
521 };
522 
524 template< typename T >
526  struct Constraints {
527  void constraints()
528  {
529  a++;
530  a--;
531  ++a;
532  --a;
533  }
534 
535  T a;
536  };
537 
539 };
540 
542 template< typename T >
544  struct Constraints {
545  void constraints()
546  {
547  std::cout << a;
548  }
549 
550  T a;
551  };
552 
554 };
555 
557 template< typename T >
558 struct Signed {
559  typedef Signed Self;
560  itkStaticConstMacro(IsSigned, bool, NumericTraits< T >::is_signed);
561  struct Constraints {
564  void constraints()
565  {
566  SignedT a = TrueT();
568 
570  }
571  };
572 
574 };
575 
577 template< typename T1, typename T2 >
578 struct SameType {
579  struct Constraints {
580  void constraints()
581  {
584  }
585  };
587 };
589 
591 template< unsigned int D1, unsigned int D2 >
593  struct Constraints {
596  void constraints()
597  {
598  DT1 a = DT2();
600 
602  }
603  };
605 };
606 
608 template< typename T >
610  struct Constraints {
611  void constraints()
612  {
620  T a;
621  bool b;
623 
624  // Test these methods that take an instance of T to
625  // allow for types with variable length.
629 
636  }
637  };
638 
640 };
641 
643 template< typename T >
645  struct Constraints {
646  void constraints()
647  {
649  unsigned int a = PixelTraits< T >::Dimension;
651  }
652  };
654 
656 };
657 
659 template< typename T >
660 struct HasValueType {
661  struct Constraints {
662  void constraints()
663  {
665  }
666  };
668 
670 };
671 
673 template< typename T >
674 struct HasZero {
675  struct Constraints {
676  void constraints()
677  {
678  T a;
679 
682  }
683  };
684 
686 };
687 
689 template< typename T1, typename T2 >
691  struct Constraints {
692  void constraints()
693  {
695  }
696  };
698 
700 };
701 
703 template< unsigned int D1, unsigned int D2 >
705  struct Constraints {
708 
709  void f(Type1) {}
710  void f(Type2, int = 0) {}
711 
712  void constraints()
713  {
715  this->f(tt);
716  }
717  };
719 };
720 
722 template< unsigned int D1, unsigned int D2 >
724  struct Constraints {
728 
729  void f(Type1) {}
730  void f(Type2, int = 0) {}
731  void f(Type3, int = 0, int = 0) {}
732 
733  void constraints()
734  {
736  this->f(tt);
737  }
738  };
740 };
741 
743 template< typename T >
744 struct IsInteger {
745  typedef IsInteger Self;
746  itkStaticConstMacro(Integral, bool, NumericTraits< T >::is_integer);
747  struct Constraints {
750  void constraints()
751  {
752  IntegralT a = TrueT();
754 
756  }
757  };
758 
760 };
761 
762 
764 template< typename T >
767  itkStaticConstMacro(Unsigned, bool, !NumericTraits< T >::is_signed);
768  struct Constraints {
771  void constraints()
772  {
773  UnsignedT a = TrueT();
775 
777  }
778  };
779 
781 };
782 
783 
785 template< typename T >
786 struct IsNonInteger {
788  itkStaticConstMacro(NonIntegral, bool, NumericTraits< T >::is_integer);
789  struct Constraints {
792  void constraints()
793  {
794  NonIntegralT a = FalseT();
796 
798  }
799  };
800 
802 };
803 
805 template< typename T >
808  itkStaticConstMacro(Integral, bool, NumericTraits< T >::is_integer);
809  itkStaticConstMacro(IsExact, bool, NumericTraits< T >::is_exact);
810  struct Constraints {
814  void constraints()
815  {
816  IntegralT a = FalseT();
817  ExactT b = FalseT();
819 
822  }
823  };
824 
826 };
827 
829 template< typename T >
830 struct IsFixedPoint {
832  itkStaticConstMacro(Integral, bool, NumericTraits< T >::is_integer);
833  itkStaticConstMacro(IsExact, bool, NumericTraits< T >::is_exact);
834  struct Constraints {
839  void constraints()
840  {
841  IntegralT a = FalseT();
842  ExactT b = TrueT();
844 
847  }
848  };
849 
851 };
852 } // end namespace Concept
853 } // end namespace itk
854 
855 #endif
Detail::UniqueType_bool< itkGetStaticConstMacro(IsExact) > ExactT
Detail::UniqueType_bool< false > FalseT
Detail::UniqueType_unsigned_int< D1-1 > Type2
Detail::UniqueType_bool< itkGetStaticConstMacro(Integral) > IntegralT
Detail::UniqueType_unsigned_int< D1 > DT1
void const_constraints(const T1 &d, const T2 &e)
void const_constraints(const T1 &d, const T2 &e)
Detail::UniqueType_bool< true > TrueT
void const_constraints(const T1 &d, const T2 &e)
static const double e
The base of the natural logarithm or Euler&#39;s number
Definition: itkMath.h:45
static bool IsPositive(T val)
Traits for a pixel that define the dimension and component type.
void RequireBooleanExpression(const T &t)
Detail::UniqueType_bool< itkGetStaticConstMacro(Integral) > IntegralT
static bool IsNonnegative(T val)
static const bool IsSigned
static const bool Integral
Detail::UniqueType_bool< true > TrueT
Detail::UniqueType_unsigned_int< D2 > DT2
static bool IsNegative(T val)
void const_constraints(const T1 &d, const T2 &e)
Detail::UniqueType_bool< itkGetStaticConstMacro(IsExact) > ExactT
void const_constraints(const T1 &d, const T2 &e)
Detail::UniqueType_bool< itkGetStaticConstMacro(Unsigned) > UnsignedT
Detail::UniqueType_bool< false > FalseT
Detail::UniqueType_unsigned_int< D1 > Type1
Detail::UniqueType_bool< itkGetStaticConstMacro(NonIntegral) > NonIntegralT
static bool IsNonpositive(T val)
Detail::UniqueType_bool< true > TrueT
Detail::UniqueType_bool< false > FalseT
Detail::UniqueType_bool< itkGetStaticConstMacro(Integral) > IntegralT
Define additional traits for native types such as int or float.
Detail::UniqueType_bool< itkGetStaticConstMacro(IsSigned) > SignedT