Merge pull request #25 from remia/fix/non-pod-variadic-warning
[asdcplib.git] / src / KM_error.h
1 /*
2 Copyright (c) 2004-2015, John Hurst
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8 1. Redistributions of source code must retain the above copyright
9    notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11    notice, this list of conditions and the following disclaimer in the
12    documentation and/or other materials provided with the distribution.
13 3. The name of the author may not be used to endorse or promote products
14    derived from this software without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27   /*! \file    KM_error.h
28     \version $Id$
29     \brief   error reporting support
30   */
31
32
33
34 #ifndef _KM_ERROR_H_
35 #define _KM_ERROR_H_
36
37 #include <string>
38
39 #define KM_DECLARE_RESULT(sym, i, l) const Result_t RESULT_##sym = Result_t(i, #sym, l);
40
41 namespace Kumu
42 {
43   // Result code container. Both a signed integer and a text string are stored in the object.
44   // When defining your own codes your choice of integer values is mostly unconstrained, but pay
45   // attention to the numbering in the other libraries that use Kumu. Values between -99 and 99
46   // are reserved for Kumu.
47
48   class Result_t
49     {
50       int value;
51       std::string label, symbol, message;
52       Result_t();
53
54     public:
55       // Return registered Result_t for the given "value" code.
56       static const Result_t& Find(int value);
57
58       // Unregister the Result_t matching the given "value" code. Returns
59       // RESULT_FALSE if "value" does not match a registered Result_t.
60       // Returns RESULT_FAIL if ( value < -99 || value > 99 ) (Kumu core
61       // codes may not be deleted).
62       static Result_t Delete(int value);
63
64       // Iteration through registered result codes, not thread safe.
65       // Get accepts contiguous values from 0 to End() - 1.
66       static unsigned int End();
67       static const Result_t& Get(unsigned int);
68
69       Result_t(int v, const std::string& s, const std::string& l);
70       Result_t(const Result_t& rhs);
71       const Result_t& operator=(const Result_t& rhs);
72       ~Result_t();
73
74       const Result_t operator()(const std::string& message) const;
75       const Result_t operator()(const int& line, const char* filename) const;
76       const Result_t operator()(const std::string& message, const int& line, const char* filename) const;
77
78       inline bool        operator==(const Result_t& rhs) const { return value == rhs.value; }
79       inline bool        operator!=(const Result_t& rhs) const { return value != rhs.value; }
80       inline bool        Success() const { return ! ( value < 0 ); }
81       inline bool        Failure() const { return ( value < 0 ); }
82
83       inline int         Value() const { return value; }
84       inline operator    int() const { return value; }
85       inline const char* Label() const { return label.c_str(); }
86       inline operator    const char*() const { return label.c_str(); }
87       inline const char* Symbol() const { return symbol.c_str(); }
88       inline const char* Message() const { return message.c_str(); }
89     };
90
91   KM_DECLARE_RESULT(FALSE,       1,   "Successful but not true.");
92   KM_DECLARE_RESULT(OK,          0,   "Success.");
93   KM_DECLARE_RESULT(FAIL,       -1,   "An undefined error was detected.");
94   KM_DECLARE_RESULT(PTR,        -2,   "An unexpected NULL pointer was given.");
95   KM_DECLARE_RESULT(NULL_STR,   -3,   "An unexpected empty string was given.");
96   KM_DECLARE_RESULT(ALLOC,      -4,   "Error allocating memory.");
97   KM_DECLARE_RESULT(PARAM,      -5,   "Invalid parameter.");
98   KM_DECLARE_RESULT(NOTIMPL,    -6,   "Unimplemented Feature.");
99   KM_DECLARE_RESULT(SMALLBUF,   -7,   "The given buffer is too small.");
100   KM_DECLARE_RESULT(INIT,       -8,   "The object is not yet initialized.");
101   KM_DECLARE_RESULT(NOT_FOUND,  -9,   "The requested file does not exist on the system.");
102   KM_DECLARE_RESULT(NO_PERM,    -10,  "Insufficient privilege exists to perform the operation.");
103   KM_DECLARE_RESULT(STATE,      -11,  "Object state error.");
104   KM_DECLARE_RESULT(CONFIG,     -12,  "Invalid configuration option detected.");
105   KM_DECLARE_RESULT(FILEOPEN,   -13,  "File open failure.");
106   KM_DECLARE_RESULT(BADSEEK,    -14,  "An invalid file location was requested.");
107   KM_DECLARE_RESULT(READFAIL,   -15,  "File read error.");
108   KM_DECLARE_RESULT(WRITEFAIL,  -16,  "File write error.");
109   KM_DECLARE_RESULT(ENDOFFILE,  -17,  "Attempt to read past end of file.");
110   KM_DECLARE_RESULT(FILEEXISTS, -18,  "Filename already exists.");
111   KM_DECLARE_RESULT(NOTAFILE,   -19,  "Filename not found.");
112   KM_DECLARE_RESULT(UNKNOWN,    -20,  "Unknown result code.");
113   KM_DECLARE_RESULT(DIR_CREATE, -21,  "Unable to create directory.");
114   KM_DECLARE_RESULT(NOT_EMPTY,  -22,  "Unable to delete non-empty directory.");
115   // 23-100 are reserved
116  
117 } // namespace Kumu
118
119 //--------------------------------------------------------------------------------
120 // convenience macros
121
122 // Convenience macros for managing return values in predicates
123 # define KM_SUCCESS(v) (((v) < 0) ? 0 : 1)
124 # define KM_FAILURE(v) (((v) < 0) ? 1 : 0)
125
126
127 // Returns RESULT_PTR if the given argument is NULL.
128 // See Result_t above for an explanation of RESULT_* symbols.
129 # define KM_TEST_NULL(p) \
130   if ( (p) == 0  ) { \
131     return Kumu::RESULT_PTR(__LINE__, __FILE__); \
132   }
133
134 // Returns RESULT_PTR if the given argument is NULL. See Result_t
135 // in WaimeaCore for an explanation of RESULT_* symbols. It then assumes
136 // that the argument is a pointer to a string and returns
137 // RESULT_NULL_STR if the first character is '\0'.
138 //
139 # define KM_TEST_NULL_STR(p) \
140   KM_TEST_NULL(p); \
141   if ( (p)[0] == '\0' ) { \
142     return Kumu::RESULT_NULL_STR(__LINE__, __FILE__); \
143   }
144
145 // RESULT_STATE is ambiguous.  Use these everywhere it is assigned to provide some context
146 #define KM_RESULT_STATE_TEST_IMPLICIT()                                 \
147   if ( result == Kumu::RESULT_STATE ) {                                 \
148     Kumu::DefaultLogSink().Error("RESULT_STATE RETURNED at %s (%d)\n", __FILE__, __LINE__); \
149   }
150
151 #define KM_RESULT_STATE_TEST_THIS(_this__r_)                            \
152   if ( _this__r_ == Kumu::RESULT_STATE ) {                              \
153     Kumu::DefaultLogSink().Error("RESULT_STATE RETURNED at %s (%d)\n", __FILE__, __LINE__); \
154   }
155
156 #define KM_RESULT_STATE_HERE()                                          \
157   Kumu::DefaultLogSink().Error("RESULT_STATE RETURNED at %s (%d)\n", __FILE__, __LINE__);
158
159
160
161 namespace Kumu
162 {
163   // simple tracing mechanism
164   class DTrace_t
165   {
166     DTrace_t();
167     
168   protected:
169     const char* m_Label;
170     Result_t*   m_Watch;
171     int         m_Line;
172     const char* m_File;
173     int         m_Sequence;
174
175   public:
176     DTrace_t(const char* Label, Result_t* Watch, int Line, const char* File);
177     ~DTrace_t();
178   };
179 }
180
181 #ifdef KM_TRACE
182 #define WDTRACE(l) DTrace_t __wl__Trace__((l), 0, __LINE__, __FILE__)
183 #define WDTRACER(l,r) DTrace_t __wl__Trace__((l), &(r), __LINE__, __FILE__)
184 #else
185 #define WDTRACE(l)
186 #define WDTRACER(l,r)
187 #endif
188
189
190 #endif // _KM_ERROR_H_
191
192 //
193 // end KM_error.h
194 //