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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #ifndef __VOM_INST_DB_H__
17 #define __VOM_INST_DB_H__
22 #include "vom/logger.hpp"
26 * A Database to store the unique 'singular' instances of a single object
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.
31 template <typename KEY, typename OBJ>
44 typename std::map<KEY, std::weak_ptr<OBJ>>::const_iterator const_iterator;
47 * Get iterator to the beginning of the DB
49 const_iterator cbegin() { return m_map.cbegin(); }
52 * Get iterator to the beginning of the DB
54 const_iterator cend() { return m_map.cend(); }
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.
63 template <typename DERIVED>
64 std::shared_ptr<OBJ> find_or_add(const KEY& key, const DERIVED& obj)
66 auto search = m_map.find(key);
68 if (search == m_map.end()) {
69 std::shared_ptr<OBJ> sp = std::make_shared<DERIVED>(obj);
73 VOM_LOG(log_level_t::DEBUG) << *sp;
77 return (search->second.lock());
81 * Find the object to the store.
83 std::shared_ptr<OBJ> find(const KEY& key)
85 auto search = m_map.find(key);
87 if (search == m_map.end()) {
88 std::shared_ptr<OBJ> sp(NULL);
93 return (search->second.lock());
97 * Release the object from the DB store, if it's the one we have stored
99 void release(const KEY& key, const OBJ* obj)
101 auto search = m_map.find(key);
103 if (search != m_map.end()) {
104 if (search->second.expired()) {
107 std::shared_ptr<OBJ> sp = m_map[key].lock();
109 if (sp.get() == obj) {
117 * Find the object to the store.
119 void add(const KEY& key, std::shared_ptr<OBJ> sp) { m_map[key] = sp; }
122 * Print each of the object in the DB into the stream provided
124 void dump(std::ostream& os)
126 for (auto entry : m_map) {
127 os << "key: " << entry.first << std::endl;
128 os << " " << entry.second.lock()->to_string() << std::endl;
133 * Populate VPP from current state, on VPP restart
137 for (auto entry : m_map) {
138 entry.second.lock()->replay();
144 * the map of objects against their key
146 std::map<const KEY, std::weak_ptr<OBJ>> m_map;
151 * fd.io coding-style-patch-verification: ON
154 * eval: (c-set-style "mozilla")