Skip to content

Commit 8ce969f

Browse files
committed
Basic application classes
1 parent 182c6a6 commit 8ce969f

29 files changed

Lines changed: 997 additions & 165 deletions

inc/mx.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "mx/sysdefs.h"
3535
#include "mx/types.h"
3636

37+
#include "mx/debug.h"
3738
#include "mx/malloc.h"
3839

3940

inc/mx/App/App.hpp

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
/*
2+
Copyright (C) 1998-2007 Emil Maskovsky
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13+
implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
18+
19+
/**
20+
@file
21+
22+
Application base classes (interface).
23+
24+
@author Emil Maskovsky
25+
*/
26+
27+
28+
#ifndef MXCPP_APPLICATION_HPP_INCLUDE_GUARD
29+
#define MXCPP_APPLICATION_HPP_INCLUDE_GUARD
30+
31+
32+
#include "mx/sysdefs.h"
33+
34+
#include "mx/types.h"
35+
36+
#include "mx/debug.h"
37+
38+
#include "mx/Except.hpp"
39+
40+
41+
/**
42+
@def MX_IMPLEMENT_APP
43+
44+
Implement main %application (platform independent).
45+
46+
Usage:
47+
@code
48+
class MyApplication: public mx::Application::Application {
49+
...
50+
}
51+
...
52+
MX_IMPLEMENT_APP(MyApplication)
53+
...
54+
@endcode
55+
56+
@param ApplicationMainClass The main %application class.
57+
58+
@warning
59+
Must be present out of all namespaces, otherwise it might be compiled
60+
as C++ extern on some compilers (actualy known BCC32 issue).
61+
*/
62+
// Command line parameters not supported in this demo.
63+
#define MX_IMPLEMENT_APP(ApplicationMainClass) \
64+
\
65+
extern "C" \
66+
int main(const int /* argc */, const char ** /* argv */) \
67+
{ \
68+
mx::TestApp theApplication/*(argc, argv)*/; \
69+
/* Make sure, that we run mx::Application::Run() and not the Run() \
70+
potentialy overridden by some inherited application class. \
71+
*/ \
72+
return mx::Application::getApp()->Run(); \
73+
}
74+
75+
76+
namespace mx
77+
{
78+
79+
80+
/**
81+
Application related exception.
82+
83+
This class serves as a base class for any application exceptions. The
84+
application is expected to use this and no other class as the base class
85+
for it's own exceptions.
86+
87+
@note
88+
Exception of this type itself should never be thrown. Always throw
89+
one of the more specific types.
90+
*/
91+
class MXCPP_DLL_EXPORT ApplicationException
92+
: public Exception
93+
{
94+
95+
MX_DECLARE_EXCEPTION_CLASS(ApplicationException, Exception);
96+
97+
// Construction, destruction.
98+
99+
protected:
100+
101+
// Protected constructor to prevent direct throwing of the exception.
102+
MX_INLINE ApplicationException(const char * sMessage = NULL);
103+
104+
105+
}; // class ApplicationException
106+
107+
108+
/**
109+
Base class of an application.
110+
*/
111+
class MXCPP_DLL_EXPORT Application
112+
: public Class
113+
{
114+
115+
MX_CLASS_NO_COPY(Application);
116+
MX_CLASS_NO_ASSIGNMENT(Application);
117+
118+
// Types, constants.
119+
120+
public:
121+
122+
/**
123+
Application return code.
124+
*/
125+
typedef enum
126+
{
127+
128+
/// Application terminated as a result of an internal error.
129+
RC_INTERNAL_ERROR = -1,
130+
131+
/// Application exit withour errors.
132+
RC_SUCCESS = 0,
133+
134+
/// Application exit with general error.
135+
RC_FAILURE = 1,
136+
137+
} ReturnCode;
138+
139+
140+
// Class methods (static).
141+
142+
public:
143+
144+
static MX_INLINE Application * getApp();
145+
146+
147+
// Class attributes (static).
148+
149+
private:
150+
151+
static Application * pApplicationInstance;
152+
153+
154+
// Construction, destruction.
155+
156+
protected:
157+
158+
Application();
159+
160+
virtual ~Application();
161+
162+
163+
// Class instance methods.
164+
165+
public:
166+
167+
int Run();
168+
169+
private:
170+
171+
// Only application main functions will be allowed to run the application.
172+
friend int main(const int, const char **);
173+
174+
// Initialize() function is private to prevent it's override.
175+
// You should not override it.
176+
bool Initialize();
177+
178+
protected:
179+
180+
// Override to provide custom initialization.
181+
virtual bool OnInit();
182+
183+
// Override to provide custom run functionality.
184+
virtual ReturnCode OnRun();
185+
186+
187+
// Class instance attributes.
188+
189+
private:
190+
191+
}; // class Application
192+
193+
194+
} // namespace mx
195+
196+
197+
// Define inline methods here if inlining is enabled.
198+
#ifdef MX_INLINE_ENABLED
199+
#include "mx/App/App.inl"
200+
#endif
201+
202+
#endif // MXCPP_APPLICATION_HPP_INCLUDE_GUARD
203+
204+
/* EOF */

