0031687: Draw Harness, ViewerTest - extend command vrenderparams with option updating...
[occt.git] / src / Standard / Standard_Failure.cxx
CommitLineData
b311480e 1// Copyright (c) 1998-1999 Matra Datavision
973c2be1 2// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 3//
973c2be1 4// This file is part of Open CASCADE Technology software library.
b311480e 5//
d5f74e42 6// This library is free software; you can redistribute it and/or modify it under
7// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 8// by the Free Software Foundation, with special exception defined in the file
9// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10// distribution for complete text of the license and disclaimer of any warranty.
b311480e 11//
973c2be1 12// Alternatively, this file may be used under the terms of Open CASCADE
13// commercial license or contractual agreement.
7fd59977 14
42cf5bc1 15
7fd59977 16#include <Standard_ErrorHandler.hxx>
42cf5bc1 17#include <Standard_Failure.hxx>
7fd59977 18#include <Standard_Macro.hxx>
42cf5bc1 19#include <Standard_NoSuchObject.hxx>
7fd59977 20#include <Standard_PCharacter.hxx>
42cf5bc1 21#include <Standard_Type.hxx>
22#include <Standard_TypeMismatch.hxx>
7fd59977 23
42cf5bc1 24#include <string.h>
92efcf78 25IMPLEMENT_STANDARD_RTTIEXT(Standard_Failure,Standard_Transient)
26
7fd59977 27static Standard_CString allocate_message(const Standard_CString AString)
28{
29 Standard_CString aStr = 0;
30 if(AString) {
31 const Standard_Size aLen = strlen(AString);
a73267f2 32 aStr = (Standard_CString) malloc(aLen+sizeof(Standard_Integer)+1);
33 if (aStr) {
34 Standard_PCharacter pStr=(Standard_PCharacter)aStr;
35 strcpy(pStr+sizeof(Standard_Integer),AString);
36 *((Standard_Integer*)aStr) = 1;
37 }
7fd59977 38 }
39 return aStr;
7fd59977 40}
41
42static Standard_CString copy_message(Standard_CString aMessage)
43{
44 Standard_CString aStr = 0;
45 if(aMessage) {
46 aStr = aMessage;
47 (*((Standard_Integer*)aStr))++;
48 }
49 return aStr;
50}
51
52static void deallocate_message(Standard_CString aMessage)
53{
54 if(aMessage) {
55 (*((Standard_Integer*)aMessage))--;
56 if(*((Standard_Integer*)aMessage)==0)
a73267f2 57 free((void*)aMessage);
7fd59977 58 }
59}
60
7fd59977 61// ******************************************************************
62// Standard_Failure *
63// ******************************************************************
b3d20c7f 64static Standard_THREADLOCAL Handle(Standard_Failure) RaisedError;
9775fa61 65
7fd59977 66// ------------------------------------------------------------------
67//
68// ------------------------------------------------------------------
69Standard_Failure::Standard_Failure ()
70: myMessage(NULL)
71{
72}
73
74// ------------------------------------------------------------------
75// Create returns mutable Failure;
76// ------------------------------------------------------------------
77Standard_Failure::Standard_Failure (const Standard_CString AString)
78: myMessage(NULL)
79{
80 myMessage = allocate_message(AString);
81}
82
a9b30f0a 83Standard_Failure::Standard_Failure (const Standard_Failure& theFailure)
84: Standard_Transient(theFailure)
7fd59977 85{
a9b30f0a 86 myMessage = copy_message(theFailure.myMessage);
7fd59977 87}
88
4db4247a 89Standard_Failure::~Standard_Failure()
7fd59977 90{
91 deallocate_message(myMessage);
92}
93
94void Standard_Failure::SetMessageString(const Standard_CString AString)
95{
96 if ( AString == GetMessageString() ) return;
97 deallocate_message(myMessage);
98 myMessage = allocate_message(AString);
99}
100
101// ------------------------------------------------------------------
102// Caught (myclass) returns mutable Failure raises NoSuchObject ;
103// ------------------------------------------------------------------
104Handle(Standard_Failure) Standard_Failure::Caught()
105{
7fd59977 106 return RaisedError ;
7fd59977 107}
108
109// ------------------------------------------------------------------
110// Raise (myclass; aMessage: CString = "") ;
111// ------------------------------------------------------------------
112void Standard_Failure::Raise (const Standard_CString AString)
113{
114 Handle(Standard_Failure) E = new Standard_Failure() ;
115 E->Reraise (AString) ;
116}
117
118// ------------------------------------------------------------------
119// Raise(myclass; aReason: in out SStream) ;
120// ------------------------------------------------------------------
121void Standard_Failure::Raise (const Standard_SStream& AReason)
122{
123 Handle(Standard_Failure) E = new Standard_Failure();
124 E->Reraise (AReason);
125}
126
127// ------------------------------------------------------------------
128// Reraise (me: mutable; aMessage: CString) ;
129// ------------------------------------------------------------------
130void Standard_Failure::Reraise (const Standard_CString AString)
131{
132 SetMessageString(AString);
133 Reraise();
134}
135
136void Standard_Failure::Reraise (const Standard_SStream& AReason)
137{
7fd59977 138 SetMessageString(AReason.str().c_str());
7fd59977 139 Reraise();
140}
141
142void Standard_Failure::Reraise ()
143{
7fd59977 144 RaisedError = this;
145 Throw();
7fd59977 146}
147
69ff08ff 148void Standard_Failure::Jump()
7fd59977 149{
9775fa61 150#if defined (OCC_CONVERT_SIGNALS)
a01039b9 151 Standard_ErrorHandler::Error (this);
152 Standard_ErrorHandler::Abort (this);
7fd59977 153#else
154 RaisedError = this;
155 Throw();
156#endif
157}
158
159
160// ------------------------------------------------------------------
161// Throw (me) is virtual ;
162// ------------------------------------------------------------------
163void Standard_Failure::Throw() const
164{
7fd59977 165 throw *this;
7fd59977 166}
167
168// ------------------------------------------------------------------
169// Print (me; s: in out OStream) returns OStream;
170// ------------------------------------------------------------------
171void Standard_Failure::Print (Standard_OStream& AStream) const
172{
173if(myMessage){
174 AStream << DynamicType() << ": " << GetMessageString();
175 }
176 else {
177 AStream << DynamicType();
178 }
179}
180
181Handle(Standard_Failure) Standard_Failure::NewInstance(const Standard_CString AString)
182{
183 return new Standard_Failure(AString) ;
184}
4db4247a 185
186//=======================================================================
187//function : GetMessageString
188//purpose : Returns error message
189//=======================================================================
190Standard_CString Standard_Failure::GetMessageString () const
191{
192 return (myMessage ? myMessage+sizeof(Standard_Integer) : "");
193}
194