Created
February 18, 2013 22:22
-
-
Save kcuzner/4981315 to your computer and use it in GitHub Desktop.
Destruction signal testing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <set> | |
#include <iostream> | |
#include <boost/signals2.hpp> | |
#include <boost/smart_ptr.hpp> | |
class Parent; | |
/** | |
* The child class has what is essentially a weak pointer | |
* reference to its parent. When its parent is destroyed, | |
* it should say it is an orphan | |
*/ | |
class Child | |
{ | |
public: | |
Child(Parent* parent); | |
~Child() { std::cout << "Child destroying..." << std::endl; } | |
bool isOrphan(); | |
Parent* getParent(); | |
private: | |
void onParentDestroying(Parent* parent); | |
Parent* parent; | |
bool isParentValid; | |
}; | |
class Parent | |
{ | |
public: | |
~Parent() | |
{ | |
std::cout << "Parent destroying..." << std::endl; | |
this->onDestroying(this); | |
} | |
boost::weak_ptr<Child> createChild() | |
{ | |
boost::shared_ptr<Child> child(new Child(this)); | |
this->children.insert(child); | |
return child; | |
} | |
void removeChild(boost::weak_ptr<Child> child) | |
{ | |
boost::shared_ptr<Child> shared = child.lock(); | |
this->children.erase(shared); | |
} | |
int howManyChildren() | |
{ | |
return this->children.size(); | |
} | |
boost::signals2::signal<void (Parent*)> onDestroying; | |
private: | |
std::set<boost::shared_ptr<Child> > children; | |
}; | |
Child::Child(Parent* parent) | |
{ | |
if (parent) | |
{ | |
this->isParentValid = true; | |
this->parent = parent; | |
parent->onDestroying.connect(boost::bind(&Child::onParentDestroying, this, _1)); | |
} | |
else | |
{ | |
this->isParentValid = false; | |
} | |
} | |
bool Child::isOrphan() | |
{ | |
return !this->isParentValid; | |
} | |
Parent* Child::getParent() | |
{ | |
return this->parent; | |
} | |
void Child::onParentDestroying(Parent* parent) | |
{ | |
if (parent == this->parent) | |
this->isParentValid = false; | |
} | |
int main() | |
{ | |
//parent goes out of scope | |
{ | |
boost::shared_ptr<Child> child; | |
{ | |
boost::shared_ptr<Parent> parent(new Parent()); | |
child = parent->createChild().lock(); | |
std::cout << "Parent in scope. Are we an orphan? " << child->isOrphan() << std::endl; | |
} | |
std::cout << "Parent out of scope. Are we an orphan? " << child->isOrphan() << std::endl; | |
} | |
//child goes out of scope | |
{ | |
boost::shared_ptr<Parent> parent(new Parent()); | |
{ | |
boost::shared_ptr<Child> child = parent->createChild().lock(); | |
std::cout << "Parent has " << parent->howManyChildren() << " children" << std::endl; | |
parent->removeChild(child); | |
} | |
std::cout << "Parent has " << parent->howManyChildren() << " children" << std::endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment