PetaVision  Alpha
ObserverTable.hpp
1 /*
2  * ObserverTable.hpp
3  *
4  * Created on: Jul 22, 2016
5  * Author: pschultz
6  */
7 
8 #ifndef OBSERVERTABLE_HPP_
9 #define OBSERVERTABLE_HPP_
10 
11 #include "observerpattern/Observer.hpp"
12 #include <map>
13 #include <string>
14 #include <vector>
15 
16 namespace PV {
17 
28  public:
29  ObserverTable() {}
30  virtual ~ObserverTable() {}
31 
32  std::vector<Observer *> const &getObjectVector() const { return mObjectVector; }
33  std::map<std::string, Observer *> const &getObjectMap() const { return mObjectMap; }
34  Observer *getObject(std::string const &name) const {
35  auto lookupResult = mObjectMap.find(name);
36  return lookupResult == mObjectMap.end() ? nullptr : lookupResult->second;
37  }
38  Observer *getObject(char *name) const { return getObject(std::string(name)); };
39  std::vector<Observer *>::size_type size() const;
40  bool addObject(std::string const &name, Observer *entry);
41  void deleteObject(std::string const &name, bool deallocateFlag);
42  void deleteObject(char const *name, bool deallocateFlag) {
43  deleteObject(std::string(name), deallocateFlag);
44  }
45  void clear(bool deallocateFlag);
46 
47  template <typename S>
48  S *lookup(std::string const &name) const {
49  return lookup<S>(name.c_str());
50  }
51 
52  template <typename S>
53  S *lookup(char const *name) const {
54  S *lookupResult = nullptr;
55  auto findResult = mObjectMap.find(name);
56  if (findResult != mObjectMap.end()) {
57  auto observerPtr = findResult->second;
58  lookupResult = dynamic_cast<S *>(observerPtr);
59  }
60  return lookupResult;
61  }
62 
63  private:
64  std::vector<Observer *> mObjectVector;
65  std::map<std::string, Observer *> mObjectMap;
66 };
67 
68 } /* namespace PV */
69 
70 #endif /* OBSERVERTABLE_HPP_ */