ITK  4.3.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< class 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  {
613  typedef typename NumericTraits< T >::ValueType ValueType;
614  typedef typename NumericTraits< T >::PrintType PrintType;
615  typedef typename NumericTraits< T >::AbsType AbsType;
616  typedef typename NumericTraits< T >::AccumulateType AccumulateType;
617  typedef typename NumericTraits< T >::RealType RealType;
618  typedef typename NumericTraits< T >::ScalarRealType ScalarRealType;
619  typedef typename NumericTraits< T >::FloatType FloatType;
620  T a;
621  bool b;
622 
623  // Test these methods that take an instance of T to
624  // allow for types with variable length.
628 
635  }
636  };
637 
639 };
640 
642 template< typename T >
644  struct Constraints {
645  void constraints()
646  {
647  typedef typename PixelTraits< T >::ValueType ValueType;
648  unsigned int a = PixelTraits< T >::Dimension;
650  }
651  };
653 
655 };
656 
658 template< typename T >
659 struct HasValueType {
660  struct Constraints {
661  void constraints()
662  {
663  typedef typename T::ValueType ValueType;
664  }
665  };
666 
668 };
669 
671 template< typename T >
672 struct HasZero {
673  struct Constraints {
674  void constraints()
675  {
676  T a;
677 
680  }
681  };
682 
684 };
685 
687 template< typename T1, typename T2 >
689  struct Constraints {
690  void constraints()
691  {
692  typedef typename JoinTraits< T1, T2 >::ValueType ValueType;
693  }
694  };
695 
697 };
698 
700 template< unsigned int D1, unsigned int D2 >
702  struct Constraints {
705 
706  void f(Type1) {}
707  void f(Type2, int = 0) {}
708 
709  void constraints()
710  {
712  this->f(tt);
713  }
714  };
716 };
717 
719 template< unsigned int D1, unsigned int D2 >
721  struct Constraints {
725 
726  void f(Type1) {}
727  void f(Type2, int = 0) {}
728  void f(Type3, int = 0, int = 0) {}
729 
730  void constraints()
731  {
733  this->f(tt);
734  }
735  };
737 };
738 
740 template< typename T >
741 struct IsInteger {
742  typedef IsInteger Self;
743  itkStaticConstMacro(Integral, bool, NumericTraits< T >::is_integer);
744  struct Constraints {
747  void constraints()
748  {
749  IntegralT a = TrueT();
751 
753  }
754  };
755 
757 };
758 
760 template< typename T >
761 struct IsNonInteger {
763  itkStaticConstMacro(NonIntegral, bool, NumericTraits< T >::is_integer);
764  struct Constraints {
767  void constraints()
768  {
769  NonIntegralT a = FalseT();
771 
773  }
774  };
775 
777 };
778 
780 template< typename T >
783  itkStaticConstMacro(Integral, bool, NumericTraits< T >::is_integer);
784  itkStaticConstMacro(IsExact, bool, NumericTraits< T >::is_exact);
785  struct Constraints {
789  void constraints()
790  {
791  IntegralT a = FalseT();
792  ExactT b = FalseT();
794 
797  }
798  };
799 
801 };
802 
804 template< typename T >
805 struct IsFixedPoint {
807  itkStaticConstMacro(Integral, bool, NumericTraits< T >::is_integer);
808  itkStaticConstMacro(IsExact, bool, NumericTraits< T >::is_exact);
809  struct Constraints {
814  void constraints()
815  {
816  IntegralT a = FalseT();
817  ExactT b = TrueT();
819 
822  }
823  };
824 
826 };
827 } // end namespace Concept
828 } // end namespace itk
829 
830 #endif
831