|
| 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 */ |
0 commit comments