Smart pointers: shared and weak.
Hello,
I finished removing NodeLinked
. Please check the related issue #18 (closed).
Now I am introducing the shared_ptr
and weak_ptr
design, following guidelines in:
http://www.umich.edu/~eecs381/handouts/C++11_smart_ptrs.pdf
which are, on the other hand, the regular guidelines -- it's just nicely explained.
I have a skeleton test building the whole WOLF tree from scratch. You can check it out here:
- branch
smart_ptr
- file
examples/test_sh_ptr.cpp
Easy notes:
- Classes names are just one letter. See the comments in the file. They are P, H, S, p, T, F, C, f, c, M, L.
- By now the test only builds the basic tree, with
shared_ptr
looking down, andweak_ptr
looking up. - It does not have other pointers, e.g. from Capture
C
to SensorS
, or from constraints to Frames, Features, or Landmarks. - It constructs and destructs the whole tree, with each node containing always 2 children:
-
H
has 2 sensors, each has 2 processors -
T
has two frames, each has 2 captures, each has 2 features, each has 2 constraints -
M
has 2 landmarks
-
Less easy notes:
- All members need to be
"shared_pointed"
to completely avoid dangling objects and non_smart pointers to objects- This means that the problem node
P
needs to be ashared_ptr<P>
, not a plain objectP
. - Also,
H
,T
andM
are shared pointers inP
, not just members; otherwise we cannot point to them from their children. - This is what we have been mostly doing in wolf, but now it is an unavoidable must. Please checkout the class
P
.
- This means that the problem node
- All classes inherit from
enable_shared_from_this<T>
to be able to establish the links from child to parent. If we find another way to do so, we can remove this inheritance. But this is regular stuff withshared_ptr
, we just need to be used to it. - Because
shared_from_this()
cannot be called during the constructor ofP
,P
has a methodsetup()
that linksH
,T
andM
toP
once the constructor ofP
is finished.- Notice that we cannot call
setup()
inside the constructor ofP
(this would not make sense), - Therefore in the
main()
we have to constructP
, then callsetup()
.
- Notice that we cannot call