Simpact Cyan
Population based event driven simulation using mNRM
booltype.h
Go to the documentation of this file.
1 #ifndef BOOLTYPE_H
2 
3 #define BOOLTYPE_H
4 
9 #include <string>
10 #include <string.h>
11 
12 #define BOOL_T_LEN 2048
13 
25 class bool_t
26 {
27 public:
29  bool_t(bool f = true);
30 
32  bool_t(const char *pStr);
33 
35  bool_t(const std::string &err);
36 
38  bool_t(const bool_t &b);
39 
41  bool_t &operator=(const bool_t &b);
42 
44  std::string getErrorString() const;
45 
47  operator bool() const;
48 
49  bool success() const;
50 private:
51  void strncpy(const char *pSrc);
52  void setErrorString(const std::string &err);
53  void setErrorString(const char *pStr);
54 
55  char m_errorString[BOOL_T_LEN];
56  static char s_defaultErrMsg[];
57  static char s_defaultSuccessMsg[];
58 };
59 
60 inline bool_t::bool_t(bool f)
61 {
62  if (f)
63  m_errorString[0] = 0;
64  else
65  setErrorString(s_defaultErrMsg);
66 }
67 
68 inline bool_t::bool_t(const char *pStr)
69 {
70  setErrorString(pStr);
71 }
72 
73 inline bool_t::bool_t(const std::string &err)
74 {
75  setErrorString(err);
76 }
77 
78 inline void bool_t::strncpy(const char *pSrc)
79 {
80 #ifndef WIN32
81  ::strncpy(m_errorString, pSrc, BOOL_T_LEN);
82 #else
83  strncpy_s(m_errorString, BOOL_T_LEN, pSrc, _TRUNCATE);
84 #endif // !WIN32
85  m_errorString[BOOL_T_LEN-1] = 0;
86 }
87 
88 inline bool_t::bool_t(const bool_t &b)
89 {
90  if (b.m_errorString[0] == 0) // No error
91  m_errorString[0] = 0;
92  else
93  strncpy(b.m_errorString);
94 }
95 
96 inline void bool_t::setErrorString(const std::string &s)
97 {
98  setErrorString(s.c_str());
99 }
100 
101 inline void bool_t::setErrorString(const char *pStr)
102 {
103  if (pStr == 0 || pStr[0] == 0)
104  strncpy(s_defaultErrMsg);
105  else
106  strncpy(pStr);
107 }
108 
109 inline bool_t &bool_t::operator=(const bool_t &b)
110 {
111  if (b.m_errorString[0] == 0) // No error
112  m_errorString[0] = 0;
113  else
114  strncpy(b.m_errorString);
115 
116  return *this;
117 }
118 
119 inline std::string bool_t::getErrorString() const
120 {
121  if (m_errorString[0] == 0)
122  return s_defaultSuccessMsg;
123  return m_errorString;
124 }
125 
126 inline bool_t::operator bool() const
127 {
128  return success();
129 }
130 
131 inline bool bool_t::success() const
132 {
133  return (m_errorString[0] == 0);
134 }
135 
136 #endif // BOOLTYPE_H
Type to return true/false with error description.
Definition: booltype.h:26
bool_t & operator=(const bool_t &b)
Assignment operator.
Definition: booltype.h:109
std::string getErrorString() const
Returns a description of the error.
Definition: booltype.h:119
bool_t(bool f=true)
Just set true or false, but leave the error description undefined in case of 'false'.
Definition: booltype.h:60