libs/EDoc/ManagedObject.cpp

Go to the documentation of this file.
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 #include "config.h"
00020 
00021 #include "EDoc/ManagedObject.h"
00022 
00023 #include "EDoc/Type.h"
00024 #include "EDoc/Function.h"
00025 #include "EDoc/FunctionType.h"
00026 #include "EDoc/File.h"
00027 #include "EDoc/PStack.h"
00028 #include "EDoc/StackRef.h"
00029 #include "EDoc/utils.h"
00030 
00031 namespace EDoc
00032 {
00033 
00034    //===========================================================================
00035    void AllocNew(File*& obj,           Dictionary& dict, std::string key_name)
00036    {
00037       EDOC_ASSERT(&dict != NULL, "");
00038       EDOC_Finer("Creating new file object with key name: " << key_name);
00039       obj = new File(dict, key_name);
00040    }
00041    //===========================================================================
00042    void AllocNew(Type*& obj,           Dictionary& dict, std::string key_name)
00043    {
00044       EDOC_ASSERT(&dict != NULL, "");
00045       obj = new Type(dict, key_name);
00046    }
00047    //===========================================================================
00048    void AllocNew(Function*& obj,       Dictionary& dict, std::string key_name)
00049    {
00050       EDOC_ASSERT(&dict != NULL, "");
00051       obj = new Function(dict, key_name);
00052    }
00053    //===========================================================================
00054    void AllocNew(FunctionType*& obj,   Dictionary& dict, std::string key_name)
00055    {
00056       EDOC_ASSERT(&dict != NULL, "");
00057       obj = new FunctionType(dict, key_name);
00058    }
00059    //===========================================================================
00060 
00061    void AllocNew(File*& obj,           Dictionary& dict, uint32_t index)
00062    {
00063       EDOC_ASSERT(&dict != NULL, "");
00064       EDOC_Finer("Creating new file object with index: " << index);
00065       obj = new File(dict, index);
00066    }
00067    //===========================================================================
00068    void AllocNew(Type*& obj,           Dictionary& dict, uint32_t index)
00069    {
00070       EDOC_ASSERT(&dict != NULL, "");
00071       obj = new Type(dict, index);
00072    }
00073    //===========================================================================
00074    void AllocNew(Function*& obj,       Dictionary& dict, uint32_t index)
00075    {
00076       EDOC_ASSERT(&dict != NULL, "");
00077       obj = new Function(dict, index);
00078    }
00079    //===========================================================================
00080    void AllocNew(FunctionType*& obj,   Dictionary& dict, uint32_t index)
00081    {
00082       EDOC_ASSERT(&dict != NULL, "");
00083       obj = new FunctionType(dict, index);
00084    }
00085    //===========================================================================
00086 
00087 
00088    //===========================================================================
00089    ManagedObject::ManagedObject(Dictionary& dict_in) :
00090       DictionarySpecific(&dict_in)
00091    {
00092       EDOC_ASSERT(&dict != NULL, "");
00093    }
00094    //===========================================================================
00095    void ManagedObject::EraseAll()
00096    {
00097       EDOC_FOREACH(StrIDObjPMap, it, items)
00098       {
00099          delete it->second;
00100          it->second = NULL;
00101       }
00102       Erase(items);
00103    }
00104    //===========================================================================
00105    size_t ManagedObject::ReplaceReferences(PStack& stack, void* remove, 
00106       void* replace)
00107    {
00108       size_t count = 0;
00109       EDOC_FOREACH(StrIDObjPMap, it, items)
00110       {
00111          count += it->second->ReplaceReferences(stack, remove, replace);
00112       }
00113 
00114       return count;
00115    }
00116    //===========================================================================
00117    void ManagedObject::Validate(PStack& stack, const Dictionary& dict_in) const
00118    {
00119       EDOC_ASSERT(&dict == &dict_in, 
00120          "Current dict: " << &dict 
00121          << "\nRequired dict: " << &dict_in);
00122 
00123       EDOC_FOREACH_CONST(StrIDObjPMap, it, items)
00124       {
00125          EDOC_Finer("Validating: " << it->first);
00126          EDOC_ASSERT(it->first == it->second->GetKeyName(), 
00127             "All items should be indexed by their key names."
00128             << " Index: " << it->first 
00129             << ", Key Name: " << it->second->GetKeyName());
00130          it->second->Validate(stack, dict_in);
00131       }
00132    }
00133    //===========================================================================
00134    std::ostream& ManagedObject::Print(std::ostream& out, 
00135       std::string prefix) const
00136    {
00137       EDOC_FOREACH_CONST(StrIDObjPMap, it, items)
00138       {
00139          it->second->Print(out, prefix) << std::endl;
00140       }
00141 
00142       return out;
00143    }
00144    //===========================================================================
00145    ManagedObject& ManagedObject::operator=(
00146       const ManagedObject& EDOC_UNUSED(right))
00147    {
00148       EDOC_ASSERT(false, "Assignment of managed objects should never occur "
00149          "but is required by SWIG wrappers.");
00150       return *this;
00151    }
00152    //===========================================================================
00153    StringIdentifiedObjectPtr ManagedObject::CAlwaysGet(std::string name)
00154    {
00155       StringIdentifiedObjectPtr item = CGet(name);
00156       if (!item)
00157       {
00158          item = New(dict, name);
00159          items.insert(std::pair<std::string, 
00160             StringIdentifiedObjectPtr>(name, item));
00161       }
00162       return item;
00163    }
00164    //===========================================================================
00165    StringIdentifiedObjectPtr ManagedObject::CAlwaysGet(
00166       const StringIdentifiedObject& right)
00167    {
00168       if (&right.dict == &dict)
00169       {
00170          // Cast away constness.
00171          return const_cast<StringIdentifiedObjectPtr>(&right);
00172       }
00173       else
00174       {
00175          return CAlwaysGet(right.GetKeyName());
00176       }
00177    }
00178    //===========================================================================
00179    void ManagedObject::CRemove(StringIdentifiedObjectPtr right)
00180    {
00181       // @@@Brendon I am removing by key name. Should i remove by pointer
00182       // value, i.e. iterate over all members till find second that has same
00183       // pointer as passed in?
00184       EDOC_ASSERT(items.count(right->GetKeyName()), "");
00185       EDOC_ASSERT(items.find(right->GetKeyName())->second == right, "");
00186       items.erase(items.find(right->GetKeyName()));
00187    }
00188    //===========================================================================
00189    void ManagedObject::CAdd(StringIdentifiedObjectPtr right)
00190    {
00191       EDOC_ASSERT(!items.count(right->GetKeyName()), "");
00192       items.insert(std::pair<std::string, StringIdentifiedObjectPtr>(
00193          right->GetKeyName(), right));
00194    }
00195    //===========================================================================
00196 
00197 }

Generated on Tue Jan 20 18:26:07 2009 for EDoc-0.2.1 by  doxygen 1.5.1