OWLNext    7.0
Borland's Object Windows Library for the modern age
Loading...
Searching...
No Matches
exbase.cpp
Go to the documentation of this file.
1//----------------------------------------------------------------------------
2// Borland WinSys Library
3// Copyright (c) 1994, 1996 by Borland International, All rights
4//
5/// \file
6/// TXBase class implementation.
7//----------------------------------------------------------------------------
8#include <owl/pch.h>
10#include <owl/exbase.h>
11
12#if defined(__BORLANDC__)
13# pragma option -w-ccc // Disable "Condition is always true/false"
14#endif
15
16namespace owl {
17
18//------------------------------------------------------------------------------
19
20// Local namespace for UTF conversion
21
22namespace {
23
24 /// Uniform adapter to Windows API for UTF conversion
25 /// Returns the number of characters written to the buffer, including any null-terminator.
26
29
30 /// Uniform adapter to Windows API for UTF conversion
31 /// Returns the number of characters written to the buffer, including any null-terminator.
32
35
36
37 /// Converter function
38 /// Passed converter can be utf16_to_utf8 or utf8_to_utf16.
39
40 template <class Result, class Source, class Converter>
41 Result convert(const Source& source, Converter* const converter, const DWORD flags = 0)
42 {
43 Result result;
44 if (!source.empty())
45 {
46 // Get the size, in characters, of the buffer needed for the translated string,
47 // including the null-terminator.
48
49 int n = (*converter)(flags, &source[0], -1, nullptr, 0);
50 CHECK(n >= 0);
51 WARN(n == 0, _T("String conversion sizing failed, GetLastError() == ") << GetLastError());
52
53 // Translate the string if the size query was successful.
54
55 if (n != 0)
56 {
57 result.resize(n); // Reserve room, including explicit null-terminator.
58 int r = (*converter)(flags, &source[0], -1, &result[0], n);
59 CHECK(r >= 0);
60 WARN(r == 0, _T("String conversion failed, GetLastError() == ") << GetLastError());
61 result.erase(r > 0 ? r - 1 : 0); // Remove the explicit null-terminator, or all if conversion failed.
62 }
63 }
64 return result;
65 }
66
67
68 // Typedefs for string types.
69 // If support is needed for non-standard string types then add alternatives here using
70 // preprocessor directives for conditional compilation.
71
72 typedef std::string narrow_string;
73 typedef std::wstring wide_string;
74
75
76 /// Uniform interface function
77
79 {
81 }
82
83
84 /// Uniform interface function overload - NOP
85
87 {
88 return s;
89 }
90
91
92 /// Uniform interface functor
93 /// Assumes the source is encoded in UTF-8 format.
94
95 template <class Result>
96 struct ConverterFromNarrowString
97 {
98 inline Result operator () (const narrow_string& s_utf8)
100 };
101
102
103 /// Uniform interface functor specialization - NOP
104
105 template <>
106 struct ConverterFromNarrowString<narrow_string>
107 {
108 inline const narrow_string& operator () (const narrow_string& s)
109 {return s;}
110 };
111
112
113} // namespace
114
115//------------------------------------------------------------------------------
116
117//
118/// Calls the xmsg class's constructor that takes a string parameter and initializes
119/// xmsg with the value of the string parameter.
120//
122 : std::exception(), str(convert_to_narrow_string(msg))
123{}
124
125//
126/// Creates a copy of the TXBase object passed in the TXBase parameter.
127//
129: std::exception(), str(src.str)
130{}
131
134
135/// Makes a copy of the exception object.
137{
138 return new TXBase(*this);
139}
140
141/// Throws the exception object.
143{
144 throw *this;
145}
146
147//
148/// Constructs a TXBase exception from scratch, and throws it.
149//
151{
152 TXBase(msg).Throw();
153}
154
155const char* TXBase::what() const noexcept
156{
157 return str.c_str ();
158}
159
161{
162 if (this != &src)
163 {
164 str = src.str;
165 }
166 return(*this);
167}
168
173
174//------------------------------------------------------------------------------
175
176} // OWL namespace
177
Diagnostic macros for assertions and tracing.
#define CHECK(condition)
Definition checks.h:239
#define WARN(condition, message)
Definition checks.h:273
Derived from xmsg, TXBase is the base class for ObjectWindows and ObjectComponents exception-handling...
Definition exbase.h:41
virtual ~TXBase()
Definition exbase.cpp:132
TXBase(const tstring &msg)
Calls the xmsg class's constructor that takes a string parameter and initializes xmsg with the value ...
Definition exbase.cpp:121
const char * what() const noexcept
Definition exbase.cpp:155
virtual TXBase * Clone()
Makes a copy of the exception object.
Definition exbase.cpp:136
tstring why() const
Definition exbase.cpp:169
static void Raise(const tstring &msg)
Constructs a TXBase exception from scratch, and throws it.
Definition exbase.cpp:150
virtual void Throw()
Throws the exception object.
Definition exbase.cpp:142
std::string str
Definition exbase.h:59
TXBase &_RTLENTRY operator=(const TXBase &src)
Definition exbase.cpp:160
#define _T(x)
Definition cygwin.h:51
Base exception support for framework exceptions.
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
std::string tstring
Definition defs.h:79