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