4 #include <unordered_set> 6 #include "caffe2/core/logging.h" 19 virtual void Start() {}
20 virtual void Stop() {}
22 virtual std::string debugInfo() {
23 return "Not implemented.";
32 virtual std::unique_ptr<ObserverBase<T>> rnnCopy(
T* subject,
int rnn_order)
59 const Observer* AttachObserver(std::unique_ptr<Observer> observer) {
60 CAFFE_ENFORCE(observer,
"Couldn't attach a null observer.");
61 std::unordered_set<const Observer*> observers;
62 for (
auto& ob : observers_list_) {
63 observers.insert(ob.get());
66 const auto* observer_ptr = observer.get();
67 if (observers.count(observer_ptr)) {
70 observers_list_.push_back(std::move(observer));
81 for (
auto it = observers_list_.begin(); it != observers_list_.end(); ++it) {
82 if (it->get() == observer_ptr) {
83 auto res = std::move(*it);
84 observers_list_.erase(it);
92 virtual size_t NumObservers() {
93 return num_observers_;
97 inline static void StartObserver(
Observer* observer) {
100 }
catch (
const std::exception& e) {
101 LOG(ERROR) <<
"Exception from observer: " << e.what();
103 LOG(ERROR) <<
"Exception from observer: unknown";
107 inline static void StopObserver(
Observer* observer) {
110 }
catch (
const std::exception& e) {
111 LOG(ERROR) <<
"Exception from observer: " << e.what();
113 LOG(ERROR) <<
"Exception from observer: unknown";
118 num_observers_ = observers_list_.size();
119 if (num_observers_ != 1) {
123 observer_cache_ = observers_list_[0].get();
127 void StartAllObservers() {
129 if (num_observers_ == 0) {
131 }
else if (num_observers_ == 1) {
132 StartObserver(observer_cache_);
134 for (
auto& observer : observers_list_) {
135 StartObserver(observer.get());
140 void StopAllObservers() {
142 if (num_observers_ == 0) {
144 }
else if (num_observers_ == 1) {
145 StopObserver(observer_cache_);
147 for (
auto& observer : observers_list_) {
148 StopObserver(observer.get());
158 size_t num_observers_ = 0;
161 std::vector<std::unique_ptr<Observer>> observers_list_;
Use this to implement a Observer using the Observer Pattern template.
std::unique_ptr< Observer > DetachObserver(const Observer *observer_ptr)
Returns a unique_ptr to the removed observer.
Inherit to make your class observable.
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...