ErrUt  1.1.0 (development version)
booltype.h
Go to the documentation of this file.
1 /*
2 
3  This file is a part of ErrUt, a small collection of error handling
4  utilities.
5 
6  Copyright (c) 2008-2018 Jori Liesenborgs
7 
8  Contact: jori.liesenborgs@gmail.com
9 
10  Permission is hereby granted, free of charge, to any person obtaining a
11  copy of this software and associated documentation files (the "Software"),
12  to deal in the Software without restriction, including without limitation
13  the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  and/or sell copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included
18  in all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
26  IN THE SOFTWARE.
27 
28 */
29 
34 #ifndef ERRUT_BOOLTYPE_H
35 
36 #define ERRUT_BOOLTYPE_H
37 
38 #include "errutconfig.h"
39 #include <string.h>
40 #include <string>
41 
42 namespace errut
43 {
44 
45 #define ERRUT_BOOL_T_LEN 2048
46 #define ERRUT_BOOL_T_FAILMSG "Unknown error"
47 #define ERRUT_BOOL_T_SUCCESSMSG "Success"
48 
60 class ERRUT_IMPORTEXPORT bool_t
61 {
62 public:
64  bool_t(bool f = true);
65 
67  bool_t(const char *pStr);
68 
70  bool_t(const std::string &err);
71 
73  bool_t(const bool_t &b);
74 
76  bool_t &operator=(const bool_t &b);
77 
79  std::string getErrorString() const;
80 
82  operator bool() const;
83 
85  bool success() const;
86 private:
87  void strncpy(const char *pSrc);
88  void setErrorString(const std::string &err);
89  void setErrorString(const char *pStr);
90 
91  char m_errorString[ERRUT_BOOL_T_LEN];
92 };
93 
94 inline bool_t::bool_t(bool f)
95 {
96  if (f)
97  m_errorString[0] = 0;
98  else
99  setErrorString(ERRUT_BOOL_T_FAILMSG);
100 }
101 
102 inline bool_t::bool_t(const char *pStr)
103 {
104  setErrorString(pStr);
105 }
106 
107 inline bool_t::bool_t(const std::string &err)
108 {
109  setErrorString(err);
110 }
111 
112 inline void bool_t::strncpy(const char *pSrc)
113 {
114 #ifndef _WIN32
115  ::strncpy(m_errorString, pSrc, ERRUT_BOOL_T_LEN);
116 #else
117  strncpy_s(m_errorString, ERRUT_BOOL_T_LEN, pSrc, _TRUNCATE);
118 #endif // !_WIN32
119  m_errorString[ERRUT_BOOL_T_LEN-1] = 0;
120 }
121 
122 inline bool_t::bool_t(const bool_t &b)
123 {
124  if (b.m_errorString[0] == 0) // No error
125  m_errorString[0] = 0;
126  else
127  strncpy(b.m_errorString);
128 }
129 
130 inline void bool_t::setErrorString(const std::string &s)
131 {
132  setErrorString(s.c_str());
133 }
134 
135 inline void bool_t::setErrorString(const char *pStr)
136 {
137  if (pStr == 0 || pStr[0] == 0)
138  strncpy(ERRUT_BOOL_T_FAILMSG);
139  else
140  strncpy(pStr);
141 }
142 
143 inline bool_t &bool_t::operator=(const bool_t &b)
144 {
145  if (b.m_errorString[0] == 0) // No error
146  m_errorString[0] = 0;
147  else
148  strncpy(b.m_errorString);
149 
150  return *this;
151 }
152 
153 inline std::string bool_t::getErrorString() const
154 {
155  if (m_errorString[0] == 0)
156  return ERRUT_BOOL_T_SUCCESSMSG;
157  return m_errorString;
158 }
159 
160 inline bool_t::operator bool() const
161 {
162  return success();
163 }
164 
165 inline bool bool_t::success() const
166 {
167  return (m_errorString[0] == 0);
168 }
169 
170 } // end namespace
171 
172 #endif // ERRUT_BOOLTYPE_H
Type to return true/false with error description.
Definition: booltype.h:61
bool_t(bool f=true)
Just set true or false, but leave the error description undefined in case of 'false'.
Definition: booltype.h:94
std::string getErrorString() const
Returns a description of the error.
Definition: booltype.h:153
bool success() const
Returns true or false, depending on the contents of this object.
Definition: booltype.h:165
bool_t & operator=(const bool_t &b)
Assignment operator.
Definition: booltype.h:143