ifw-core  2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
tools.hpp
Go to the documentation of this file.
1 
9 #ifndef IFW_CTD_TOOLS_HPP_
10 #define IFW_CTD_TOOLS_HPP_
11 
12 #include <limits.h>
13 #include <stdarg.h>
14 #include <string>
15 #include <map>
16 #include <list>
17 #include <fstream>
18 #include <variant>
19 
20 #include "rad/logger.hpp"
21 #include "rad/exceptions.hpp"
22 
23 // TODO: Should document which exceptions are thrown in the Doxygen doc (@throw <ex>).
24 
25 namespace ifw {
26 
28  // TODO: Check if can be replaced with functions from fmtlib.net.
29  std::string Write(const char* format, ...);
30 
31 }
32 
33 #define IFW_LOCATION {\
34  std::string(__FILE__), \
35  std::string(__FUNCTION__), \
36  std::to_string(__LINE__)}
37 
38 
39 // TODO: Use "namespace ctd::defines {" (in CTD, DIT, TCCS).
40 namespace ctd {
41  namespace defines {
42 
44  std::string GenUniqueId(const std::string& prefix = "");
45 
47  // template <class TYPE>
48  // class List: public std::list<TYPE> {
49  // public:
50  // TYPE& operator[] (const int32_t index) {
51  // auto list_it = this->begin();
52  // std::advance(list_it, index);
53  // return *list_it;
54  // }
55  // };
56 
58  template <class TYPE>
59  void CleanVector(const std::vector<TYPE>& vect,
60  const TYPE& pattern,
61  std::vector<TYPE>& clean_vect) {
62  clean_vect.clear();
63  for (const TYPE& el : vect) {
64  if (el != pattern) {
65  clean_vect.insert(el);
66  }
67  }
68 
69  }
70 
73  template <class TYPE>
74  void CheckRange(const std::string& par,
75  TYPE value,
76  TYPE lower_limit,
77  TYPE upper_limit);
78 
81  template <class TYPE>
82  void CheckRange(const std::string& par,
83  const TYPE& value,
84  std::vector<TYPE>& valid_values);
85 
86  // /// @brief Check for specific element in the list.
87  // template <class TYPE>
88  // int32_t ElInList(const List<TYPE>& list,
89  // const TYPE& value) {
90  // RAD_LOG_TRACE();
91 
92  // int32_t i = 0;
93  // for (auto list_it = list.begin(); list_it != list.end(); list_it++, i++) {
94  // if (*list_it == value) {
95  // return i;
96  // }
97  // }
98  // return -1;
99  // }
100 
102  template <class TYPE>
103  std::string DumpVector(const std::vector<TYPE>& vector,
104  const std::string& separator = "\n") {
105  RAD_LOG_TRACE();
106 
107  std::stringstream tmp_buf;
108  int32_t i = 0;
109  for (auto it = vector.begin(); it != vector.end(); it++, i++) {
110  tmp_buf << *it << separator;
111  }
112  return tmp_buf.str();
113  }
114 
116  template <class MAP_TYPE>
117  bool ElInMap(const std::string& key,
118  const MAP_TYPE& map) {
119  RAD_LOG_TRACE();
120 
121  return (map.find(key) != map.end());
122  }
123 
124  }
125 }
126 
128 void IFW_DEBUG(const char* format, ...);
129 
130 
131 #endif // IFW_CTD_DEFINES_HPP_
std::string DumpVector(const std::vector< TYPE > &vector, const std::string &separator="\n")
Dump contents of a list into a string buffer.
Definition: tools.hpp:103
std::string GenUniqueId(const std::string &prefix)
Generate a unique UUID based ID. A prefix may be prepended, if requested.
Definition: defines.cpp:15
bool ElInMap(const std::string &key, const MAP_TYPE &map)
Check if a given key is contained in an STL map.
Definition: tools.hpp:117
std::string Write(const char *format,...)
Create formatted string, return formatted string (C formatting convention).
Definition: defines.cpp:54
void CleanVector(const std::vector< TYPE > &vect, const TYPE &pattern, std::vector< TYPE > &clean_vect)
@ brief STL like list that can be indexed with []&#39;s.
Definition: tools.hpp:59
void IFW_DEBUG(const char *format,...)
Print out a temporary debug log. These kind of logs should be removed from the code.
Definition: defines.cpp:71
void CheckRange(const std::string &par, TYPE value, TYPE lower_limit, TYPE upper_limit)
Check if a value is within a given range.