Leosac  0.8.0
Open Source Access Control
Registry.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #pragma once
21 
23 #include "tools/log.hpp"
24 #include <boost/any.hpp>
25 #include <map>
26 
27 namespace Leosac
28 {
29 
31 {
32  public:
33  RegistryKeyNotFoundException(const std::string &key)
34  : LEOSACException(BUILD_STR("Cannot find key " << key << " in registry."))
35  {
36  }
37 };
38 
54 template <typename KeyType = std::string>
55 class Registry
56 {
57  private:
58  template <typename T>
59  static bool is_type_valid(const boost::any &a)
60  {
61  try
62  {
63  boost::any_cast<T>(a);
64  return true;
65  }
66  catch (const boost::bad_any_cast &)
67  {
68  return false;
69  }
70  }
71 
72  public:
73  using Clock = std::chrono::steady_clock;
77  template <typename T>
78  std::enable_if_t<std::is_same<KeyType, std::string>::value &&
79  !std::is_same<KeyType, const char *>::value,
80  T>
81  get(const std::string &key) const
82  {
83  auto itr = store_.find(key);
84  if (itr == store_.end())
86 
87  ASSERT_LOG(is_type_valid<T>(itr->second),
88  "The object type for key " << key
89  << " mismatch the required type.");
90  return boost::any_cast<T>(itr->second);
91  }
92 
96  template <typename T>
97  std::enable_if_t<std::is_same<KeyType, const char *>::value &&
98  !std::is_same<KeyType, std::string>::value,
99  T>
100  get(const char *key) const
101  {
102  return get<T>(std::string(key));
103  }
104 
105  template <typename T>
106  std::enable_if_t<!std::is_same<KeyType, const char *>::value &&
107  !std::is_same<KeyType, std::string>::value,
108  T>
109  get(const KeyType &key) const
110  {
111  auto itr = store_.find(key);
112  if (itr == store_.end())
114  "NOT_SPECIFIED_BECAUSE_IS_NOT_STRING");
115 
116  ASSERT_LOG(is_type_valid<T>(itr->second),
117  "The object type for key " << key
118  << " mismatch the required type.");
119  return boost::any_cast<T>(itr->second);
120  }
121 
122  template <typename T>
123  void set(const KeyType &key, const T &obj)
124  {
125  purge();
126  store_[key] = boost::any(obj);
127  }
128 
129  template <typename T>
130  void set(const KeyType &key, const T &obj,
131  const std::chrono::steady_clock::time_point &expire_at)
132  {
133  purge();
134  store_[key] = boost::any(obj);
135  expiry_[key] = expire_at;
136  }
137 
143  void erase(const KeyType &key)
144  {
145  store_.erase(key);
146  expiry_.erase(key);
147  }
148 
149  bool has(const KeyType &key) const
150  {
151  return store_.find(key) != store_.end();
152  }
153 
157  void purge()
158  {
159  std::vector<KeyType> to_remove;
160 
161  auto now = Clock::now();
162  for (const auto &expiry_info : expiry_)
163  {
164  if (now > expiry_info.second)
165  {
166  to_remove.push_back(expiry_info.first);
167  }
168  }
169 
170  for (const auto &key : to_remove)
171  {
172  erase(key);
173  }
174  }
175 
176  size_t size() const
177  {
178  return store_.size();
179  }
180 
181  private:
182  std::map<KeyType, Clock::time_point> expiry_;
183  std::map<KeyType, boost::any> store_;
184 };
185 }
Leosac::Registry::is_type_valid
static bool is_type_valid(const boost::any &a)
Definition: Registry.hpp:59
Leosac::Registry::has
bool has(const KeyType &key) const
Definition: Registry.hpp:149
BUILD_STR
#define BUILD_STR(param)
Internal macro.
Definition: log.hpp:63
ASSERT_LOG
#define ASSERT_LOG(cond, msg)
Definition: log.hpp:190
Leosac::Registry::get
std::enable_if_t< std::is_same< KeyType, const char * >::value &&!std::is_same< KeyType, std::string >::value, T > get(const char *key) const
Specialization for string literals.
Definition: Registry.hpp:100
Leosac::Registry
A simple container that can holds object of any type.
Definition: Registry.hpp:55
Leosac::Registry::Clock
std::chrono::steady_clock Clock
Definition: Registry.hpp:73
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Leosac::Registry::set
void set(const KeyType &key, const T &obj, const std::chrono::steady_clock::time_point &expire_at)
Definition: Registry.hpp:130
leosacexception.hpp
Exception class for LEOSAC Project related errors.
Leosac::Registry::get
std::enable_if_t<!std::is_same< KeyType, const char * >::value &&!std::is_same< KeyType, std::string >::value, T > get(const KeyType &key) const
Definition: Registry.hpp:109
LEOSACException
A base class for Leosac specific exception.
Definition: leosacexception.hpp:40
Leosac::RegistryKeyNotFoundException::RegistryKeyNotFoundException
RegistryKeyNotFoundException(const std::string &key)
Definition: Registry.hpp:33
Leosac::Registry::get
std::enable_if_t< std::is_same< KeyType, std::string >::value &&!std::is_same< KeyType, const char * >::value, T > get(const std::string &key) const
Specialization for key of type std::string.
Definition: Registry.hpp:81
Leosac::Registry::set
void set(const KeyType &key, const T &obj)
Definition: Registry.hpp:123
Leosac::Registry::erase
void erase(const KeyType &key)
Erase an entry for the store.
Definition: Registry.hpp:143
Leosac::Registry::size
size_t size() const
Definition: Registry.hpp:176
Leosac::RegistryKeyNotFoundException
Definition: Registry.hpp:30
Leosac::Registry::expiry_
std::map< KeyType, Clock::time_point > expiry_
Definition: Registry.hpp:182
Leosac::Registry::purge
void purge()
Remove expired entry from the store.
Definition: Registry.hpp:157
log.hpp
Leosac::Registry::store_
std::map< KeyType, boost::any > store_
Definition: Registry.hpp:183