Windows portability fixes.
[asdcplib.git] / src / KM_error.h
1 /*
2 Copyright (c) 2004-2006, 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 namespace Kumu
38 {
39   // Result code container. Both a signed integer and a text string are stored in the object.
40   // When defining your own codes your choice of integer values is mostly unconstrained, but pay
41   // attention to the numbering in the other libraries that use Kumu. Values between -99 and 99
42   // are reserved for Kumu.
43
44   class Result_t
45     {
46       int value;
47       const char* label;
48       Result_t();
49
50     public:
51       static const Result_t& Find(int);
52
53       Result_t(int v, const char* l);
54       ~Result_t();
55
56       inline bool        operator==(const Result_t& rhs) const { return value == rhs.value; }
57       inline bool        operator!=(const Result_t& rhs) const { return value != rhs.value; }
58       inline bool        Success() const { return ( value >= 0 ); }
59       inline bool        Failure() const { return ( value < 0 ); }
60
61       inline int         Value() const { return value; }
62       inline operator    int() const { return value; }
63
64       inline const char* Label() const { return label; }
65       inline operator    const char*() const { return label; }
66     };
67
68   const Result_t RESULT_FALSE      ( 1,   "Successful but not true.");
69   const Result_t RESULT_OK         ( 0,   "Success.");
70   const Result_t RESULT_FAIL       (-1,   "An undefined error was detected.");
71   const Result_t RESULT_PTR        (-2,   "An unexpected NULL pointer was given.");
72   const Result_t RESULT_NULL_STR   (-3,   "An unexpected empty string was given.");
73   const Result_t RESULT_ALLOC      (-4,   "Error allocating memory.");
74   const Result_t RESULT_PARAM      (-5,   "Invalid parameter.");
75   const Result_t RESULT_NOTIMPL    (-6,   "Unimplemented Feature.");
76   const Result_t RESULT_SMALLBUF   (-7,   "The given buffer is too small.");
77   const Result_t RESULT_INIT       (-8,   "The object is not yet initialized.");
78   const Result_t RESULT_NOT_FOUND  (-9,   "The requested file does not exist on the system.");
79   const Result_t RESULT_NO_PERM    (-10,  "Insufficient privilege exists to perform the operation.");
80   const Result_t RESULT_STATE      (-11,  "Object state error.");
81   const Result_t RESULT_CONFIG     (-12,  "Invalid configuration option detected.");
82   const Result_t RESULT_FILEOPEN   (-13,  "File open failure.");
83   const Result_t RESULT_BADSEEK    (-14,  "An invalid file location was requested.");
84   const Result_t RESULT_READFAIL   (-15,  "File read error.");
85   const Result_t RESULT_WRITEFAIL  (-16,  "File write error.");
86   const Result_t RESULT_ENDOFFILE  (-17,  "Attempt to read past end of file.");
87   const Result_t RESULT_FILEEXISTS (-18,  "Filename already exists.");
88   const Result_t RESULT_NOTAFILE   (-19,  "Filename not found.");
89 } // namespace Kumu
90
91 //--------------------------------------------------------------------------------
92 // convenience macros
93
94 // Convenience macros for managing return values in predicates
95 # define KM_SUCCESS(v) (((v) < 0) ? 0 : 1)
96 # define KM_FAILURE(v) (((v) < 0) ? 1 : 0)
97
98
99 // Returns RESULT_PTR if the given argument is NULL.
100 // See Result_t above for an explanation of RESULT_* symbols.
101 # define KM_TEST_NULL(p) \
102   if ( (p) == 0  ) { \
103     return Kumu::RESULT_PTR; \
104   }
105
106 // Returns RESULT_PTR if the given argument is NULL. See Result_t
107 // in WaimeaCore for an explanation of RESULT_* symbols. It then assumes
108 // that the argument is a pointer to a string and returns
109 // RESULT_NULL_STR if the first character is '\0'.
110 //
111 # define KM_TEST_NULL_STR(p) \
112   KM_TEST_NULL(p); \
113   if ( (p)[0] == '\0' ) { \
114     return Kumu::RESULT_NULL_STR; \
115   }
116
117 namespace Kumu
118 {
119   // simple tracing mechanism
120   class DTrace_t
121   {
122     DTrace_t();
123     
124   protected:
125     const char* m_Label;
126     Result_t*   m_Watch;
127     int         m_Line;
128     const char* m_File;
129     int         m_Sequence;
130
131   public:
132     DTrace_t(const char* Label, Result_t* Watch, int Line, const char* File);
133     ~DTrace_t();
134   };
135 }
136
137 #ifdef KM_TRACE
138 #define WDTRACE(l) DTrace_t __wl__Trace__((l), 0, __LINE__, __FILE__)
139 #define WDTRACER(l,r) DTrace_t __wl__Trace__((l), &(r), __LINE__, __FILE__)
140 #else
141 #define WDTRACE(l)
142 #define WDTRACER(l,r)
143 #endif
144
145
146 #endif // _KM_ERROR_H_
147
148 //
149 // end KM_error.h
150 //