/
util.h
39 lines (30 loc) · 925 Bytes
/
util.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
#ifndef LATTICELM_UTIL_H__
#define LATTICELM_UTIL_H__
#include <vector>
#define LATTICELM_SAFE
#define THROW_ERROR(msg) do { \
std::ostringstream oss; \
oss << msg; \
throw std::runtime_error(oss.str()); } \
while (0);
namespace latticelm {
// Perform safe access to a vector
template < class T >
inline const T & SafeAccess(const std::vector<T> & vec, int idx) {
#ifdef LATTICELM_SAFE
if(idx < 0 || idx >= (int)vec.size())
THROW_ERROR("Out of bound access size="<<vec.size()<<", idx="<<idx);
#endif
return vec[idx];
}
// Perform safe access to a vector
template < class T >
inline T & SafeAccess(std::vector<T> & vec, int idx) {
#ifdef LATTICELM_SAFE
if(idx < 0 || idx >= (int)vec.size())
THROW_ERROR("Out of bound access size="<<vec.size()<<", idx="<<idx);
#endif
return vec[idx];
}
}
#endif