8 #ifndef IMPBASE_TRACKING_H
9 #define IMPBASE_TRACKING_H
11 #include <IMP/base/base_config.h>
17 IMPBASE_BEGIN_NAMESPACE
22 template <
class Tracked>
33 void add_tracked(Tracked*tr) {
41 void remove_tracked(Tracked*tr) {
45 "Tracked object " << (tr ? tr->get_name():
"nullptr")
48 if (added_.find(tr) != added_.end()) {
51 removed_.insert(std::make_pair(tr, tr->get_name()));
54 bool get_is_dirty()
const {
return !added_.empty() || !removed_.empty();}
55 void set_is_dirty(
bool tf) {
61 std::string get_changed_description()
const {
62 std::ostringstream oss;
63 if (!added_.empty()) {
64 oss <<
" Added: " << ObjectsTemp(added_.begin(), added_.end());
66 if (!removed_.empty()) {
69 it= removed_.begin(); it != removed_.end(); ++it) {
70 oss << it->second <<
", ";
77 TrackedIterator tracked_begin()
const {
78 return tracked_.begin();
80 TrackedIterator tracked_end()
const {
81 return tracked_.end();
85 for (
unsigned int i=0; i< tracked_.size(); ++i) {
87 tracked[i]->set_no_tracker();
94 template <
class Type,
class Tracker>
101 std::string name):
Object(name) {
102 IMP_USAGE_CHECK(tracker,
"Must pass non-null tracker in constructor.");
104 set_tracker(me, tracker);
107 void set_tracker(Type *me,
Tracker *tracker) {
109 "Can't pass a null oject with a non-null tacker.");
110 if (tracker == tracker_)
return;
112 static_cast<T*
>(tracker_.get())
113 ->remove_tracked(me_);
118 static_cast<T*
>(tracker_)->add_tracked(me_);
126 bool get_is_tracked()
const {
return tracker_;}
127 Tracker *get_tracker()
const {
return tracker_;}
129 set_tracker(
nullptr,
nullptr);
133 IMPBASE_END_NAMESPACE