NAIA  1.0.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
Utils.hpp
Go to the documentation of this file.
1 #ifndef NAIA_UTILS_HPP
2 #define NAIA_UTILS_HPP
3 
4 // our headers
6 
7 // c++ headers
8 #include <map>
9 #include <vector>
10 
11 namespace NAIA {
12 
13 namespace _private {
14 
15 // NOTE: this is a neat trick to have a piece of code only called N times, regardless of which thread calls it and when
16 // credits: https://twitter.com/The_Whole_Daisy/status/1450911231959703559?s=20&t=K4-GOLFtd-2kJoRn7hMxdA
17 template <unsigned int times_to_run> struct LimitedWarning {
19 
20  LimitedWarning(std::string message, std::string source) : msg{std::move(message)}, src{std::move(source)} {}
21 
22  void operator()() const {
23  try {
24  static bool unused = [this] {
25  static int count = 0;
26  static auto logger = getLogger(src);
27 
28  logger->warn("{}", msg);
29  if (++count < times_to_run - 1) {
31  }
32  if (count == times_to_run - 1) {
33  logger->warn("{} (This warning will not be printed anymore)", msg);
34  }
35  return true;
36  }();
37  (void)unused;
38  } catch (const NotDonePrintingException &) {
39  }
40  }
41 
42  std::string msg;
43  std::string src;
44 };
45 
46 // NOTE(vformato): This will become inline in c++17
48 } // namespace _private
49 
51 template <typename T, size_t N, typename Key>
52 typename std::enable_if<std::is_convertible<Key, size_t>::value, bool>::type
53 ContainsKeys(const std::array<T, N> &container, Key key) {
54  return N > static_cast<size_t>(key);
55 }
56 
58 template <typename T, typename Key, typename Value>
59 typename std::enable_if<std::is_convertible<T, Key>::value, bool>::type
60 KeyExists(T key, const std::map<Key, Value> &container) {
62  return container.find(static_cast<Key>(key)) != end(container);
63 }
64 
66 template <typename T, typename Value>
67 typename std::enable_if<std::is_convertible<T, size_t>::value, bool>::type
68 KeyExists(T key, const std::vector<Value> &container) {
70  return container.size() > static_cast<size_t>(key);
71 }
72 
74 template <typename T, typename Key, typename Value>
75 typename std::enable_if<std::is_convertible<T, Key>::value, bool>::type
76 ContainsKeys(const std::map<Key, Value> &container, T key) {
77  return container.find(static_cast<Key>(key)) != end(container);
78 }
79 
81 template <typename T, typename Key>
82 typename std::enable_if<std::is_convertible<Key, size_t>::value, bool>::type
83 ContainsKeys(const std::vector<T> &container, Key key) {
84  return container.size() > static_cast<size_t>(key);
85 }
86 
88 template <typename T, typename Key, typename... Keys, typename Value>
89 typename std::enable_if<std::is_convertible<T, Key>::value, bool>::type
90 ContainsKeys(const std::map<Key, Value> &container, T key, Keys... keys) {
91  if (container.find(static_cast<Key>(key)) != end(container)) {
92  return ContainsKeys(container.at(key), keys...);
93  }
94 
95  return false;
96 }
97 
99 template <typename T, typename Key, typename... Keys>
100 typename std::enable_if<std::is_convertible<Key, size_t>::value, bool>::type
101 ContainsKeys(const std::vector<T> &container, Key key, Keys... keys) {
102  if (container.size() > static_cast<size_t>(key))
103  return ContainsKeys(container[key], keys...);
104 
105  return false;
106 }
107 
108 } // namespace NAIA
109 #endif
void operator()() const
Definition: Utils.hpp:22
LimitedWarning(std::string message, std::string source)
Definition: Utils.hpp:20
const LimitedWarning< 10 > keyexists_deprecation_msg
Definition: Utils.cpp:3
std::enable_if< std::is_convertible< T, Key >::value, bool >::type KeyExists(T key, const std::map< Key, Value > &container)
Definition: Utils.hpp:60
std::enable_if< std::is_convertible< Key, size_t >::value, bool >::type ContainsKeys(const std::array< T, N > &container, Key key)
Definition: Utils.hpp:53
NAIAChain::EventItr end(NAIAChain &chain)
Definition: NAIAChain.h:298
auto getLogger(const std::string &fnName)
Create a new logger with a given function name.
Definition: Logging.h:18