ifw-core  5.0.0-pre2
tools.hpp
Go to the documentation of this file.
1 
6 #ifndef IFW_CORE_UTILS_TOOLS_HPP_
7 #define IFW_CORE_UTILS_TOOLS_HPP_
8 
9 #include <limits.h>
10 #include <stdarg.h>
11 #include <string>
12 #include <map>
13 #include <list>
14 #include <fstream>
15 #include <variant>
16 
17 #include <core/utils/base/base.hpp>
18 
19 namespace ifw {
20 
22  // TODO: Remove this and replace calls to this function with (fmt::print(). fmtlib).
23  std::string Write(const char* format, ...);
24 
25 }
26 
27 #define IFW_LOCATION {\
28  std::string(__FILE__), \
29  std::string(__FUNCTION__), \
30  std::to_string(__LINE__)}
31 
32 namespace core::utils::base {
33 
35  std::string ResolvePath(const std::string& filename);
36 
38  std::string GenUniqueId(const std::string& prefix = "");
39 
41  template <class TYPE>
42  void CleanVector(const std::vector<TYPE>& vect,
43  const TYPE& pattern,
44  std::vector<TYPE>& clean_vect) {
45  clean_vect.clear();
46  for (const TYPE& el : vect) {
47  if (el != pattern) {
48  clean_vect.insert(el);
49  }
50  }
51  }
52 
55  template <class TYPE>
56  void CheckRange(const std::string& par,
57  TYPE value,
58  TYPE lower_limit,
59  TYPE upper_limit);
60 
63  template <class TYPE>
64  void CheckRange(const std::string& par,
65  const TYPE& value,
66  std::vector<TYPE>& valid_values);
67 
69  template <class TYPE>
70  int32_t ElInVector(const std::vector<TYPE>& vect,
71  const TYPE& value) {
72  int32_t i = 0;
73  for (auto it = vect.begin(); it != vect.end(); it++, i++) {
74  if (*it == value) {
75  return i;
76  }
77  }
78  return -1;
79  }
80 
82  template <class TYPE>
83  std::string DumpVector(const std::vector<TYPE>& vector,
84  const std::string& separator = "\n") {
85  std::stringstream tmp_buf;
86  int32_t i = 0;
87  for (auto it = vector.begin(); it != vector.end(); it++, i++) {
88  tmp_buf << *it << separator;
89  }
90  return tmp_buf.str();
91  }
92 
94  template <class MAP_TYPE>
95  bool ElInMap(const std::string& key,
96  const MAP_TYPE& map) {
97  return (map.find(key) != map.end());
98  }
99 
100  namespace testutils {
101 
102  // TODO: Remove this function not needed (can use WAF_MODULE_PATH instead).
105  std::string GetResDir(const std::string& module_path,
106  const std::string& test_res_dir); // = "test/resource");
107 
108  // TODO: Remove this function not needed (can use WAF_MODULE_PATH instead).
110  void SetRootEnvVars(const std::string& module_path,
111  const std::string& test_res_dir, // = "test/resource",
112  const std::vector<std::string>& cfgpath_dirs);
113 
114  } // namespace testutils
115 
116 }
117 
119 void IFW_DEBUG(const char* format, ...);
120 
121 #endif // IFW_CORE_UTILS_TOOLS_HPP_
void SetRootEnvVars(const std::string &module_path, const std::string &test_res_dir, const std::vector< std::string > &cfgpath_dirs)
Set the root environment variables: CFGPATH, INTROOT, DATAROOT.
Definition: tools.cpp:114
std::string GetResDir(const std::string &module_path, const std::string &test_res_dir)
Derives the "resource" directory from the current working point.
Definition: tools.cpp:81
Definition: base.hpp:28
std::string ResolvePath(const std::string &filename)
Resolve the filename if it contains env. variables, "~" and relative paths.
Definition: tools.cpp:30
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:83
void CleanVector(const std::vector< TYPE > &vect, const TYPE &pattern, std::vector< TYPE > &clean_vect)
Remove elements from list, which are equal to "pattern".
Definition: tools.hpp:42
void CheckRange(const std::string &par, TYPE value, TYPE lower_limit, TYPE upper_limit)
Check if a value is within a given range.
std::string GenUniqueId(const std::string &prefix="")
Generate a unique UUID based ID. A prefix may be prepended, if requested.
Definition: tools.cpp:58
bool ElInMap(const std::string &key, const MAP_TYPE &map)
Check if a given key is contained in an STL map.
Definition: tools.hpp:95
int32_t ElInVector(const std::vector< TYPE > &vect, const TYPE &value)
Check for specific element in the list. Return index if found, otherwise -1.
Definition: tools.hpp:70
Definition: tools.hpp:19
std::string Write(const char *format,...)
Create formatted string, return formatted string (C formatting convention).
Definition: testutils.py:1
void IFW_DEBUG(const char *format,...)
Print out a temporary debug log. These kind of logs should be removed from the code.
Definition: tools.cpp:172