1 /*
2     Copyright (c) 2008-2009 NetAllied Systems GmbH
3 
4 	This file is part of COLLADABaseUtils.
5 
6     Licensed under the MIT Open Source License,
7     for details please see LICENSE file or the website
8     http://www.opensource.org/licenses/mit-license.php
9 */
10 
11 #ifndef __COLLADABU_EXCEPTION_H__
12 #define __COLLADABU_EXCEPTION_H__
13 
14 #include "COLLADABUPrerequisites.h"
15 #include "COLLADABUStableHeaders.h"
16 
17 #include <iostream>
18 
19 
20 namespace COLLADABU
21 {
22 
23     /** Class that is thrown by the base utils classes if something goes wrong. */
24     class Exception
25     {
26 
27 	public:
28 
29 		enum Type
30 		{
31             ERROR_TYPE_UNKNOWN,
32 			ERROR_FILE_OPEN,
33 			ERROR_SET_BUFFER,
34 			ERROR_UTF8_2_WIDE,
35 			ERROR_WIDE_2_UTF8,
36 			ERROR_NATIVE_2_WIDE,
37 			ERROR_WIDE_2_NATIVE
38 		};
39 
40 	protected:
41 
42         /** The type of the exception. */
43 		Type mExceptionType;
44 
45         /** The error message for output. */
46         String mMessage;
47 
48     public:
49 
50         /** Constructor. Creates an exception of unknown type with the given message. */
Exception(const String & message)51         Exception ( const String& message )
52             : mExceptionType ( ERROR_TYPE_UNKNOWN )
53             , mMessage ( message )
54         {}
55 
56         /** Constructor. Creates an exception of type @a type with the given message. */
Exception(Type exceptionType,const String & message)57 		Exception ( Type exceptionType, const String& message )
58         : mExceptionType ( exceptionType )
59         , mMessage ( message )
60         {}
61 
62         /** Constructor. */
Exception(Type exceptionType,const String file,const long line,const String message)63         Exception ( Type exceptionType, const String file, const long line, const String message )
64             : mExceptionType ( exceptionType )
65         {
66             std::ostringstream stream;
67             stream << file << ":" << line << ": " << message;
68             mMessage = stream.str ().c_str ();
69         }
70 
71         /** Constructor. */
Exception(const String file,const long line,const String message)72         Exception ( const String file, const long line, const String message )
73             : mExceptionType ( ERROR_TYPE_UNKNOWN )
74         {
75             std::ostringstream stream;
76             stream << file << ":" << line << ": " << message;
77             mMessage = stream.str ().c_str ();
78         }
79 
80         /** Destructor. */
~Exception()81         virtual ~Exception () {}
82 
83 		/** Returns the type of the exception*/
getType()84 		Type getType()const {return mExceptionType;}
85 
86 		/** Returns the text, describing the exception.*/
getMessage()87 		String getMessage()const {return mMessage;};
88 
89         /** Print the massage in the standard error output. */
printMessage()90         void printMessage ()
91         {
92             if ( mExceptionType == ERROR_TYPE_UNKNOWN )
93                 std::cerr << "MainException: " << mMessage << std::endl;
94             else
95                 std::cerr << "MainException: " << mMessage << ", Error type " << mExceptionType << std::endl;
96         }
97 
98 
99 	};
100 
101 } //namespace COLLADABU
102 
103 
104 #endif //__COLLADABU_EXCEPTION_H__
105