ITK  5.1.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  using EnforcerInstantiation = Enforcer< & Constraints::constraints >;
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  auto 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  T x = 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  {
265 CLANG_PRAGMA_PUSH
266 CLANG_SUPPRESS_Wfloat_equal
269 CLANG_PRAGMA_POP
270  }
272 
273  T1 a;
274  T2 b;
275  };
276 
278 };
279 
282 template< typename T1, typename T2 = T1 >
283 struct Comparable {
284  struct Constraints {
285  void constraints()
286  {
291 CLANG_PRAGMA_PUSH
292 CLANG_SUPPRESS_Wfloat_equal
295 CLANG_PRAGMA_POP
296  }
298 
299  T1 a;
300  T2 b;
301  };
302 
304 };
305 
308 template< typename T1, typename T2 = T1, typename T3 = T1 >
310  struct Constraints {
311  void constraints()
312  {
313  a = static_cast< T3 >( b + c );
314  a = static_cast< T3 >( b - c );
316  }
318 
319  void const_constraints(const T1 & d, const T2 & e)
320  {
321  a = static_cast< T3 >( d + e );
322  a = static_cast< T3 >( d - e );
323  }
324 
325  T3 a;
326  T1 b;
327  T2 c;
328  };
329 
331 };
332 
333 
336 template< typename T1, typename T2 = T1>
338  struct Constraints {
339  void constraints()
340  {
341  a += c;
342  a -= c;
344  }
346 
347  void const_constraints(const T1 & d)
348  {
349  a += d;
350  a -= d;
351  }
352 
353  T2 a;
354  T1 c;
355  };
356 
358 };
359 
361 template< typename T1, typename T2 = T1, typename T3 = T1 >
363  struct Constraints {
364  void constraints()
365  {
366  a = static_cast< T3 >( b * c );
368  }
370 
371  void const_constraints(const T1 & d, const T2 & e)
372  {
373  a = static_cast< T3 >( d * e );
374  }
375 
376  T3 a;
377  T1 b;
378  T2 c;
379  };
381 };
382 
384 template< typename T1, typename T2 = T1 >
386  struct Constraints {
387  void constraints()
388  {
389  a *= b;
391  }
393 
394  void const_constraints(const T1 & d)
395  {
396  a *= d;
397  }
398 
399  T2 a;
400  T1 b;
401  };
402 
404 };
405 
407 template< typename T1, typename T2 = T1, typename T3 = T1 >
409  struct Constraints {
410  void constraints()
411  {
412  a = static_cast< T3 >( b / c );
414  }
416 
417  void const_constraints(const T1 & d, const T2 & e)
418  {
419  a = static_cast< T3 >( d / e );
420  }
421 
422  T3 a;
423  T1 b;
424  T2 c;
425  };
426 
428 };
429 
430 
432 template< typename T1, typename T2 = T1 >
434  struct Constraints {
435  void constraints()
436  {
437  a /= c;
439  }
441 
442  void const_constraints(const T1 & d)
443  {
444  a /= d;
445  }
446 
447  T1 c;
448  T2 a;
449  };
450 
452 };
453 
454 
457 template< typename T1, typename T2 = T1, typename T3 = T1 >
459  struct Constraints {
460  void constraints()
461  {
462  a = static_cast< T3 >( b & c );
463  a = static_cast< T3 >( b | c );
464  a = static_cast< T3 >( b ^ c );
465  a &= static_cast< T3 >( c );
466  a |= static_cast< T3 >( c );
467  a ^= static_cast< T3 >( c );
469  }
471 
472  void const_constraints(const T1 & d, const T2 & e)
473  {
474  a = static_cast< T3 >( d & e );
475  a = static_cast< T3 >( d | e );
476  a = static_cast< T3 >( d ^ e );
477  a &= static_cast< T3 >( e );
478  a |= static_cast< T3 >( e );
479  a ^= static_cast< T3 >( e );
480  }
481 
482  T3 a;
483  T1 b;
484  T2 c;
485  };
486 
488 };
489 
491 template< typename T1, typename T2 = T1, typename T3 = T1 >
493  struct Constraints {
494  void constraints()
495  {
496  a = static_cast< T3 >( b[c] );
498  }
500 
501  void const_constraints(const T1 & d, const T2 & e)
502  {
503  a = static_cast< T3 >( d[e] );
504  }
505 
506  T3 a;
507  T1 b;
508  T2 c;
509  };
510 
512 };
513 
515 template< typename T >
516 struct NotOperator {
517  struct Constraints {
518  void constraints()
519  {
520  a = !a;
521  }
522 
523  T a;
524  };
525 
527 };
528 
530 template< typename T >
532  struct Constraints {
533  void constraints()
534  {
535  a++;
536  a--;
537  ++a;
538  --a;
539  }
540 
541  T a;
542  };
543 
545 };
546 
548 template< typename T >
550  struct Constraints {
551  void constraints()
552  {
553  std::cout << a;
554  }
555 
556  T a;
557  };
558 
560 };
561 
563 template< typename T >
564 struct Signed {
565  using Self = Signed;
566  static constexpr bool IsSigned = NumericTraits< T >::is_signed;
567  struct Constraints {
570  void constraints()
571  {
572  SignedT a = TrueT();
574 
576  }
577  };
578 
580 };
581 
583 template< typename T1, typename T2 >
584 struct SameType {
585  struct Constraints {
586  void constraints()
587  {
590  }
591  };
593 };
595 
597 template< unsigned int D1, unsigned int D2 >
599  struct Constraints {
602  void constraints()
603  {
604  DT1 a = DT2();
606 
608  }
609  };
611 };
612 
614 template< typename T >
616  struct Constraints {
617  void constraints()
618  {
626  T a;
627  bool b;
629 
630  // Test these methods that take an instance of T to
631  // allow for types with variable length.
635 
642  }
643  };
644 
646 };
647 
649 template< typename T >
651  struct Constraints {
652  void constraints()
653  {
655  unsigned int a = PixelTraits< T >::Dimension;
657  }
658  };
660 
662 };
663 
665 template< typename T >
666 struct HasValueType {
667  struct Constraints {
668  void constraints()
669  {
671  }
672  };
674 
676 };
677 
679 template< typename T >
680 struct HasZero {
681  struct Constraints {
682  void constraints()
683  {
684  T a;
685 
688  }
689  };
690 
692 };
693 
695 template< typename T1, typename T2 >
697  struct Constraints {
698  void constraints()
699  {
701  }
702  };
704 
706 };
707 
709 template< unsigned int D1, unsigned int D2 >
711  struct Constraints {
714 
715  void f(Type1) {}
716  void f(Type2, int = 0) {}
717 
718  void constraints()
719  {
721  this->f(tt);
722  }
723  };
725 };
726 
728 template< unsigned int D1, unsigned int D2 >
730  struct Constraints {
734 
735  void f(Type1) {}
736  void f(Type2, int = 0) {}
737  void f(Type3, int = 0, int = 0) {}
738 
739  void constraints()
740  {
742  this->f(tt);
743  }
744  };
746 };
747 
749 template< typename T >
750 struct IsInteger {
751  using Self = IsInteger;
752  static constexpr bool Integral = NumericTraits< T >::IsInteger;
753  struct Constraints {
756  void constraints()
757  {
758  IntegralT a = TrueT();
760 
762  }
763  };
764 
766 };
767 
768 
770 template< typename T >
773  static constexpr bool Unsigned = !NumericTraits< T >::is_signed;
774  struct Constraints {
777  void constraints()
778  {
779  UnsignedT a = TrueT();
781 
783  }
784  };
785 
787 };
788 
789 
791 template< typename T >
792 struct IsNonInteger {
795  struct Constraints {
798  void constraints()
799  {
800  NonIntegralT a = FalseT();
802 
804  }
805  };
806 
808 };
809 
811 template< typename T >
814  static constexpr bool Integral = NumericTraits< T >::IsInteger;
815  static constexpr bool IsExact = std::numeric_limits< typename NumericTraits< T >::ValueType >::is_exact;
816  struct Constraints {
820  void constraints()
821  {
822  IntegralT a = FalseT();
823  ExactT b = FalseT();
825 
828  }
829  };
830 
832 };
833 
835 template< typename T >
836 struct IsFixedPoint {
838  static constexpr bool Integral = NumericTraits< T >::IsInteger;
839  static constexpr bool IsExact = std::numeric_limits< typename NumericTraits< T >::ValueType >::is_exact;
840  struct Constraints {
845  void constraints()
846  {
847  IntegralT a = FalseT();
848  ExactT b = TrueT();
850 
853  }
854  };
855 
857 };
858 } // end namespace Concept
859 } // end namespace itk
860 
861 #endif
static constexpr bool Integral
void const_constraints(const T1 &d, const T2 &e)
Detail::UniqueType_unsigned_int< D2 > DT2
void const_constraints(const T1 &d, const T2 &e)
static constexpr bool IsExact
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkArray.h:26
static constexpr T NonpositiveMin()
Detail::UniqueType_bool< false > FalseT
void const_constraints(const T1 &d, const T2 &e)
static bool IsPositive(T val)
Traits for a pixel that define the dimension and component type.
Detail::UniqueType_bool< true > TrueT
void RequireBooleanExpression(const T &t)
Detail::UniqueType_bool< false > FalseT
Detail::UniqueType_bool< true > TrueT
static bool IsNonnegative(T val)
static constexpr bool Integral
static bool IsNegative(T val)
void const_constraints(const T1 &d, const T2 &e)
void const_constraints(const T1 &d, const T2 &e)
static bool IsNonpositive(T val)
static constexpr bool Integral
static constexpr double e
The base of the natural logarithm or Euler's number
Definition: itkMath.h:53
static constexpr bool NonIntegral
Define additional traits for native types such as int or float.
Detail::UniqueType_bool< true > TrueT
Detail::UniqueType_bool< false > FalseT
static constexpr bool IsSigned