Object Store
Prev Next
Objects Object Relations

Object Store

Now that you've written your entity classes you might want to insert them into the oos::object_store. But before doing so you have to tell the oos::object_store about your entity hierarchy once.

Setup object hierarchy

Assume we have the abstract base class Vehicle and derived from this classes Truck, Car and Bike. Now lets make this hierarchy known to the oos::object_store:

ostore.insert_prototype<Vehicle>("vehicle", true);
ostore.insert_prototype<Truck, Vehicle>("truck");
ostore.insert_prototype<Car, Vehicle>("car");
ostore.insert_prototype<Bike, Vehicle>("bike");

As you can see it is quite simple to add the hierarchy to the oos::object_store by calling method oos::object_store::insert_prototype. As there are several ways to call this method we decide to use the one with template arguments.

The Vehicle class is an abstract base (directly derived from oos::object). Here we need only one template argument (the class itself: Vehicle). With the first method paramter you give your type a unique name. The second parameter is a flag telling the oos::object_store that this type is abstract. Settings this flag to true you can't insert objects of this concrete type.

Add objects

Now that we've setup up our hierarchy we can add new objects to the oos::object_store.

typedef object_ptr<vehicle> vehicle_ptr;
vehicle_ptr truck = ostore.insert(new Truck("MAN");
vehicle_ptr car = ostore.insert(new Car("VW Beetle");
vehicle_ptr bike = ostore.insert(new Bike("Honda");

As you can see we use oos::object_ptr of type vehicle. The vehicle class in our example is the abstract base class for all concrete vehicle types. So the concrete vehicle object is inserted correctly and assigned to the object_ptr.

That means once you have inserted an object of any concrete type you access it via an appropriate object_ptr afterwards. You should never work with the raw instance pointer. This could lead to inconsistencies.

Modify objects

Now that we have some objects inserted we may want to modify them. The important thing here is as mentioned above that you don't deal with raw pointer to your object when try to modify it. You always have a pointer object wrapped around the object (like shared_ptr). The oos::object_store returns an oos::object_ptr when an object is inserted. Once you received the oos::object_ptr you can change your object by using it like usual pointer.

typedef oos::object_ptr<Truck> truck_ptr;
truck_ptr truck = ostore.insert(new Truck("MAN"));
truck->weight(2.5);
truck->color("red");

Remove objects

Once we have an object_ptr object we can easily delete this object from the object_store by calling oos::object_store::remove()

typedef oos::object_ptr<Truck> truck_ptr;
truck_ptr truck = ostore.insert(new Truck("MAN"));
if (ostore.is_removable(truck)) {
// remove object
ostore.remove(truck);
} else {
// object can't be removed
}

When removing an object internally a check is done if the object (and subsequently all connected objects) can be removed. This test is done by checking the pointer and reference count of each concerning object. If the test succeeds the object is removed and true is returned. If the test fails false is returned.

Prev Next
Objects Object Relations