NAIA
 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, typename Key, typename Value>
52 typename std::enable_if<std::is_convertible<T, Key>::value, bool>::type
53 KeyExists(T key, const std::map<Key, Value> &container) {
55  return container.find(static_cast<Key>(key)) != end(container);
56 }
57 
59 template <typename T, typename Value>
60 typename std::enable_if<std::is_convertible<T, size_t>::value, bool>::type
61 KeyExists(T key, const std::vector<Value> &container) {
63  return container.size() > static_cast<size_t>(key);
64 }
65 
67 template <typename T, typename Key, typename Value>
68 typename std::enable_if<std::is_convertible<T, Key>::value, bool>::type
69 ContainsKeys(const std::map<Key, Value> &container, T key) {
70  return container.find(static_cast<Key>(key)) != end(container);
71 }
72 
74 template <typename T, typename Key>
75 typename std::enable_if<std::is_convertible<Key, size_t>::value, bool>::type
76 ContainsKeys(const std::vector<T> &container, Key key) {
77  return container.size() > static_cast<size_t>(key);
78 }
79 
81 template <typename T, typename Key, typename... Keys, typename Value>
82 typename std::enable_if<std::is_convertible<T, Key>::value, bool>::type
83 ContainsKeys(const std::map<Key, Value> &container, T key, Keys... keys) {
84  if (container.find(static_cast<Key>(key)) != end(container)) {
85  return ContainsKeys(container.at(key), keys...);
86  }
87 
88  return false;
89 }
90 
92 template <typename T, typename Key, typename... Keys>
93 typename std::enable_if<std::is_convertible<Key, size_t>::value, bool>::type
94 ContainsKeys(const std::vector<T> &container, Key key, Keys... keys) {
95  if (container.size() > static_cast<size_t>(key))
96  return ContainsKeys(container[key], keys...);
97 
98  return false;
99 }
100 
101 } // namespace NAIA
102 #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
SingleTreeChain::EventItr end(SingleTreeChain &chain)
std::enable_if< std::is_convertible< T, Key >::value, bool >::type KeyExists(T key, const std::map< Key, Value > &container)
Definition: Utils.hpp:53
std::enable_if< std::is_convertible< T, Key >::value, bool >::type ContainsKeys(const std::map< Key, Value > &container, T key)
Definition: Utils.hpp:69
auto getLogger(const std::string &fnName)
Create a new logger with a given function name.
Definition: Logging.h:18