From 570397e89e48aee3d95e3027908a40279052b302 Mon Sep 17 00:00:00 2001 From: mac Date: Tue, 13 Jun 2000 14:31:53 +0000 Subject: [PATCH] Added ownership functionality and made the thing non-copyable --- eo/src/utils/eoState.cpp | 10 ++++++++++ eo/src/utils/eoState.h | 24 ++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/eo/src/utils/eoState.cpp b/eo/src/utils/eoState.cpp index d69c23d3..baf57c7d 100644 --- a/eo/src/utils/eoState.cpp +++ b/eo/src/utils/eoState.cpp @@ -13,6 +13,8 @@ using namespace std; + + void removeComment(string& str, string comment) { string::size_type pos = str.find(comment); @@ -41,6 +43,14 @@ bool is_section(const string& str, string& name) return true; } + +eoState::~eoState(void) +{ + for (unsigned i = 0; i < ownedObjects.size(); ++i) + { + delete ownedObjects[i]; + } +} void eoState::registerObject(eoPersistent& registrant) { diff --git a/eo/src/utils/eoState.h b/eo/src/utils/eoState.h index 05316784..b37a590e 100644 --- a/eo/src/utils/eoState.h +++ b/eo/src/utils/eoState.h @@ -44,10 +44,27 @@ class eoState { public : + eoState(void) {} + + ~eoState(void); + /** * Object registration function, note that it does not take ownership! */ void registerObject(eoPersistent& registrant); + + /** + * Copies the object (MUST be derived from eoPersistent) + * and returns a reference to the owned object. + * Note: it does not register the object, this must be done afterwards! + */ + template + T& takeOwnership(const T& persistent) + { + // If the compiler budges here, T is not a subclass of eoPersistent + ownedObjects.push_back(new T(persistent)); + return static_cast(*ownedObjects.back()); + } /** * Loading error thrown when nothing seems to work. @@ -96,6 +113,13 @@ private : ObjectMap objectMap; std::vector creationOrder; + + std::vector ownedObjects; + + // private copy and assignment as eoState is supposed to be unique + eoState(const eoState&); + eoState& operator=(const eoState&); + }; #endif //eoState_h