Emeric Vigier | 2f62582 | 2012-08-06 11:09:52 -0400 | [diff] [blame] | 1 | // Copyright (C) 1999-2005 Open Source Telecom Corporation. |
| 2 | // Copyright (C) 2006-2010 David Sugar, Tycho Softworks. |
| 3 | // |
| 4 | // This program is free software; you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation; either version 2 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // This program is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with this program; if not, write to the Free Software |
| 16 | // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | // |
| 18 | // As a special exception, you may use this file as part of a free software |
| 19 | // library without restriction. Specifically, if other files instantiate |
| 20 | // templates or use macros or inline functions from this file, or you compile |
| 21 | // this file and link it with other files to produce an executable, this |
| 22 | // file does not by itself cause the resulting executable to be covered by |
| 23 | // the GNU General Public License. This exception does not however |
| 24 | // invalidate any other reasons why the executable file might be covered by |
| 25 | // the GNU General Public License. |
| 26 | // |
| 27 | // This exception applies only to the code released under the name GNU |
| 28 | // Common C++. If you copy code from other releases into a copy of GNU |
| 29 | // Common C++, as the General Public License permits, the exception does |
| 30 | // not apply to the code that you add in this way. To avoid misleading |
| 31 | // anyone as to the status of such modified files, you must delete |
| 32 | // this exception notice from them. |
| 33 | // |
| 34 | // If you write modifications of your own for GNU Common C++, it is your choice |
| 35 | // whether to permit this exception to apply to your modifications. |
| 36 | // If you do not wish that, delete this exception notice. |
| 37 | // |
| 38 | |
| 39 | #include <cc++/config.h> |
| 40 | |
| 41 | #if !defined(_MSC_VER) || _MSC_VER >= 1300 |
| 42 | |
| 43 | #include <cc++/exception.h> |
| 44 | #include <cc++/export.h> |
| 45 | #include <cc++/persist.h> |
| 46 | #include "assert.h" |
| 47 | |
| 48 | #ifdef CCXX_NAMESPACES |
| 49 | namespace ost { |
| 50 | using namespace std; |
| 51 | #endif |
| 52 | |
| 53 | #ifdef CCXX_EXCEPTIONS |
| 54 | # ifndef COMMON_STD_EXCEPTION |
| 55 | |
| 56 | PersistException::PersistException(String const& reason) |
| 57 | : _what(reason) |
| 58 | {} // Nothing :) |
| 59 | |
| 60 | const String& PersistException::getString() const |
| 61 | { |
| 62 | return _what; |
| 63 | } |
| 64 | # endif |
| 65 | #endif |
| 66 | |
| 67 | const char* BaseObject::getPersistenceID() const |
| 68 | { |
| 69 | return "BaseObject"; |
| 70 | } |
| 71 | |
| 72 | BaseObject::BaseObject() |
| 73 | {} // Do nothing |
| 74 | |
| 75 | BaseObject::~BaseObject() |
| 76 | {} // Do nothing |
| 77 | |
| 78 | bool BaseObject::write(Engine& archive) const |
| 79 | { |
| 80 | // Do nothing |
| 81 | return true; // Successfully |
| 82 | } |
| 83 | |
| 84 | bool BaseObject::read(Engine& archive) |
| 85 | { |
| 86 | // Do nothing |
| 87 | return true; // Successfully |
| 88 | } |
| 89 | |
| 90 | static TypeManager::StringFunctionMap* theInstantiationFunctions = 0; |
| 91 | static int refCount = 0; |
| 92 | |
| 93 | TypeManager::StringFunctionMap& _internal_GetMap() |
| 94 | { |
| 95 | return *theInstantiationFunctions; |
| 96 | } |
| 97 | |
| 98 | void TypeManager::add(const char* name, NewBaseObjectFunction construction) |
| 99 | { |
| 100 | if (refCount++ == 0) { |
| 101 | theInstantiationFunctions = new StringFunctionMap; |
| 102 | } |
| 103 | assert(_internal_GetMap().find(String(name)) == _internal_GetMap().end()); |
| 104 | _internal_GetMap()[String(name)] = construction; |
| 105 | } |
| 106 | |
| 107 | void TypeManager::remove(const char* name) |
| 108 | { |
| 109 | assert(_internal_GetMap().find(String(name)) != _internal_GetMap().end()); |
| 110 | _internal_GetMap().erase(_internal_GetMap().find(String(name))); |
| 111 | if (--refCount == 0) { |
| 112 | delete theInstantiationFunctions; |
| 113 | theInstantiationFunctions = 0; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | BaseObject* TypeManager::createInstanceOf(const char* name) |
| 118 | { |
| 119 | if (!refCount || _internal_GetMap().find(String(name)) == _internal_GetMap().end()) |
| 120 | return NULL; |
| 121 | return (_internal_GetMap()[String(name)])(); |
| 122 | } |
| 123 | |
| 124 | TypeManager::Registration::Registration(const char *name, NewBaseObjectFunction func) : |
| 125 | myName(name) |
| 126 | { |
| 127 | TypeManager::add(name, func); |
| 128 | } |
| 129 | |
| 130 | TypeManager::Registration::~Registration() |
| 131 | { |
| 132 | TypeManager::remove(myName.c_str()); |
| 133 | } |
| 134 | |
| 135 | #ifdef CCXX_NAMESPACES |
| 136 | } |
| 137 | #endif |
| 138 | |
| 139 | #endif |
| 140 | |
| 141 | /** EMACS ** |
| 142 | * Local variables: |
| 143 | * mode: c++ |
| 144 | * c-basic-offset: 4 |
| 145 | * End: |
| 146 | */ |