fixed segfault
[ozulis.git] / src / ozulis / core / id.cxx
blob42e88e674fa863aa9ae78d12e957b6a14b00fdc5
1 #include <ozulis/core/assert.hh>
3 #include "id.hh"
5 namespace ozulis
7 namespace core
9 template <typename H>
10 IdBase<H>::IdBase()
11 : Singleton<IdBase<H> > (),
12 childs_(),
13 parents_(),
14 nextId_(1)
18 template <typename H>
19 id_t
20 IdBase<H>::nextId()
22 return IdBase<H>::instance().nextId_++;
25 template <typename H>
26 void
27 IdBase<H>::setParent(id_t parent, id_t child)
29 if (parent == child)
30 return;
31 assert(parent < child);
33 IdBase<H> & thiz = IdBase<H>::instance();
34 if (thiz.parents_.capacity() < child)
35 thiz.parents_.resize((child + 128) & 0x7f);
37 thiz.parents_[child] = parent;
38 thiz.childs_[parent].insert(child);
41 template <typename H>
42 id_t
43 IdBase<H>::parent(id_t child)
45 IdBase<H> & thiz = IdBase<H>::instance();
47 return thiz.parents_[child];
50 template <typename H>
51 bool
52 IdBase<H>::check(id_t parent, id_t child)
54 IdBase<H> & thiz = IdBase<H>::instance();
56 assert(parent < child);
57 id_t currentParent = thiz.parents_[child];
58 while (currentParent > parent)
60 child = currentParent;
61 currentParent = thiz.parents_[child];
63 return currentParent == parent;
66 template <typename H, typename T>
67 id_t
68 Id<H, T>::id()
70 static id_t id_ = IdBase<H>::nextId();
71 return id_;
74 template <typename H, typename P, typename T>
75 id_t
76 IdP<H, P, T>::id()
78 static id_t id_ = -1;
80 if (id_ != -1)
81 return id_;
83 id_t parent = Id<H, P>::id();
84 id_ = Id<H, T>::id();
86 IdBase<H>::setParent(parent, id_);
87 return id_;