new functions for KM_fileIO
[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   const Result_t RESULT_DIR_CREATE (-21, "Unable to create directory.");
92  
93 } // namespace Kumu
94
95 //--------------------------------------------------------------------------------
96 // convenience macros
97
98 // Convenience macros for managing return values in predicates
99 # define KM_SUCCESS(v) (((v) < 0) ? 0 : 1)
100 # define KM_FAILURE(v) (((v) < 0) ? 1 : 0)
101
102
103 // Returns RESULT_PTR if the given argument is NULL.
104 // See Result_t above for an explanation of RESULT_* symbols.
105 # define KM_TEST_NULL(p) \
106   if ( (p) == 0  ) { \
107     return Kumu::RESULT_PTR; \
108   }
109
110 // Returns RESULT_PTR if the given argument is NULL. See Result_t
111 // in WaimeaCore for an explanation of RESULT_* symbols. It then assumes
112 // that the argument is a pointer to a string and returns
113 // RESULT_NULL_STR if the first character is '\0'.
114 //
115 # define KM_TEST_NULL_STR(p) \
116   KM_TEST_NULL(p); \
117   if ( (p)[0] == '\0' ) { \
118     return Kumu::RESULT_NULL_STR; \
119   }
120
121 namespace Kumu
122 {
123   // simple tracing mechanism
124   class DTrace_t
125   {
126     DTrace_t();
127     
128   protected:
129     const char* m_Label;
130     Result_t*   m_Watch;
131     int         m_Line;
132     const char* m_File;
133     int         m_Sequence;
134
135   public:
136     DTrace_t(const char* Label, Result_t* Watch, int Line, const char* File);
137     ~DTrace_t();
138   };
139 }
140
141 #ifdef KM_TRACE
142 #define WDTRACE(l) DTrace_t __wl__Trace__((l), 0, __LINE__, __FILE__)
143 #define WDTRACER(l,r) DTrace_t __wl__Trace__((l), &(r), __LINE__, __FILE__)
144 #else
145 #define WDTRACE(l)
146 #define WDTRACER(l,r)
147 #endif
148
149
150 #endif // _KM_ERROR_H_
151
152 //
153 // end KM_error.h
154 //