Object Relations
Prev Next
Object Store Object Views

Object relations

When it comes to object relations you can use one to one and one to many relations in a straight forward way.

OneToOne relations

In this example we have two object types an address class and a person class.

class address : public oos::object
{
public:
//...
private:
std::string street_;
std::string city_;
};

Each person class should have an address, so we add an oos::object_ptr of type address to the members. That's it. Now we have a one to one relationship beetween two classes.

class person : public oos::object
{
public:
//...
private:
};

With this kind of relationship we have a hard linked relationship. Which means if we remove the person from our store the address object is removed as well.

In case we want to keep the address object we can use a oos::object_ref of type adress instead of object_ptr.

class person : public oos::object
{
private:
};

With object_ref we have a soft link to the address inside our person class and it won't be removed on a person removal. We must explicitly remove the address.

OneToMany relations

When it comes to one to many releationships it is also quiet easy. OOS comes with three types of container classes which can be used to setup one to many relationships.

Because these classes are designed in the same way as the STL classes we can use them in the same way plus the benefit of the relationship.

Our handy person class needs a list of friends which are also of type person. And because we want this list with soft linked person we use oos::object_ref as the type of the list.

class person : public oos::object
{
public:
// the value type of the list
typedef oos::object_ref<person> person_ref;
// the list definition
typedef oos::object_list<person_ref> person_list_t;
// shortcuts to the iterators
typedef person_list_t::iterator iterator;
typedef person_list_t::const_iterator const_iterator;
// c'tor for person
person(const std::string &name)
: name_(name)
// the list need the person to handle the relationship
, friends_(this)
{}
std::string name() const { return name_; }
// add a friend to persons friend list
void add_friend(const person_ref &p)
{
friends_.push_back(p);
}
// STL like iterators
iterator begin() { return friends_.begin(); }
iterator end() { return friends_.end(); }
private:
std::string name_;
person_list_t friends_;
};

Why declare a list if we don't use it? Next is an example howto to use our persons friend list.

We insert a new person into the object_store. Than we insert and immediatily add some persons as friends to our first person.

typedef oos::object_ptr<person> person_ptr;
// create a new person
person_ptr p = ostore.insert(new person("joe"));
// add some friends
p->add_friend(ostore.insert(new person("walter")));
p->add_friend(ostore.insert(new person("helen")));
p->add_friend(ostore.insert(new person("tim")));

Now we can simply iterate over the list like we used to do it with all STL containers. Period.

// access all friends
person::iterator i = p->begin();
for (i; i != p->end(); ++i) {
std::cout << i->value()->name() << std::endl;
}
Prev Next
Object Store Object Views