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 private:
49  void strncpy(const char *pSrc);
50  void setErrorString(const std::string &err);
51  void setErrorString(const char *pStr);
52 
53  char m_errorString[BOOL_T_LEN];
54  static char s_defaultErrMsg[];
55  static char s_defaultSuccessMsg[];
56 };
57 
58 inline bool_t::bool_t(bool f)
59 {
60  if (f)
61  m_errorString[0] = 0;
62  else
63  setErrorString(s_defaultErrMsg);
64 }
65 
66 inline bool_t::bool_t(const char *pStr)
67 {
68  setErrorString(pStr);
69 }
70 
71 inline bool_t::bool_t(const std::string &err)
72 {
73  setErrorString(err);
74 }
75 
76 inline void bool_t::strncpy(const char *pSrc)
77 {
78 #ifndef WIN32
79  ::strncpy(m_errorString, pSrc, BOOL_T_LEN);
80 #else
81  strncpy_s(m_errorString, BOOL_T_LEN, pSrc, _TRUNCATE);
82 #endif // !WIN32
83  m_errorString[BOOL_T_LEN-1] = 0;
84 }
85 
86 inline bool_t::bool_t(const bool_t &b)
87 {
88  if (b.m_errorString[0] == 0) // No error
89  m_errorString[0] = 0;
90  else
91  strncpy(b.m_errorString);
92 }
93 
94 inline void bool_t::setErrorString(const std::string &s)
95 {
96  setErrorString(s.c_str());
97 }
98 
99 inline void bool_t::setErrorString(const char *pStr)
100 {
101  if (pStr == 0 || pStr[0] == 0)
102  strncpy(s_defaultErrMsg);
103  else
104  strncpy(pStr);
105 }
106 
107 inline bool_t &bool_t::operator=(const bool_t &b)
108 {
109  if (b.m_errorString[0] == 0) // No error
110  m_errorString[0] = 0;
111  else
112  strncpy(b.m_errorString);
113 
114  return *this;
115 }
116 
117 inline std::string bool_t::getErrorString() const
118 {
119  if (m_errorString[0] == 0)
120  return s_defaultSuccessMsg;
121  return m_errorString;
122 }
123 
124 inline bool_t::operator bool() const
125 {
126  return (m_errorString[0] == 0);
127 }
128 
129 #endif // BOOLTYPE_H
Type to return true/false with error description.
Definition: booltype.h:25
bool_t(bool f=true)
Just set true or false, but leave the error description undefined in case of 'false'.
Definition: booltype.h:58
std::string getErrorString() const
Returns a description of the error.
Definition: booltype.h:117
bool_t & operator=(const bool_t &b)
Assignment operator.
Definition: booltype.h:107