inc/mx/App/App.inl

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Copyright (C) 1998-2007 Emil Maskovsky
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13+
implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
18+
19+
/**
20+
@file
21+
22+
Application base classes (inline methods).
23+
24+
@author Emil Maskovsky
25+
*/
26+
27+
28+
/**
29+
Constructor.
30+
31+
@param [in] sMessage The exception message.
32+
*/
33+
MX_INLINE mx::ApplicationException::ApplicationException(
34+
const char * const sMessage)
35+
: Super(sMessage)
36+
{}
37+
38+
39+
/**
40+
Returns the application instance.
41+
*/
42+
/* static */ MX_INLINE mx::Application * mx::Application::getApp()
43+
{
44+
mxAssert(pApplicationInstance != NULL);
45+
return pApplicationInstance;
46+
}
47+
48+
49+
/* EOF */

inc/mx/Except.hpp

Lines changed: 6 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ class MXCPP_DLL_EXPORT Exception
150150

151151
// Class methods (static).
152152

153+
public:
154+
155+
static MX_NORETURN HandleUncaughtException(
156+
const Exception * const pException = NULL);
157+
153158
private:
154159

155160
static MX_INLINE void setLastRaisedException(
@@ -191,7 +196,7 @@ class MXCPP_DLL_EXPORT Exception
191196

192197
int WriteMessage(FILE * const stream) const;
193198

194-
MX_NORETURN Fail() const;
199+
MX_NORETURN Fail(const char * const sMessage = NULL) const;
195200

196201
protected:
197202

@@ -288,34 +293,6 @@ MX_NORETURN ThrowException(
288293

289294
// Derived exceptions.
290295

291-
/**
292-
Application related exception.
293-
294-
This class serves as a base class for any application exceptions. The
295-
application is expected to use this and no other class as the base class
296-
for it's own exceptions.
297-
298-
@note
299-
Exception of this type itself should never be thrown. Always throw
300-
one of the more specific types.
301-
*/
302-
class MXCPP_DLL_EXPORT ApplicationException
303-
: public Exception
304-
{
305-
306-
MX_DECLARE_EXCEPTION_CLASS(ApplicationException, Exception);
307-
308-
// Construction, destruction.
309-
310-
protected:
311-
312-
// Protected constructor to prevent direct throwing of the exception.
313-
MX_INLINE ApplicationException(const char * sMessage = NULL);
314-
315-
316-
}; // class ApplicationException
317-
318-
319296
/**
320297
System related exception.
321298
@@ -377,35 +354,6 @@ class MXCPP_DLL_EXPORT KernelException
377354
}; // class KernelException
378355

379356

380-
/**
381-
Memory related exception.
382-
383-
This class serves as a base class for any memory related exceptions.
384-
This class is supposed to be used in declaration of function which throw
385-
either of these more specific exceptions. It can also be used in catch
386-
blocks for catching either of these more specific exceptions.
387-
388-
@note
389-
Exception of this type itself should never be thrown. Always throw
390-
one of the more specific types.
391-
*/
392-
class MXCPP_DLL_EXPORT MemoryException
393-
: public KernelException
394-
{
395-
396-
MX_DECLARE_EXCEPTION_CLASS(MemoryException, KernelException);
397-
398-
// Construction, destruction.
399-
400-
protected:
401-
402-
// Protected constructor to prevent direct throwing of the exception.
403-
MX_INLINE MemoryException(const char * sMessage = NULL);
404-
405-
406-
}; // class MemoryException
407-
408-
409357
class MXCPP_DLL_EXPORT StreamException
410358
: public KernelException
411359
{

inc/mx/Except.inl

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,6 @@ MX_INLINE const char * mx::Exception::message() const
7474
}
7575

7676

77-
/**
78-
Constructor.
79-
80-
@param [in] sMessage The exception message.
81-
*/
82-
MX_INLINE mx::ApplicationException::ApplicationException(
83-
const char * const sMessage)
84-
: Super(sMessage)
85-
{}
86-
87-
8877
/**
8978
Constructor.
9079
@@ -107,17 +96,6 @@ MX_INLINE mx::KernelException::KernelException(
10796
{}
10897

10998

110-
/**
111-
Constructor.
112-
113-
@param [in] sMessage The exception message.
114-
*/
115-
MX_INLINE mx::MemoryException::MemoryException(
116-
const char * const sMessage)
117-
: Super(sMessage)
118-
{}
119-
120-
12199
/**
122100
Constructor.
123101

0 commit comments

Comments
 (0)