Added ownership functionality and made the thing non-copyable
This commit is contained in:
parent
49ee190e10
commit
570397e89e
2 changed files with 34 additions and 0 deletions
|
|
@ -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)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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 <class T>
|
||||
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<T&>(*ownedObjects.back());
|
||||
}
|
||||
|
||||
/**
|
||||
* Loading error thrown when nothing seems to work.
|
||||
|
|
@ -96,6 +113,13 @@ private :
|
|||
ObjectMap objectMap;
|
||||
|
||||
std::vector<ObjectMap::iterator> creationOrder;
|
||||
|
||||
std::vector<eoPersistent*> ownedObjects;
|
||||
|
||||
// private copy and assignment as eoState is supposed to be unique
|
||||
eoState(const eoState&);
|
||||
eoState& operator=(const eoState&);
|
||||
|
||||
};
|
||||
|
||||
#endif //eoState_h
|
||||
|
|
|
|||
Reference in a new issue