07ea2cb4acff658e70d4c43ca3cd3bb7cdbd7cf6
[vpp.git] / src / vpp-api / vom / singular_db.hpp
1 /*
2  * Copyright (c) 2017 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #ifndef __VOM_INST_DB_H__
17 #define __VOM_INST_DB_H__
18
19 #include <memory>
20 #include <ostream>
21
22 #include "vom/logger.hpp"
23
24 namespace VOM {
25 /**
26  * A Database to store the unique 'singular' instances of a single object
27  * type.
28  * The instances are stored as weak pointers. So the DB does not own these
29  * objects, they are owned by object in the client_db.
30  */
31 template <typename KEY, typename OBJ>
32 class singular_db
33 {
34 public:
35   /**
36    * Constructor
37    */
38   singular_db() {}
39
40   /**
41    * Iterator
42    */
43   typedef
44     typename std::map<KEY, std::weak_ptr<OBJ>>::const_iterator const_iterator;
45
46   /**
47    * Get iterator to the beginning of the DB
48    */
49   const_iterator cbegin() { return m_map.cbegin(); }
50
51   /**
52    * Get iterator to the beginning of the DB
53    */
54   const_iterator cend() { return m_map.cend(); }
55
56   /**
57    * Find or add the object to the store.
58    * The object passed is deisred state. A new instance will be copy
59    * constructed from it. This function is templatised on the object type
60    * passed, which may be drrived from, the object type stored. this
61    * prevents slicing during the make_shared construction.
62    */
63   template <typename DERIVED>
64   std::shared_ptr<OBJ> find_or_add(const KEY& key, const DERIVED& obj)
65   {
66     auto search = m_map.find(key);
67
68     if (search == m_map.end()) {
69       std::shared_ptr<OBJ> sp = std::make_shared<DERIVED>(obj);
70
71       m_map[key] = sp;
72
73       VOM_LOG(log_level_t::DEBUG) << *sp;
74       return (sp);
75     }
76
77     return (search->second.lock());
78   }
79
80   /**
81    * Find the object to the store.
82    */
83   std::shared_ptr<OBJ> find(const KEY& key)
84   {
85     auto search = m_map.find(key);
86
87     if (search == m_map.end()) {
88       std::shared_ptr<OBJ> sp(NULL);
89
90       return (sp);
91     }
92
93     return (search->second.lock());
94   }
95
96   /**
97    * Release the object from the DB store, if it's the one we have stored
98    */
99   void release(const KEY& key, const OBJ* obj)
100   {
101     auto search = m_map.find(key);
102
103     if (search != m_map.end()) {
104       if (search->second.expired()) {
105         m_map.erase(key);
106       } else {
107         std::shared_ptr<OBJ> sp = m_map[key].lock();
108
109         if (sp.get() == obj) {
110           m_map.erase(key);
111         }
112       }
113     }
114   }
115
116   /**
117    * Find the object to the store.
118    */
119   void add(const KEY& key, std::shared_ptr<OBJ> sp) { m_map[key] = sp; }
120
121   /**
122    * Print each of the object in the DB into the stream provided
123    */
124   void dump(std::ostream& os)
125   {
126     for (auto entry : m_map) {
127       os << "key: " << entry.first << std::endl;
128       os << "  " << entry.second.lock()->to_string() << std::endl;
129     }
130   }
131
132   /**
133    * Populate VPP from current state, on VPP restart
134    */
135   void replay()
136   {
137     for (auto entry : m_map) {
138       entry.second.lock()->replay();
139     }
140   }
141
142 private:
143   /**
144    * the map of objects against their key
145    */
146   std::map<const KEY, std::weak_ptr<OBJ>> m_map;
147 };
148 };
149
150 /*
151  * fd.io coding-style-patch-verification: ON
152  *
153  * Local Variables:
154  * eval: (c-set-style "mozilla")
155  * End:
156  */
157
158 #endif