-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBase.h
142 lines (136 loc) · 7.34 KB
/
Base.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright 2018 S520
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met :
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and / or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef BASE_H_
#define BASE_H_
#include <stdexcept>
#include <boost/container/vector.hpp>
#include <boost/array.hpp>
/// <summary>
/// テンプレートを記述する基底クラス
/// </summary>
class Base {
protected:
/// <summary>
/// vectorコンテナへの範囲外アクセス時にデフォルト値を返す関数
/// </summary>
/// <remarks>vectorコンテナが空の場合は0を、インデックスが負の場合は0番目の要素を、インデックスが要素数以上の場合は最後の要素を返す</remarks>
/// <param name="v">vectorオブジェクト</param>
/// <param name="index">インデックス</param>
/// <param name="file">(Debug用)呼び出し元ファイル</param>
/// <param name="func">(Debug用)呼び出し元関数</param>
/// <param name="line">(Debug用)呼び出し元行数</param>
/// <returns>vectorコンテナの要素またはデフォルト値</returns>
template<typename T>T VectorGetOrDefault(const boost::container::vector<T> &v, int index, char* file, char* func, int line) {
char debugstr[1000];
if (!v.empty()) {
if (index < 0) {
sprintf_s(debugstr, sizeof(debugstr), "Out of Renge!!! (Vector index: %d < 0) at File: %s / Func: %s / Line: %d\n", index, file, func, line);
OutputDebugString(debugstr);
return v[0];
} else if (index > static_cast<int>(v.size() - 1)) {
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);
OutputDebugString(debugstr);
return v.back();
} else {
return v[index];
}
} else {
sprintf_s(debugstr, sizeof(debugstr), "Out of Renge!!! (This Vector is Empty) at File: %s / Func: %s / Line: %d\n", file, func, line);
OutputDebugString(debugstr);
return static_cast<T>(0.0);
}
}
/// <summary>
/// arrayコンテナへの範囲外アクセス時にデフォルト値を返す関数
/// </summary>
/// <remarks>arrayコンテナが空の場合は0を、インデックスが負の場合は0番目の要素を、インデックスが要素数以上の場合は最後の要素を返す</remarks>
/// <param name="a">arrayオブジェクト</param>
/// <param name="index">インデックス</param>
/// <param name="file">(Debug用)呼び出し元ファイル</param>
/// <param name="func">(Debug用)呼び出し元関数</param>
/// <param name="line">(Debug用)呼び出し元行数</param>
/// <returns>vectorコンテナの要素またはデフォルト値</returns>
template<typename T, std::size_t N>T ArrayGetOrDefault(const boost::array<T, N>&a, int index, char* file, char* func, int line) {
char debugstr[1000];
if (!a.empty()) {
if (index < 0) {
sprintf_s(debugstr, sizeof(debugstr), "Out of Renge!!! (Array index: %d < 0) at File: %s / Func: %s / Line: %d\n", index, file, func, line);
OutputDebugString(debugstr);
return a[0];
} else if (index > static_cast<int>(a.size() - 1)) {
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);
OutputDebugString(debugstr);
return a.back();
} else {
return a[index];
}
} else {
sprintf_s(debugstr, sizeof(debugstr), "Out of Renge!!! (This Array is Empty) at File: %s / Func: %s / Line: %d\n", file, func, line);
OutputDebugString(debugstr);
return static_cast<T>(0.0);
}
}
template<typename T>
T VectorTryGet(const boost::container::vector<T> &v, int index, char* file, char* func, int line) {
try {
return v.at(index);
} catch (std::out_of_range) {
char debugstr[1000];
if (!v.empty()) {
if (index < 0) {
sprintf_s(debugstr, sizeof(debugstr), "Out of Renge!!! (Vector index: %d < 0) at File: %s / Func: %s / Line: %d\n", index, file, func, line);
OutputDebugString(debugstr);
} else if (index > static_cast<int>(v.size() - 1)) {
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);
OutputDebugString(debugstr);
}
} else {
sprintf_s(debugstr, sizeof(debugstr), "Out of Renge!!! (This Vector is Empty) at File: %s / Func: %s / Line: %d\n", file, func, line);
OutputDebugString(debugstr);
}
throw;
}
}
template<typename T, std::size_t N>
T ArrayTryGet(const boost::array<T, N> &a, int index, char* file, char* func, int line) {
try {
return a.at(index);
} catch (std::out_of_range) {
char debugstr[1000];
if (!a.empty()) {
if (index < 0) {
sprintf_s(debugstr, sizeof(debugstr), "Out of Renge!!! (Array index: %d < 0) at File: %s / Func: %s / Line: %d\n", index, file, func, line);
OutputDebugString(debugstr);
} else if (index > static_cast<int>(a.size() - 1)) {
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);
OutputDebugString(debugstr);
}
} else {
sprintf_s(debugstr, sizeof(debugstr), "Out of Renge!!! (This Array is Empty) at File: %s / Func: %s / Line: %d\n", file, func, line);
OutputDebugString(debugstr);
}
throw;
}
}
};
#endif // BASE_H_