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;
|
using namespace std;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void removeComment(string& str, string comment)
|
void removeComment(string& str, string comment)
|
||||||
{
|
{
|
||||||
string::size_type pos = str.find(comment);
|
string::size_type pos = str.find(comment);
|
||||||
|
|
@ -42,6 +44,14 @@ bool is_section(const string& str, string& name)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
eoState::~eoState(void)
|
||||||
|
{
|
||||||
|
for (unsigned i = 0; i < ownedObjects.size(); ++i)
|
||||||
|
{
|
||||||
|
delete ownedObjects[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void eoState::registerObject(eoPersistent& registrant)
|
void eoState::registerObject(eoPersistent& registrant)
|
||||||
{
|
{
|
||||||
string name = createObjectName(dynamic_cast<eoObject*>(®istrant));
|
string name = createObjectName(dynamic_cast<eoObject*>(®istrant));
|
||||||
|
|
|
||||||
|
|
@ -44,11 +44,28 @@ class eoState
|
||||||
{
|
{
|
||||||
public :
|
public :
|
||||||
|
|
||||||
|
eoState(void) {}
|
||||||
|
|
||||||
|
~eoState(void);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Object registration function, note that it does not take ownership!
|
* Object registration function, note that it does not take ownership!
|
||||||
*/
|
*/
|
||||||
void registerObject(eoPersistent& registrant);
|
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.
|
* Loading error thrown when nothing seems to work.
|
||||||
*/
|
*/
|
||||||
|
|
@ -96,6 +113,13 @@ private :
|
||||||
ObjectMap objectMap;
|
ObjectMap objectMap;
|
||||||
|
|
||||||
std::vector<ObjectMap::iterator> creationOrder;
|
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
|
#endif //eoState_h
|
||||||
|
|
|
||||||
Reference in a new issue