ifw-core 6.0.0
Loading...
Searching...
No Matches
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 <ciiLogManager.hpp>
18
20
21
22namespace ifw {
23
25 // TODO: Remove this and replace calls to this function with (fmt::print(). fmtlib).
26 std::string Write(const char* format, ...);
27
28}
29
30#define IFW_LOCATION {\
31 std::string(__FILE__), \
32 std::string(__FUNCTION__), \
33 std::to_string(__LINE__)}
34
35
36namespace ifw::core::utils::base {
37
39 std::string ResolvePath(const std::string& filename);
40
42 std::string GenUniqueId(const std::string& prefix = "");
43
45 template <class TYPE>
46 void CleanVector(const std::vector<TYPE>& vect,
47 const TYPE& pattern,
48 std::vector<TYPE>& clean_vect) {
49 clean_vect.clear();
50 for (const TYPE& el : vect) {
51 if (el != pattern) {
52 clean_vect.insert(el);
53 }
54 }
55 }
56
59 template <class TYPE>
60 void CheckRange(const std::string& par,
61 TYPE value,
62 TYPE lower_limit,
63 TYPE upper_limit);
64
67 template <class TYPE>
68 void CheckRange(const std::string& par,
69 const TYPE& value,
70 std::vector<TYPE>& valid_values);
71
73 template <class TYPE>
74 int32_t ElInVector(const std::vector<TYPE>& vect,
75 const TYPE& value) {
76 int32_t i = 0;
77 for (auto it = vect.begin(); it != vect.end(); it++, i++) {
78 if (*it == value) {
79 return i;
80 }
81 }
82 return -1;
83 }
84
86 template <class TYPE>
87 std::string DumpVector(const std::vector<TYPE>& vector,
88 const std::string& separator = "\n") {
89 std::stringstream tmp_buf;
90 int32_t i = 0;
91 for (auto it = vector.begin(); it != vector.end(); it++, i++) {
92 tmp_buf << *it << separator;
93 }
94 return tmp_buf.str();
95 }
96
98 template <class MAP_TYPE>
99 bool ElInMap(const std::string& key,
100 const MAP_TYPE& map) {
101 return (map.find(key) != map.end());
102 }
103
104 namespace testutils {
105
106 // TODO: Remove this function not needed (can use WAF_MODULE_PATH instead).
109 std::string GetResDir(const std::string& module_path,
110 const std::string& test_res_dir); // = "test/resource");
111
112 // TODO: Remove this function not needed (can use WAF_MODULE_PATH instead).
114 void SetRootEnvVars(const std::string& module_path,
115 const std::string& test_res_dir, // = "test/resource",
116 const std::vector<std::string>& cfgpath_dirs);
117
118 } // namespace testutils
119
120}
121
123void IFW_DEBUG(const char* format, ...);
124
125#endif // IFW_CORE_UTILS_TOOLS_HPP_
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:80
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:113
Definition base.hpp:33
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:46
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:57
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:87
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:74
bool ElInMap(const std::string &key, const MAP_TYPE &map)
Check if a given key is contained in an STL map.
Definition tools.hpp:99
std::string ResolvePath(const std::string &filename)
Resolve the filename if it contains env. variables, "~" and relative paths.
Definition tools.cpp:29
Definition defines.cpp:12
std::string Write(const char *format,...)
Create formatted string, return formatted string (C formatting convention).
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:157