0026377: Passing Handle objects as arguments to functions as non-const reference...
[occt.git] / src / Standard / Standard_OutOfMemory.cxx
1 // Created on: 2016-01-06
2 // Created by: Andrey Betenev
3 // Copyright (c) 2016 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <Standard_OutOfMemory.hxx>
17
18 #include <algorithm>
19 #include <stdlib.h>
20
21 IMPLEMENT_STANDARD_RTTIEXT(Standard_OutOfMemory,Standard_ProgramError)
22
23 //=======================================================================
24 //function : Standard_OutOfMemory
25 //purpose  :
26 //=======================================================================
27
28 Standard_OutOfMemory::Standard_OutOfMemory(const Standard_CString theMessage)
29 {
30   // call explicitly own method (non-virtual call)
31   Standard_OutOfMemory::SetMessageString (theMessage);
32 }
33
34 //=======================================================================
35 //function : GetMessageString
36 //purpose  :
37 //=======================================================================
38
39 Standard_CString Standard_OutOfMemory::GetMessageString() const
40 {
41   return myBuffer;
42 }
43   
44 //=======================================================================
45 //function : SetMessageString
46 //purpose  :
47 //=======================================================================
48
49 void Standard_OutOfMemory::SetMessageString (const Standard_CString theMessage)
50 {
51   // restrict length of the message by buffer size
52   size_t n = (theMessage ? std::min (strlen (theMessage), sizeof(myBuffer) - 1) : 0);
53
54   // first set line end symbol to be safe in case of concurrent call
55   myBuffer[n] = '\0';
56   if (n > 0)
57     memcpy (myBuffer, theMessage, n);
58 }
59
60 //=======================================================================
61 //function : Raise
62 //purpose  :
63 //=======================================================================
64
65 void Standard_OutOfMemory::Raise(const Standard_CString theMessage)
66 {
67   NewInstance(theMessage)->Reraise();
68 }
69
70 //=======================================================================
71 //function : Raise
72 //purpose  :
73 //=======================================================================
74
75 void Standard_OutOfMemory::Raise(Standard_SStream& theMessage)
76 {
77   NewInstance(theMessage.str().c_str())->Reraise();
78 }
79
80 //=======================================================================
81 //function : NewInstance
82 //purpose  :
83 //=======================================================================
84
85 // global instance must be allocated at load-time
86 static Handle(Standard_OutOfMemory) anOutOfMemInstance = new Standard_OutOfMemory;
87
88 Handle(Standard_OutOfMemory) Standard_OutOfMemory::NewInstance(const Standard_CString theMessage)
89 {
90   anOutOfMemInstance->SetMessageString (theMessage);
91   return anOutOfMemInstance;
92 }
93
94 //=======================================================================
95 //function : Throw
96 //purpose  :
97 //=======================================================================
98
99 void Standard_OutOfMemory::Throw () const
100 {
101   throw *this;
102 }