ATCFS
 全て クラス 名前空間 ファイル 関数 変数 型定義 マクロ定義 ページ
Base.h
[詳解]
1 // Copyright 2018 S520
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met :
6 //
7 // 1. Redistributions of source code must retain the above copyright notice,
8 // this list of conditions and the following disclaimer.
9 // 2. Redistributions in binary form must reproduce the above copyright notice,
10 // this list of conditions and the following disclaimer in the documentation
11 // and / or other materials provided with the distribution.
12 //
13 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 // DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17 // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 
24 #ifndef BASE_H_
25 #define BASE_H_
26 #include <stdexcept>
27 #include <boost/container/vector.hpp>
28 #include <boost/array.hpp>
29 
33 class Base {
34  protected:
45  template<typename T>T VectorGetOrDefault(const boost::container::vector<T> &v, int index, char* file, char* func, int line) {
46  char debugstr[1000];
47  if (!v.empty()) {
48  if (index < 0) {
49  sprintf_s(debugstr, sizeof(debugstr), "Out of Renge!!! (Vector index: %d < 0) at File: %s / Func: %s / Line: %d\n", index, file, func, line);
50  OutputDebugString(debugstr);
51  return v[0];
52  } else if (index > static_cast<int>(v.size() - 1)) {
53  sprintf_s(debugstr, sizeof(debugstr), "Out of Renge!!! (Vector index: %d > %d) at File: %s / Func: %s / Line: %d\n", index, static_cast<int>(v.size() - 1), file, func, line);
54  OutputDebugString(debugstr);
55  return v.back();
56  } else {
57  return v[index];
58  }
59  } else {
60  sprintf_s(debugstr, sizeof(debugstr), "Out of Renge!!! (This Vector is Empty) at File: %s / Func: %s / Line: %d\n", file, func, line);
61  OutputDebugString(debugstr);
62  return static_cast<T>(0.0);
63  }
64  }
65 
76  template<typename T, std::size_t N>T ArrayGetOrDefault(const boost::array<T, N>&a, int index, char* file, char* func, int line) {
77  char debugstr[1000];
78  if (!a.empty()) {
79  if (index < 0) {
80  sprintf_s(debugstr, sizeof(debugstr), "Out of Renge!!! (Array index: %d < 0) at File: %s / Func: %s / Line: %d\n", index, file, func, line);
81  OutputDebugString(debugstr);
82  return a[0];
83  } else if (index > static_cast<int>(a.size() - 1)) {
84  sprintf_s(debugstr, sizeof(debugstr), "Out of Renge!!! (Array index: %d > %d) at File: %s / Func: %s / Line: %d\n", index, static_cast<int>(a.size() - 1), file, func, line);
85  OutputDebugString(debugstr);
86  return a.back();
87  } else {
88  return a[index];
89  }
90  } else {
91  sprintf_s(debugstr, sizeof(debugstr), "Out of Renge!!! (This Array is Empty) at File: %s / Func: %s / Line: %d\n", file, func, line);
92  OutputDebugString(debugstr);
93  return static_cast<T>(0.0);
94  }
95  }
96 
97  template<typename T>
98  T VectorTryGet(const boost::container::vector<T> &v, int index, char* file, char* func, int line) {
99  try {
100  return v.at(index);
101  } catch (std::out_of_range) {
102  char debugstr[1000];
103  if (!v.empty()) {
104  if (index < 0) {
105  sprintf_s(debugstr, sizeof(debugstr), "Out of Renge!!! (Vector index: %d < 0) at File: %s / Func: %s / Line: %d\n", index, file, func, line);
106  OutputDebugString(debugstr);
107  } else if (index > static_cast<int>(v.size() - 1)) {
108  sprintf_s(debugstr, sizeof(debugstr), "Out of Renge!!! (Vector index: %d > %d) at File: %s / Func: %s / Line: %d\n", index, static_cast<int>(v.size() - 1), file, func, line);
109  OutputDebugString(debugstr);
110  }
111  } else {
112  sprintf_s(debugstr, sizeof(debugstr), "Out of Renge!!! (This Vector is Empty) at File: %s / Func: %s / Line: %d\n", file, func, line);
113  OutputDebugString(debugstr);
114  }
115  throw;
116  }
117  }
118 
119  template<typename T, std::size_t N>
120  T ArrayTryGet(const boost::array<T, N> &a, int index, char* file, char* func, int line) {
121  try {
122  return a.at(index);
123  } catch (std::out_of_range) {
124  char debugstr[1000];
125  if (!a.empty()) {
126  if (index < 0) {
127  sprintf_s(debugstr, sizeof(debugstr), "Out of Renge!!! (Array index: %d < 0) at File: %s / Func: %s / Line: %d\n", index, file, func, line);
128  OutputDebugString(debugstr);
129  } else if (index > static_cast<int>(a.size() - 1)) {
130  sprintf_s(debugstr, sizeof(debugstr), "Out of Renge!!! (Array index: %d > %d) at File: %s / Func: %s / Line: %d\n", index, static_cast<int>(a.size() - 1), file, func, line);
131  OutputDebugString(debugstr);
132  }
133  } else {
134  sprintf_s(debugstr, sizeof(debugstr), "Out of Renge!!! (This Array is Empty) at File: %s / Func: %s / Line: %d\n", file, func, line);
135  OutputDebugString(debugstr);
136  }
137  throw;
138  }
139  }
140 };
141 
142 #endif // BASE_H_
T VectorTryGet(const boost::container::vector< T > &v, int index, char *file, char *func, int line)
Definition: Base.h:98
テンプレートを記述する基底クラス
Definition: Base.h:33
T VectorGetOrDefault(const boost::container::vector< T > &v, int index, char *file, char *func, int line)
vectorコンテナへの範囲外アクセス時にデフォルト値を返す関数
Definition: Base.h:45
T ArrayTryGet(const boost::array< T, N > &a, int index, char *file, char *func, int line)
Definition: Base.h:120
T ArrayGetOrDefault(const boost::array< T, N > &a, int index, char *file, char *func, int line)
arrayコンテナへの範囲外アクセス時にデフォルト値を返す関数
Definition: Base.h:76