00001 /******************************************************************************* 00002 00003 Copyright (C) 2007 by Brendon Costa 00004 00005 This library is free software; you can redistribute it and/or modify 00006 it under the terms of the "LGPL Like" License defined in the file COPYING 00007 that should have been distributed along with this source. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00012 00013 You should have received a copy of the "LGPL Like" License 00014 along with this library; see the file COPYING. if not, it can be 00015 obtained from the EDoc++ website: 00016 http://edoc.sourceforge.net/license.html 00017 00018 *******************************************************************************/ 00019 #ifndef EDOC_MANAGEDOBJECT_H 00020 #define EDOC_MANAGEDOBJECT_H 00021 00022 #include "EDoc/DictionarySpecific.h" 00023 #include "EDoc/exceptions.h" 00024 #include "EDoc/StringIdentifiedObject.h" 00025 #include <string> 00026 #include <map> 00027 #include <list> 00028 #include <vector> 00029 00030 namespace EDoc 00031 { 00032 class File; 00033 class Type; 00034 class FunctionType; 00035 class Function; 00036 class Dictionary; 00037 00038 void AllocNew(File*& obj, Dictionary& dict, std::string key_name); 00039 void AllocNew(Type*& obj, Dictionary& dict, std::string key_name); 00040 void AllocNew(Function*& obj, Dictionary& dict, std::string key_name); 00041 void AllocNew(FunctionType*& obj, Dictionary& dict, std::string key_name); 00042 00043 void AllocNew(File*& obj, Dictionary& dict, uint32_t index); 00044 void AllocNew(Type*& obj, Dictionary& dict, uint32_t index); 00045 void AllocNew(Function*& obj, Dictionary& dict, uint32_t index); 00046 void AllocNew(FunctionType*& obj, Dictionary& dict, uint32_t index); 00047 00048 00049 //=========================================================================== 00050 /** \brief Generic representation for a collection of StringIdentifiedObject 00051 * instances. 00052 * 00053 * This stores the StringIdentifiedObject instances and is the ultimate 00054 * authority of where to find them. This class will be derived by a template 00055 * class for each dervied varient of StringIdentifiedObject. Namely File, 00056 * Type, FunctionType and Function. 00057 * 00058 * This class provides basic functionality for creating new 00059 * StringIdentifiedObject instances and searchif for existing instances. 00060 */ 00061 class ManagedObject : public DictionarySpecific 00062 { 00063 public: 00064 00065 //------------------------------------------------------------------------ 00066 /** \brief Constructor. 00067 */ 00068 ManagedObject(Dictionary& dict_in); 00069 00070 //------------------------------------------------------------------------ 00071 /** \brief Destructor. 00072 */ 00073 virtual ~ManagedObject() {} 00074 00075 //------------------------------------------------------------------------ 00076 /** \brief Assignment operator. 00077 */ 00078 ManagedObject& operator=(const ManagedObject& right); 00079 00080 //------------------------------------------------------------------------ 00081 /** \brief See DictionarySpecific::ReplaceReferences() 00082 */ 00083 virtual size_t ReplaceReferences(PStack& stack, 00084 void* remove, void* replace); 00085 00086 //------------------------------------------------------------------------ 00087 /** \brief See DictionarySpecific::Print() 00088 */ 00089 virtual std::ostream& Print(std::ostream& out, 00090 std::string prefix="") const; 00091 00092 //------------------------------------------------------------------------ 00093 /** \brief See DictionarySpecific::Validate() 00094 */ 00095 virtual void Validate(PStack& stack, const Dictionary& dict_in) const; 00096 00097 00098 00099 00100 00101 00102 00103 00104 00105 00106 //------------------------------------------------------------------------ 00107 /** \brief Rease all StringIdentifiedObject instances referenced by this 00108 * contains and the data they use. 00109 */ 00110 void EraseAll(); 00111 00112 //------------------------------------------------------------------------ 00113 /** \brief Common : get instance from key name. 00114 */ 00115 inline StringIdentifiedObjectPtr CGet(std::string name) 00116 { 00117 if (!items.count(name)) 00118 { 00119 return NULL; 00120 } 00121 else 00122 { 00123 return items.find(name)->second; 00124 } 00125 } 00126 00127 //------------------------------------------------------------------------ 00128 /** \brief Common : get instance from key name or create new instance if 00129 * key does not exist. 00130 */ 00131 StringIdentifiedObjectPtr CAlwaysGet(std::string name); 00132 00133 //------------------------------------------------------------------------ 00134 /** \brief Common : get instance equivilant to right or create new 00135 * instance if no equivilant to right exists in this collection. 00136 */ 00137 StringIdentifiedObjectPtr CAlwaysGet(const StringIdentifiedObject& right); 00138 00139 //------------------------------------------------------------------------ 00140 /** \brief Common : Remove instance equivilant to right. 00141 * 00142 * Throws BugException if equivilant to right does not exist in this 00143 * collection. 00144 */ 00145 void CRemove(StringIdentifiedObjectPtr right); 00146 00147 //------------------------------------------------------------------------ 00148 /** \brief Add given instance to this collection. 00149 * 00150 * If an equivilant to right already exists in collection will throw a 00151 * BugException. 00152 */ 00153 void CAdd(StringIdentifiedObjectPtr right); 00154 00155 //------------------------------------------------------------------------ 00156 /** \brief See CGet() 00157 */ 00158 inline const StringIdentifiedObjectPtr CGet(std::string name) const 00159 { 00160 return const_cast<ManagedObject*>(this)->CGet(name); 00161 } 00162 00163 //------------------------------------------------------------------------ 00164 /** \brief Used to create new instance of particular type of 00165 * StringIdentifiedObject that this collection manages. 00166 */ 00167 virtual StringIdentifiedObjectPtr New(Dictionary& dict, std::string name) = 0; 00168 00169 //------------------------------------------------------------------------ 00170 // @@@Brendon This should be private. 00171 /** \brief Maps unique string idenitifcation keys to object instances. 00172 */ 00173 std::map<std::string, StringIdentifiedObjectPtr> items; 00174 00175 //------------------------------------------------------------------------ 00176 00177 }; 00178 //=========================================================================== 00179 typedef ManagedObject* ManagedObjectPtr; 00180 typedef std::list<ManagedObject*> MngObjPList; 00181 typedef std::vector<ManagedObject*> MngObjPVec; 00182 } 00183 00184 #endif // EDOC_MANAGEDOBJECT_H