Skip to content

Commit

Permalink
rectangles: Move enable_shared_from_this to base
Browse files Browse the repository at this point in the history
In preparation of making a recursive find_rect() function which will need
shared_from_this in the base class, we need to move enable_shared_from_this into
the base Rectangle class.

CompositeRectangle will still get a type-safe shared_from_this()
function using static_pointer_cast, which is safe because
CompositeRectangle::shared_from_this is guaranteed to be called with a
CompositeRectangle instance.
  • Loading branch information
hyperair committed May 5, 2012
1 parent 2131a26 commit 89d4fb5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/imgpack/algorithm/rectangles.cc
Expand Up @@ -101,6 +101,19 @@ void CompositeRectangle::recalculate_size ()
recalculate_size_impl ();
}

std::shared_ptr<const CompositeRectangle>
CompositeRectangle::shared_from_this () const
{
return std::static_pointer_cast<const CompositeRectangle>
(Rectangle::shared_from_this ());
}

CompositeRectangle::Ptr CompositeRectangle::shared_from_this ()
{
return std::static_pointer_cast<CompositeRectangle>
(Rectangle::shared_from_this ());
}



// HCompositeRectangle definitions
Expand Down
10 changes: 7 additions & 3 deletions src/imgpack/algorithm/rectangles.hh
Expand Up @@ -12,7 +12,8 @@ namespace ImgPack
{
class CompositeRectangle;

class Rectangle
class Rectangle :
public std::enable_shared_from_this<Rectangle>
{
public:
typedef std::shared_ptr<Rectangle> Ptr;
Expand Down Expand Up @@ -51,8 +52,7 @@ namespace ImgPack


class CompositeRectangle :
public Rectangle,
public std::enable_shared_from_this<CompositeRectangle>
public Rectangle
{
public:
typedef std::shared_ptr<CompositeRectangle> Ptr;
Expand All @@ -69,6 +69,10 @@ namespace ImgPack
void orphan_child (Rectangle &child);

void recalculate_size ();

std::shared_ptr<const CompositeRectangle> shared_from_this () const;
Ptr shared_from_this ();

protected:
virtual void recalculate_size_impl () = 0;
std::pair<Rectangle::Ptr, Rectangle::Ptr> _children;
Expand Down

0 comments on commit 89d4fb5

Please sign in to comment.