TooN
size_mismatch.hh
1 // -*- c++ -*-
2 
3 // Copyright (C) 2009 Tom Drummond (twd20@cam.ac.uk),
4 // Ed Rosten (er258@cam.ac.uk)
5 
6 //All rights reserved.
7 //
8 //Redistribution and use in source and binary forms, with or without
9 //modification, are permitted provided that the following conditions
10 //are met:
11 //1. Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 //2. Redistributions in binary form must reproduce the above copyright
14 // notice, this list of conditions and the following disclaimer in the
15 // documentation and/or other materials provided with the distribution.
16 //
17 //THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
18 //AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 //IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 //ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
21 //LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 //CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 //SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 //INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 //CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 //ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 //POSSIBILITY OF SUCH DAMAGE.
28 
29 namespace TooN {
30 
31 // class to generate compile time error
32 // general case which doesn't exist
33 template<int Size1, int Size2>
35 
36 // special cases which do exist
37 template<int Size>
38 struct SizeMismatch_<Size,Size>{
39  static inline void test(int, int){}
40 };
41 
42 template<int Size>
43 struct SizeMismatch_<Dynamic,Size>{
44  static inline void test(int size1, int size2){
45  if(size1!=size2){
46  #ifdef TOON_TEST_INTERNALS
47  throw Internal::SizeMismatch();
48  #elif !defined TOON_NDEBUG_SIZE
49  std::cerr << "TooN Size Mismatch" << std::endl;
50  std::abort();
51  #endif
52  }
53  }
54 };
55 
56 template<int Size>
57 struct SizeMismatch_<Size,Dynamic>{
58  static inline void test(int size1, int size2){
59  if(size1!=size2){
60  #ifdef TOON_TEST_INTERNALS
61  throw Internal::SizeMismatch();
62  #elif !defined TOON_NDEBUG_SIZE
63  std::cerr << "TooN Size Mismatch" << std::endl;
64  std::abort();
65  #endif
66  }
67  }
68 };
69 
70 template <>
71 struct SizeMismatch_<Dynamic,Dynamic>{
72  static inline void test(int size1, int size2){
73  if(size1!=size2){
74  #ifdef TOON_TEST_INTERNALS
75  throw Internal::SizeMismatch();
76  #elif !defined TOON_NDEBUG_SIZE
77  std::cerr << "TooN Size Mismatch" << std::endl;
78  std::abort();
79  #endif
80  }
81  }
82 };
83 
84 #if 0
85 namespace Internal
86 {
87  struct BadSize;
88 }
89 #endif
90 
91 #ifdef TOON_TEST_INTERNALS
92 template<int Size1, int Size2>
93 struct SizeMismatch_
94 {
95  static inline void test(int, int)
96  {
97  throw Internal::StaticSizeMismatch();
98  }
99 };
100 #endif
101 
102 template<int Size1, int Size2>
104 {
105  static inline void test(int s1, int s2)
106  {
107  SizeMismatch_< (Size1 == Dynamic || Size1 == Resizable)?Dynamic:Size1,
108  (Size2 == Dynamic || Size2 == Resizable)?Dynamic:Size2 >::test(s1, s2);
109  }
110 };
111 
112 }
Pretty generic SFINAE introspection generator.
Definition: vec_test.cc:21
Definition: size_mismatch.hh:34
Definition: size_mismatch.hh:103