TooN
slice_error.hh
1 // -*- c++ -*-
2 
3 // Copyright (C) 2009 Ed Rosten (er258@cam.ac.uk)
4 
5 //All rights reserved.
6 //
7 //Redistribution and use in source and binary forms, with or without
8 //modification, are permitted provided that the following conditions
9 //are met:
10 //1. Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 //2. Redistributions in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in the
14 // documentation and/or other materials provided with the distribution.
15 //
16 //THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
17 //AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 //IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 //ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
20 //LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 //CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 //SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 //INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 //CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 //ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 //POSSIBILITY OF SUCH DAMAGE.
27 
28 namespace TooN {
29 
30 namespace Internal
31 {
32  template<bool StaticBad>
33  struct BadSlice;
34 
42  template<>
43  struct BadSlice<0>{
44  static void check(){}
45  };
46 
55  template<int Size, int Start, int Length>
56  struct CheckSlice
57  {
58 
61  template<int Num> struct N
62  {
63  static int n(int num)
64  {
65  return (Num==Dynamic||Num==Resizable)?num:Num;
66  }
67  };
68 
75  static void check()
76  {
77  //Sanity check all basic static sizes
79  BadSlice<!(Start >= 0)>::check();
80  BadSlice<!(Length >= 0)>::check();
82  }
83 
93  static void check(int size, int start, int length)
94  {
95  //Sanity check all basic static sizes
99 
100  //We can make sure Length <= Size, even if Start is unknown
102 
103  //We can make sure Start < Size even if Length is unknown
105 
107  #ifndef TOON_NDEBUG_MISMATCH
108  if(Start != Dynamic && Size != Resizable && Start != start)
109  {
110  std::cerr << "TooN slice: mismatch between static and dynamic start.\n";
111  std::abort();
112  }
113  if(Length != Dynamic && Size != Resizable && Length != length)
114  {
115  std::cerr << "TooN slice: mismatch between static and dynamic length.\n";
116  std::abort();
117  }
118  if(Size != Dynamic && Size != Resizable && Size != size)
119  {
120  std::cerr << "TooN slice: mismatch between static and dynamic size.\n";
121  std::abort();
122  }
123  #endif
124  if( N<Start>::n(start) + N<Length>::n(length) > N<Size>::n(size) ||
125  N<Start>::n(start) < 0 ||
126  N<Length>::n(length) < 0)
127  {
128  #ifdef TOON_TEST_INTERNALS
129  throw Internal::SliceError();
130  #elif !defined TOON_NDEBUG_SLICE
131  std::cerr << "TooN slice out of range" << std::endl;
132  std::abort();
133  #endif
134  }
135  }
136  };
137 
138  #ifdef TOON_TEST_INTERNALS
139  template<bool StaticBad>
140  struct BadSlice{
141  static void check(){
142  throw Internal::StaticSliceError();
143  }
144  };
145  #endif
146 }
147 
148 }
Definition: slice_error.hh:56
Pretty generic SFINAE introspection generator.
Definition: vec_test.cc:21
Definition: slice_error.hh:61
Definition: slice_error.hh:33
static void check()
This function does nothing: it merely exists.
Definition: slice_error.hh:44