pointer with releation code refactor

Tag: refactoring Author: dashagua1hao Date: 2013-06-01

Recently, I am about to starting to work on a project which involves refactoring and modifying existing code in c++.

My Project caches lots of objects which have Parent-child relationship between them, every object has a unique ID. See the following code:

class GetReleation
{
  tempalte<typename T>
  T* GetParent(const int &iID);

  tempalte<typename T>
  map<int, T*> GetChildren(const int &iID);

  .....
}

class ObjectClass
{
  int GetID();
  int GetOhterProperty();

  ......
}

The objects cached in the project were never changed during running, so locks is avoid. And the following code works fine.

ObjectType pObject = getReleation.GetParent(iID);
pObject->GetOhterProperty();

But Now, objects may add/modify/delete during running, a new module is added to cached objects and manage objects change. It has a read-write lock to protect multithread visit, all objects cache with a boost share_ptr. The new module recommended direct functions to get property of object(for multithread safe use), you should not get pointer of object, then use pointer to get property.

To achieve goals of get property of object, There are following methods:

1?NewModule ObjectVisitClass.GetProperty(iID);

There are so many class use member function of GetReleation, use new module directly means a lot of work to do.

2?use GetReleation, but change implement of member function.

T* GetParent(const int &iID)
{
   Get boost share_ptr<T> from new module;
   return boost share_ptr<T>::get();
}

user of GetReleation get pointer of object, in multithread will core dump, because this object may have been deleted in new module when user get the pointer of object.

we can return a new copy object of boost share_ptr::get()

T* GetParent(const int &iID)
{
   Get boost share_ptr<T> from new module;
   return new T(*boost share_ptr<T>::get());
}

This works fine, but means a lot of copy. And the property of object maybe out of date, because object may change in new module after user get copyed object(not the same object in new module).

Please forgive my poor english, Is there a better way to implment this? Any help will be appreciate, Thx!