FD.io VPP  v19.08.3-2-gbabecb413
Vector Packet Processing
singular_db.hpp
Go to the documentation of this file.
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 <map>
20 #include <memory>
21 #include <ostream>
22 
23 #include "vom/logger.hpp"
24 
25 namespace VOM {
26 /**
27  * A Database to store the unique 'singular' instances of a single object
28  * type.
29  * The instances are stored as weak pointers. So the DB does not own these
30  * objects, they are owned by object in the client_db.
31  */
32 template <typename KEY, typename OBJ>
34 {
35 public:
36  /**
37  * Constructor
38  */
40 
41  /**
42  * Iterator
43  */
44  typedef
45  typename std::map<KEY, std::weak_ptr<OBJ>>::const_iterator const_iterator;
46 
47  /**
48  * Get iterator to the beginning of the DB
49  */
50  const_iterator begin() const { return m_map.cbegin(); }
51 
52  /**
53  * Get iterator to the beginning of the DB
54  */
55  const_iterator end() const { return m_map.cend(); }
56 
57  /**
58  * Find or add the object to the store.
59  * The object passed is deisred state. A new instance will be copy
60  * constructed from it. This function is templatised on the object type
61  * passed, which may be drrived from, the object type stored. this
62  * prevents slicing during the make_shared construction.
63  */
64  template <typename DERIVED>
65  std::shared_ptr<OBJ> find_or_add(const KEY& key, const DERIVED& obj)
66  {
67  auto search = m_map.find(key);
68 
69  if (search == m_map.end()) {
70  std::shared_ptr<OBJ> sp = std::make_shared<DERIVED>(obj);
71 
72  m_map[key] = sp;
73 
75  return (sp);
76  }
77 
78  return (search->second.lock());
79  }
80 
81  /**
82  * Find the object to the store.
83  */
84  std::shared_ptr<OBJ> find(const KEY& key)
85  {
86  auto search = m_map.find(key);
87 
88  if (search == m_map.end()) {
89  std::shared_ptr<OBJ> sp(NULL);
90 
91  return (sp);
92  }
93 
94  return (search->second.lock());
95  }
96 
97  /**
98  * Release the object from the DB store, if it's the one we have stored
99  */
100  void release(const KEY& key, const OBJ* obj)
101  {
102  auto search = m_map.find(key);
103 
104  if (search != m_map.end()) {
105  if (search->second.expired()) {
106  m_map.erase(key);
107  } else {
108  std::shared_ptr<OBJ> sp = m_map[key].lock();
109 
110  if (sp.get() == obj) {
111  m_map.erase(key);
112  }
113  }
114  }
115  }
116 
117  /**
118  * Find the object to the store.
119  */
120  void add(const KEY& key, std::shared_ptr<OBJ> sp) { m_map[key] = sp; }
121 
122  /**
123  * Populate VPP from current state, on VPP restart
124  */
125  void replay()
126  {
127  for (auto entry : m_map) {
128  entry.second.lock()->replay();
129  }
130  }
131 
132 private:
133  /**
134  * the map of objects against their key
135  */
136  std::map<const KEY, std::weak_ptr<OBJ>> m_map;
137 };
138 };
139 
140 /*
141  * fd.io coding-style-patch-verification: ON
142  *
143  * Local Variables:
144  * eval: (c-set-style "mozilla")
145  * End:
146  */
147 
148 #endif
#define VOM_LOG(lvl)
Definition: logger.hpp:181
void add(const KEY &key, std::shared_ptr< OBJ > sp)
Find the object to the store.
std::shared_ptr< OBJ > find(const KEY &key)
Find the object to the store.
Definition: singular_db.hpp:84
void release(const KEY &key, const OBJ *obj)
Release the object from the DB store, if it&#39;s the one we have stored.
static const log_level_t DEBUG
Definition: logger.hpp:32
const_iterator begin() const
Get iterator to the beginning of the DB.
Definition: singular_db.hpp:50
const_iterator end() const
Get iterator to the beginning of the DB.
Definition: singular_db.hpp:55
std::shared_ptr< OBJ > find_or_add(const KEY &key, const DERIVED &obj)
Find or add the object to the store.
Definition: singular_db.hpp:65
A Database to store the unique &#39;singular&#39; instances of a single object type.
Definition: singular_db.hpp:33
std::map< arp_proxy_config::key_t, std::weak_ptr< VOM::arp_proxy_config > >::const_iterator const_iterator
Iterator.
Definition: singular_db.hpp:45
singular_db()
Constructor.
Definition: singular_db.hpp:39
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
typedef key
Definition: ipsec.api:247
void replay()
Populate VPP from current state, on VPP restart.