GenApi  3.5.1
GCException.h
Go to the documentation of this file.
1 //-----------------------------------------------------------------------------
2 // (c) 2004 by Basler Vision Technologies
3 // Author: Fritz Dierks
4 //
5 // License: This file is published under the license of the EMVA GenICam Standard Group.
6 // A text file describing the legal terms is included in your installation as 'GenICam_license.pdf'.
7 // If for some reason you are missing this file please contact the EMVA or visit the website
8 // (http://www.genicam.org) for a full copy.
9 //
10 // THIS SOFTWARE IS PROVIDED BY THE EMVA GENICAM STANDARD GROUP "AS IS"
11 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
12 // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
13 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE EMVA GENICAM STANDARD GROUP
14 // OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
15 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
16 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
17 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
18 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
19 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
20 // POSSIBILITY OF SUCH DAMAGE.
21 //
22 // Contributions
23 // changed minor things like namespace
24 // added a AccessException
25 // added a TimeoutException
26 // Copyright (c) 2005 STEMMER IMAGING (Rupert Stelz)
27 //
28 // used _vsnprintf_s function for VS8 and higher (compiler fork)
29 // Copyright (c) 2006 STEMMER IMAGING (Sascha Dorenbeck)
30 //
31 // prepared for remove of std::exception base class
32 // Copyright (c) 2014 STEMMER IMAGING (Sascha Dorenbeck)
33 //
34 //-----------------------------------------------------------------------------
41 #ifndef GENCAM_EXCEPTION_H_
42 #define GENCAM_EXCEPTION_H_
43 
44 #include <cassert>
45 #include <cstdarg>
46 #include <exception>
47 #include <sstream>
48 #include <stdio.h>
49 #include <stdarg.h>
50 #include <Base/GCTypes.h>
51 #include <Base/GCString.h>
52 #include <Base/GCCompatibility.h>
53 
54 #pragma pack(push, 8)
55 
56 namespace GENICAM_NAMESPACE
57 {
62  class GCBASE_RTTI_CLASS_API GenericException
63  {
64  public:
66  GenericException(const char* pDescription, const char *pSourceFileName, unsigned int SourceLine);
67 
69  GenericException(const char* pDescription, const char *pSourceFileName, unsigned int SourceLine, const char* pExceptionType);
70 
72  GenericException(const char* pDescription, const char *pSourceFileName, unsigned int SourceLine, const char *pEntryPoint, const char *pErrorNodeName, const char* pExceptionType);
73 
75  virtual const char* GetDescription() const throw();
76 
78  virtual const char* GetSourceFileName() const throw();
79 
81  virtual unsigned int GetSourceLine() const throw();
82 
84  virtual const char *what() const throw();
85 
86  virtual ~GenericException() throw();
87 
88  private:
89 
91  void AssembleMessage();
92 
95 
97  GENICAM_NAMESPACE::gcstring m_ExceptionType;
98 
100  unsigned int m_SourceLine;
101 
103  GENICAM_NAMESPACE::gcstring m_SourceFileName;
104 
106  GENICAM_NAMESPACE::gcstring m_Description;
107 
108  /* the following members are GenApi specific */
109 
111  GENICAM_NAMESPACE::gcstring m_EntryPoint;
112 
114  GENICAM_NAMESPACE::gcstring m_ErrorNodeName;
115  };
116 
119 #define DECLARE_EXCEPTION( name ) \
120  class GCBASE_RTTI_CLASS_API name : public GENICAM_NAMESPACE::GenericException \
121  { \
122  public: \
123  name( const char* pDescription, const char *pSourceFileName, int SourceLine ); \
124  name( const char* pDescription, const char *pSourceFileName, int SourceLine, const char* pExceptionType ); \
125  name( const char* pDescription, const char *pSourceFileName, int SourceLine, const char *pEntryPoint, const char *pErrorNodeName, const char* pExceptionType ); \
126  }
127 
129 
130  DECLARE_EXCEPTION(BadAllocException);
131 
133  DECLARE_EXCEPTION(InvalidArgumentException);
134 
136  DECLARE_EXCEPTION(OutOfRangeException);
137 
139  DECLARE_EXCEPTION(PropertyException);
140 
142  DECLARE_EXCEPTION(RuntimeException);
143 
145  DECLARE_EXCEPTION(LogicalErrorException);
146 
148  DECLARE_EXCEPTION(AccessException);
149 
151  DECLARE_EXCEPTION(TimeoutException);
152 
154  DECLARE_EXCEPTION(DynamicCastException);
155 
157 
158  /*-----------------------------------------------------------------*/
159  // Utilities
160  /*-----------------------------------------------------------------*/
161 
166  template <typename E>
168  {
169  public:
170 
171  ExceptionReporter(const char* pSourceFileName, int SourceLine)
172  : m_SourceFileName(pSourceFileName)
173  , m_SourceLine(SourceLine)
174  {
175  }
176 
177  ExceptionReporter(const char* pSourceFileName, int SourceLine, const char* pExceptionType)
178  : m_SourceFileName(pSourceFileName)
179  , m_SourceLine(SourceLine)
180  , m_ExceptionType(pExceptionType)
181  {
182  }
183 
184  E Report(const char* pFormat, ...)
185  {
186  char pBuffer[2048];
187  va_list vap;
188  va_start(vap, pFormat);
189 
190 # if defined (_MSC_VER)
191  vsnprintf_s(pBuffer, sizeof pBuffer, _TRUNCATE, pFormat, vap);
192 # else
193  vsnprintf( pBuffer, sizeof pBuffer, pFormat, vap );
194 # endif
195 
196  va_end(vap);
197 
198  return E(pBuffer, m_SourceFileName.c_str(), m_SourceLine, m_ExceptionType.c_str());
199  };
200 
201  E Report()
202  {
203  return E("", m_SourceFileName.c_str(), m_SourceLine, m_ExceptionType.c_str());
204  }
205 
206  E Report(const std::string &s)
207  {
208  return E(s.c_str(), m_SourceFileName.c_str(), m_SourceLine, m_ExceptionType.c_str());
209  }
210 
211  E Report(const std::stringstream& str)
212  {
213  return E(str.str().c_str(), m_SourceFileName.c_str(), m_SourceLine, m_ExceptionType.c_str());
214  }
215 
216  protected:
219 
222 
225  };
226 
229 
231  #define GENERIC_EXCEPTION GENICAM_NAMESPACE::ExceptionReporter<GENICAM_NAMESPACE::GenericException>(__FILE__, __LINE__).Report
232 
234  #define BAD_ALLOC_EXCEPTION GENICAM_NAMESPACE::ExceptionReporter<GENICAM_NAMESPACE::BadAllocException>(__FILE__, __LINE__, "BadAllocException" ).Report
235 
237  #define INVALID_ARGUMENT_EXCEPTION GENICAM_NAMESPACE::ExceptionReporter<GENICAM_NAMESPACE::InvalidArgumentException>(__FILE__, __LINE__, "InvalidArgumentException" ).Report
238 
240  #define OUT_OF_RANGE_EXCEPTION GENICAM_NAMESPACE::ExceptionReporter<GENICAM_NAMESPACE::OutOfRangeException>(__FILE__, __LINE__, "OutOfRangeException" ).Report
241 
243  #define PROPERTY_EXCEPTION GENICAM_NAMESPACE::ExceptionReporter<GENICAM_NAMESPACE::PropertyException>(__FILE__, __LINE__, "PropertyException" ).Report
244 
246  #define RUNTIME_EXCEPTION GENICAM_NAMESPACE::ExceptionReporter<GENICAM_NAMESPACE::RuntimeException>(__FILE__, __LINE__, "RuntimeException" ).Report
247 
249  #define LOGICAL_ERROR_EXCEPTION GENICAM_NAMESPACE::ExceptionReporter<GENICAM_NAMESPACE::LogicalErrorException>(__FILE__, __LINE__, "LogicalErrorException" ).Report
250 
252  #define ACCESS_EXCEPTION GENICAM_NAMESPACE::ExceptionReporter<GENICAM_NAMESPACE::AccessException>(__FILE__, __LINE__, "AccessException" ).Report
253 
255  #define TIMEOUT_EXCEPTION GENICAM_NAMESPACE::ExceptionReporter<GENICAM_NAMESPACE::TimeoutException>(__FILE__, __LINE__,"TimeoutException" ).Report
256 
258  #define DYNAMICCAST_EXCEPTION GENICAM_NAMESPACE::ExceptionReporter<GENICAM_NAMESPACE::DynamicCastException>(__FILE__, __LINE__, "DynamicCastException" ).Report
259 
261  #define CHECK_RANGE_I64(_Value, _Min, _Max, _Inc) \
262  if((int64_t)(_Value) < (int64_t)(_Min)) \
263  throw OUT_OF_RANGE_EXCEPTION("Value = %" FMT_I64 "d must be equal or greater than Min = %" FMT_I64 "d", (int64_t)(_Value), (int64_t)(_Min)); \
264  else if((int64_t)(_Value) > (int64_t)(_Max)) \
265  throw OUT_OF_RANGE_EXCEPTION("Value = %" FMT_I64 "d must be equal or smaller than Max = %" FMT_I64 "d", (int64_t)(_Value), (int64_t)(_Max)); \
266  else if ( 0 == _Inc ) \
267  throw LOGICAL_ERROR_EXCEPTION("Increment must not equal 0!"); \
268  else if( ((int64_t)(_Value) - (int64_t)(_Min)) % (int64_t)(_Inc) != 0) \
269  throw OUT_OF_RANGE_EXCEPTION("The difference between Value = %" FMT_I64 "d and Min = %" FMT_I64 "d must be dividable without rest by Inc = %" FMT_I64 "d", (int64_t)(_Value), (int64_t)(_Min), (int64_t)(_Inc));
270 
272  #define CHECK_RANGE_FLT(_Value, _Min, _Max) \
273  if ((_Value) < (_Min)) \
274  throw OUT_OF_RANGE_EXCEPTION( "Value %f must be greater than or equal %f", (_Value), (_Min) ); \
275  else if ((_Value) > (_Max)) \
276  throw OUT_OF_RANGE_EXCEPTION( "Value %f must be smaller than or equal %f", (_Value), (_Max) );
277 
279  #define CHECK_DYNAMIC_CAST_POINTER( _Pointer )\
280  assert( (_Pointer) != NULL ); \
281  if (NULL == (_Pointer)) throw LOGICAL_ERROR_EXCEPTION( "Unexpected type in dynamic cast" )
282 
284 
285 }
286 
287 #pragma pack(pop)
288 
289 #endif // GENCAM_EXCEPTION_H_
GENICAM_NAMESPACE::gcstring m_SourceFileName
the path to the source file where the exception is thrown
Definition: GCException.h:218
printf like creation of exceptions
Definition: GCException.h:167
#define DECLARE_EXCEPTION(name)
Definition: GCException.h:119
Definition: GCArray.h:33
A string class which is a clone of std::string.
Definition: GCString.h:50
Portable string implementation.
int m_SourceLine
The line within the source file where the exception is thrown.
Definition: GCException.h:221
GenICam&#39;s exception class.
Definition: GCException.h:62
GENICAM_NAMESPACE::gcstring m_ExceptionType
the type of the exception in string
Definition: GCException.h:224
Platform-dependent type definitions.