[249] | 1 |
|
---|
| 2 | /*
|
---|
| 3 | *@@sourcefile bs_base.cpp:
|
---|
| 4 | * implements the BSRoot class, which was added
|
---|
| 5 | * with V0.9.14.
|
---|
| 6 | *
|
---|
[254] | 7 | *@@header "cppbase\bs_base.h"
|
---|
| 8 | *@@header "cppbase\bs_list.h"
|
---|
| 9 | *@@header "cppbase\bs_errors.h"
|
---|
[249] | 10 | *@@added V0.9.14 (2001-07-12) [umoeller]
|
---|
| 11 | */
|
---|
| 12 |
|
---|
| 13 | /*
|
---|
| 14 | * This file Copyright (C) 2001 Ulrich Mller.
|
---|
| 15 | * This program is free software; you can redistribute it and/or modify
|
---|
| 16 | * it under the terms of the GNU General Public License as published by
|
---|
| 17 | * the Free Software Foundation, in version 2 as it comes in the COPYING
|
---|
| 18 | * file of this distribution.
|
---|
| 19 | * This program is distributed in the hope that it will be useful,
|
---|
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 22 | * GNU General Public License for more details.
|
---|
| 23 | */
|
---|
| 24 |
|
---|
| 25 | #define OS2EMX_PLAIN_CHAR
|
---|
| 26 | // this is needed for "os2emx.h"; if this is defined,
|
---|
| 27 | // emx will define PSZ as _signed_ char, otherwise
|
---|
| 28 | // as unsigned char
|
---|
| 29 |
|
---|
| 30 | #define INCL_DOSSEMAPHORES
|
---|
| 31 | #include <os2.h>
|
---|
| 32 |
|
---|
| 33 | #include <stdio.h>
|
---|
| 34 | #include <stdlib.h>
|
---|
| 35 | #include <string.h>
|
---|
| 36 | #include <stdarg.h>
|
---|
| 37 |
|
---|
| 38 | #include "setup.h"
|
---|
| 39 |
|
---|
| 40 | // base includes
|
---|
| 41 | #include "cppbase\bs_base.h"
|
---|
| 42 |
|
---|
| 43 | #pragma hdrstop
|
---|
| 44 |
|
---|
| 45 | /* ******************************************************************
|
---|
| 46 | *
|
---|
| 47 | * Root classes
|
---|
| 48 | *
|
---|
| 49 | ********************************************************************/
|
---|
| 50 |
|
---|
| 51 | // class ID of BSRoot, which is the ONLY class ID
|
---|
| 52 | // whose parent is NULL
|
---|
| 53 | BSClassID BSRoot::tBSRoot("BSRoot", NULL);
|
---|
| 54 |
|
---|
| 55 | #ifdef __DEBUG__
|
---|
| 56 | FILE* G_DebugLogFile = NULL;
|
---|
| 57 |
|
---|
| 58 | /*
|
---|
| 59 | *@@ OpenDebugLog:
|
---|
| 60 | *
|
---|
| 61 | */
|
---|
| 62 |
|
---|
| 63 | void BSRoot::OpenDebugLog(const char *pcszDir)
|
---|
| 64 | {
|
---|
| 65 | if (!G_DebugLogFile)
|
---|
| 66 | {
|
---|
| 67 | CHAR sz[256];
|
---|
| 68 | strcpy(sz, pcszDir);
|
---|
| 69 | strcat(sz, "\\");
|
---|
| 70 | strcat(sz, "debug.log");
|
---|
| 71 | G_DebugLogFile = fopen(sz, "a");
|
---|
| 72 | fprintf(G_DebugLogFile, "\n\nDebug log opened\n\n");
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | /*
|
---|
| 77 | *@@ WriteToDebugLog:
|
---|
| 78 | *
|
---|
| 79 | */
|
---|
| 80 |
|
---|
| 81 | void BSRoot::WriteToDebugLog(const char *pcszFormat, ...)
|
---|
| 82 | {
|
---|
| 83 | if (G_DebugLogFile)
|
---|
| 84 | {
|
---|
| 85 | va_list args;
|
---|
| 86 | va_start(args, pcszFormat);
|
---|
| 87 | vfprintf(G_DebugLogFile, pcszFormat, args);
|
---|
| 88 | va_end(args);
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 | #endif
|
---|
| 92 |
|
---|
| 93 | /*
|
---|
| 94 | *@@ BSRoot:
|
---|
| 95 | * default constructor. This is protected to
|
---|
| 96 | * make sure that derived classes won't
|
---|
| 97 | * forget to specify their class ID when
|
---|
| 98 | * constructing objects.
|
---|
| 99 | */
|
---|
| 100 |
|
---|
| 101 | BSRoot::BSRoot(BSClassID &Class)
|
---|
| 102 | : _Class(Class)
|
---|
| 103 | {
|
---|
| 104 | #ifdef __DEBUG__
|
---|
| 105 | if (G_DebugLogFile)
|
---|
| 106 | {
|
---|
| 107 | fprintf(G_DebugLogFile,
|
---|
| 108 | "Created %s at 0x%lX\n",
|
---|
| 109 | _Class._pcszClassName,
|
---|
| 110 | this);
|
---|
| 111 | fflush(G_DebugLogFile);
|
---|
| 112 | }
|
---|
| 113 | #endif
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | /*
|
---|
| 117 | *@@ BSRoot:
|
---|
| 118 | * protected copy constructor.
|
---|
| 119 | *
|
---|
| 120 | * This is only provided so that classes
|
---|
| 121 | * derived from BSRoot need not always
|
---|
| 122 | * specify a copy constructor themselves.
|
---|
| 123 | */
|
---|
| 124 |
|
---|
| 125 | BSRoot::BSRoot(const BSRoot &p)
|
---|
| 126 | : _Class(p._Class)
|
---|
| 127 | {
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | /*
|
---|
| 131 | *@@ operator=:
|
---|
| 132 | * protected assignment operator.
|
---|
| 133 | *
|
---|
| 134 | * This is only provided so that classes
|
---|
| 135 | * derived from BSRoot need not always
|
---|
| 136 | * specify an assigment operator themselves.
|
---|
| 137 | */
|
---|
| 138 |
|
---|
| 139 | BSRoot& BSRoot::operator=(const BSRoot &p)
|
---|
| 140 | {
|
---|
| 141 | _Class = p._Class;
|
---|
| 142 | return *this;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | /*
|
---|
| 146 | *@@ ~BSRoot:
|
---|
| 147 | * public virtual destructor.
|
---|
| 148 | */
|
---|
| 149 |
|
---|
| 150 | BSRoot::~BSRoot()
|
---|
| 151 | {
|
---|
| 152 | #ifdef __DEBUG__
|
---|
| 153 | if (G_DebugLogFile)
|
---|
| 154 | {
|
---|
| 155 | fprintf(G_DebugLogFile,
|
---|
| 156 | "Deleted %s at 0x%lX\n",
|
---|
| 157 | _Class._pcszClassName,
|
---|
| 158 | this);
|
---|
| 159 | fflush(G_DebugLogFile);
|
---|
| 160 | }
|
---|
| 161 | #endif
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | /*
|
---|
| 165 | *@@ IsA:
|
---|
| 166 | * returns true if the object is an instance of
|
---|
| 167 | * the given class, or if the given class is a
|
---|
| 168 | * parent class of the object's class.
|
---|
| 169 | *
|
---|
| 170 | * This is similar to the somIsA() method in SOM
|
---|
| 171 | * and can be very useful for testing whether an
|
---|
| 172 | * object supports a certain method (before doing
|
---|
| 173 | * an explicit typecast).
|
---|
| 174 | *
|
---|
| 175 | * For example, assume the following hierarchy:
|
---|
| 176 | *
|
---|
| 177 | + BSRoot
|
---|
| 178 | + +-- MyGrandParentClass
|
---|
| 179 | + +-- MyParentClass
|
---|
| 180 | + +-- MyClass
|
---|
| 181 | *
|
---|
| 182 | * Assume that "obj" is an instance of MyParentClass.
|
---|
| 183 | * Invoking obj.isA() will return:
|
---|
| 184 | *
|
---|
| 185 | + obj.isA(BSRoot::tBSRoot) == true
|
---|
| 186 | + obj.isA(MyGrandParentClass::tMyGrandParentClass) == true
|
---|
| 187 | + obj.isA(MyParentClass::tMyParentClass) == true
|
---|
| 188 | + obj.isA(MyClass::tMyClass) == false
|
---|
| 189 | */
|
---|
| 190 |
|
---|
| 191 | bool BSRoot::IsA(BSClassID &Class) const
|
---|
| 192 | {
|
---|
| 193 | BSClassID *c = &_Class;
|
---|
| 194 | while (c)
|
---|
| 195 | {
|
---|
| 196 | if (c == &Class)
|
---|
| 197 | return true;
|
---|
| 198 |
|
---|
| 199 | c = c->_pParentClass;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | return false;
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | /*
|
---|
| 206 | *@@ QueryClassName:
|
---|
| 207 | * returns the name of the class that this object
|
---|
| 208 | * is an instance of.
|
---|
| 209 | */
|
---|
| 210 |
|
---|
| 211 | const char* BSRoot::QueryClassName() const
|
---|
| 212 | {
|
---|
| 213 | return (_Class._pcszClassName);
|
---|
| 214 | }
|
---|
| 215 |
|
---|
| 216 | /* ******************************************************************
|
---|
| 217 | *
|
---|
| 218 | * Locks
|
---|
| 219 | *
|
---|
| 220 | ********************************************************************/
|
---|
| 221 |
|
---|
| 222 | /*
|
---|
| 223 | *@@ BSMutex:
|
---|
| 224 | *
|
---|
| 225 | *@@added V0.9.20 (2002-07-06) [umoeller]
|
---|
| 226 | */
|
---|
| 227 |
|
---|
| 228 | BSMutex::BSMutex()
|
---|
| 229 | {
|
---|
| 230 | DosCreateMutexSem(NULL, &_hmtx, 0, FALSE);
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | /*
|
---|
| 234 | *@@ ~BSMutex:
|
---|
| 235 | *
|
---|
| 236 | *@@added V0.9.20 (2002-07-06) [umoeller]
|
---|
| 237 | */
|
---|
| 238 |
|
---|
| 239 | BSMutex::~BSMutex()
|
---|
| 240 | {
|
---|
| 241 | // DosCloseMutexSem(_hmtx);
|
---|
| 242 | // do not close the sem, or we can't use strings
|
---|
| 243 | // during termination
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | /*
|
---|
| 247 | *@@ Request:
|
---|
| 248 | *
|
---|
| 249 | *@@added V0.9.20 (2002-07-06) [umoeller]
|
---|
| 250 | */
|
---|
| 251 |
|
---|
| 252 | int BSMutex::Request() const
|
---|
| 253 | {
|
---|
| 254 | APIRET arc;
|
---|
| 255 | if (arc = DosRequestMutexSem(_hmtx, SEM_INDEFINITE_WAIT))
|
---|
| 256 | {
|
---|
| 257 | DosBeep(1000, 100);
|
---|
| 258 | throw arc;
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | return true;
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | /*
|
---|
| 265 | *@@ Release:
|
---|
| 266 | *
|
---|
| 267 | *@@added V0.9.20 (2002-07-06) [umoeller]
|
---|
| 268 | */
|
---|
| 269 |
|
---|
| 270 | int BSMutex::Release() const
|
---|
| 271 | {
|
---|
| 272 | DosReleaseMutexSem(_hmtx);
|
---|
| 273 | return true;
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 |
|
---|