2 -----------------------------------------------------------------------------
3 This source file is part of VideoTexture library
5 Copyright (c) 2006 The Zdeno Ash Miklas
7 This program is free software; you can redistribute it and/or modify it under
8 the terms of the GNU Lesser General Public License as published by the Free Software
9 Foundation; either version 2 of the License, or (at your option) any later
12 This program is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License along with
17 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
18 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
19 http://www.gnu.org/copyleft/lesser.txt.
20 -----------------------------------------------------------------------------
27 #include <PyObjectPlus.h>
29 #include "Exception.h"
32 // exception identificators
33 ExceptionID ErrGeneral, ErrNotFound;
35 // exception descriptions
36 ExpDesc errGenerDesc (ErrGeneral, "General Error");
37 ExpDesc errNFoundDesc (ErrNotFound, "Error description not found");
41 // implementation of ExpDesc
44 ExpDesc::ExpDesc (ExceptionID & exp, char * desc, RESULT hres)
45 : m_expID(exp), m_hRslt(hres), m_description(desc)
50 ExpDesc::~ExpDesc (void) {}
52 // list of descriptions
53 std::vector<ExpDesc*> ExpDesc::m_expDescs;
59 // last exception description
60 std::string Exception::m_lastError;
63 char * Exception::m_logFile = NULL;
67 Exception::Exception ()
70 m_expID = &ErrNotFound;
77 Exception::~Exception () throw() { }
81 Exception::Exception (const Exception & xpt)
85 // assignment operator
86 Exception & Exception::operator= (const Exception & xpt)
87 { copy (xpt); return *this; }
90 // get exception description
91 const char * Exception::what()
93 // set exception description
96 return m_desc.c_str();
100 // debug version - with file and line of exception
101 Exception::Exception (ExceptionID & expID, RESULT rslt, char * fil, int lin)
102 : m_expID (&expID), m_hRslt (rslt)
105 if (strlen(fil) > 0 || lin > 0)
106 setFileLine (fil, lin);
111 void Exception::setFileLine (char * fil, int lin)
113 if (fil != NULL) m_fileName = fil;
119 void Exception::report(void)
121 // set exception description
124 PyErr_SetString(PyExc_RuntimeError, what());
125 // if log file is set
126 if (m_logFile != NULL)
128 // write description to log
129 std::ofstream logf (m_logFile, std::ios_base::app);
130 logf << m_fileName << ':' << m_line << ':' << m_desc << std::endl;
137 // set exception description
138 void Exception::setXptDesc (void)
140 // if description is not set
141 if (m_desc.size() == 0)
143 // start of search -1
144 // found description "NotFound" 0
145 // found description without matching result 1
146 // found description with matching result 2
148 // find exception description
149 for (std::vector<ExpDesc*>::iterator it = ExpDesc::m_expDescs.begin(); it != ExpDesc::m_expDescs.end(); ++it)
151 // use "NotFound", if there is not better
152 if (best < 0 && (*it)->isExp(&ErrNotFound) > 0)
154 (*it)->loadDesc(m_desc);
158 int nBest = (*it)->isExp(m_expID, m_hRslt);
159 // if exception is matching better
160 if (nBest > 0 && best < nBest)
163 (*it)->loadDesc(m_desc);
165 // if matching exactly, finish search
166 if (best == 2) break;
170 // length of result code
171 const size_t rsltSize = 11;
172 // delimit description
173 //const char delimRslt[] = ": ";
174 // set text of description
175 char rsltTxt[rsltSize];
176 std::ostringstream os;
177 os << std::hex << m_hRslt << ": " << '\0';
178 // copy result to description
179 m_desc.insert(0, rsltTxt);
180 // copy exception description to last exception string
181 m_lastError = m_desc;
186 // copy exception data
187 void Exception::copy (const Exception & xpt)
190 m_expID = xpt.m_expID;
191 m_hRslt = xpt.m_hRslt;
195 m_fileName = xpt.m_fileName;
199 void registerAllExceptions(void)
201 errGenerDesc.registerDesc();
202 errNFoundDesc.registerDesc();
203 MaterialNotAvailDesc.registerDesc();
204 ImageSizesNotMatchDesc.registerDesc();
205 SceneInvalidDesc.registerDesc();
206 CameraInvalidDesc.registerDesc();
207 ObserverInvalidDesc.registerDesc();
208 MirrorInvalidDesc.registerDesc();
209 MirrorSizeInvalidDesc.registerDesc();
210 MirrorNormalInvalidDesc.registerDesc();
211 MirrorHorizontalDesc.registerDesc();
212 MirrorTooSmallDesc.registerDesc();
213 SourceVideoEmptyDesc.registerDesc();
214 SourceVideoCreationDesc.registerDesc();