otsdaq  v2_03_00
StringMacros.h
1 #ifndef _ots_StringMacros_h_
2 #define _ots_StringMacros_h_
3 
4 #include "otsdaq-core/Macros/CoutMacros.h"
5 
6 #include <map>
7 #include <set>
8 #include <typeinfo> // operator typeid
9 #include <vector>
10 #include <memory> //shared_ptr
11 
12 namespace ots
13 {
15 {
16  private: // private constructor because all static members, should never instantiate
17  // this class
18  StringMacros(void);
19  ~StringMacros(void);
20 
21  public:
22  // Here is the list of static helper functions:
23  //
24  // wildCardMatch
25  // inWildCardSet
26  // getWildCardMatchFromMap
27  //
28  // decodeURIComponent
29  // convertEnvironmentVariables
30  // isNumber
31  // getNumber
32  // validateValueForDefaultStringDataType
33  //
34  // getSetFromString
35  // getVectorFromString
36  // getMapFromString
37  //
38  // setToString
39  // vectorToString
40  // mapToString
41  //
42  // demangleTypeName
43 
44  static bool wildCardMatch(const std::string& needle,
45  const std::string& haystack,
46  unsigned int* priorityIndex = 0);
47  static bool inWildCardSet(const std::string& needle,
48  const std::set<std::string>& haystack);
49 
50  //========================================================================================================================
51  // getWildCardMatchFromMap ~
52  // returns value if needle is in haystack otherwise throws exception
53  // (considering wildcards AND match priority as defined by
54  // StringMacros::wildCardMatch)
55  template<class T>
56  static T& getWildCardMatchFromMap(
57  const std::string& needle,
58  std::map<std::string, T>& haystack,
59  std::string* foundKey = 0); // defined in included .icc source
60 
61  static std::string decodeURIComponent(const std::string& data);
62  static std::string convertEnvironmentVariables(const std::string& data);
63  static bool isNumber(const std::string& stringToCheck);
64 
65  template<class T>
66  static bool getNumber(const std::string& s,
67  T& retValue); // defined in included .icc source
68  static std::string getTimestampString(const std::string& linuxTimeInSeconds);
69  static std::string getTimestampString(const time_t& linuxTimeInSeconds = time(0));
70 
71  // special validation ignoring any table info - just assuming type string
72  template<class T>
73  static T validateValueForDefaultStringDataType(
74  const std::string& value,
75  bool doConvertEnvironmentVariables = true); // defined in included .icc source
76  static std::string validateValueForDefaultStringDataType(
77  const std::string& value, bool doConvertEnvironmentVariables = true);
78 
79  static void getSetFromString(const std::string& inputString,
80  std::set<std::string>& setToReturn,
81  const std::set<char>& delimiter = {',', '|', '&'},
82  const std::set<char>& whitespace = {
83  ' ', '\t', '\n', '\r'});
84 
85  template<class T /*value type*/,
86  class S = std::string /*name string or const string*/>
87  static void getMapFromString(
88  const std::string& inputString,
89  std::map<S, T>& mapToReturn,
90  const std::set<char>& pairPairDelimiter = {',', '|', '&'},
91  const std::set<char>& nameValueDelimiter = {'=', ':'},
92  const std::set<char>& whitespace = {' ', '\t', '\n', '\r'});
93  static void getMapFromString(
94  const std::string& inputString,
95  std::map<std::string, std::string>& mapToReturn,
96  const std::set<char>& pairPairDelimiter = {',', '|', '&'},
97  const std::set<char>& nameValueDelimiter = {'=', ':'},
98  const std::set<char>& whitespace = {' ', '\t', '\n', '\r'});
99  static void getVectorFromString(
100  const std::string& inputString,
101  std::vector<std::string>& listToReturn,
102  const std::set<char>& delimiter = {',', '|', '&'},
103  const std::set<char>& whitespace = {' ', '\t', '\n', '\r'},
104  std::vector<char>* listOfDelimiters = 0);
105 
106  //*ToString declarations (template definitions are in included .icc source)
107  template<class T>
108  static std::string mapToString(const std::map<std::string, T>& mapToReturn,
109  const std::string& primaryDelimeter = ", ",
110  const std::string& secondaryDelimeter = ": ");
111  template<class T>
112  static std::string mapToString(
113  const std::map<std::pair<std::string, std::string>, T>& mapToReturn,
114  const std::string& primaryDelimeter = ", ",
115  const std::string& secondaryDelimeter = ": ");
116  template<class T>
117  static std::string mapToString(
118  const std::map<std::pair<std::string, std::pair<std::string, std::string>>, T>&
119  mapToReturn,
120  const std::string& primaryDelimeter = ", ",
121  const std::string& secondaryDelimeter = ": ");
122  template<class T>
123  static std::string mapToString(
124  const std::map<std::string, std::pair<std::string, T>>& mapToReturn,
125  const std::string& primaryDelimeter = ", ",
126  const std::string& secondaryDelimeter = ": ");
127  template<class T>
128  static std::string mapToString(
129  const std::map<std::string, std::map<std::string, T>>& mapToReturn,
130  const std::string& primaryDelimeter = ", ",
131  const std::string& secondaryDelimeter = ": ");
132  template<class T>
133  static std::string mapToString(const std::map<std::string, std::set<T>>& mapToReturn,
134  const std::string& primaryDelimeter = ", ",
135  const std::string& secondaryDelimeter = ": ");
136  template<class T>
137  static std::string mapToString(
138  const std::map<std::string, std::vector<T>>& mapToReturn,
139  const std::string& primaryDelimeter = ", ",
140  const std::string& secondaryDelimeter = ": ");
141  static std::string mapToString(const std::map<std::string, uint8_t>& mapToReturn,
142  const std::string& primaryDelimeter = ", ",
143  const std::string& secondaryDelimeter = ": ");
144 
145  template<class T>
146  static std::string setToString(const std::set<T>& setToReturn,
147  const std::string& delimeter = ", ");
148  static std::string setToString(const std::set<uint8_t>& setToReturn,
149  const std::string& delimeter = ", ");
150 
151  template<class T>
152  static std::string vectorToString(const std::vector<T>& setToReturn,
153  const std::string& delimeter = ", ");
154  static std::string vectorToString(const std::vector<uint8_t>& setToReturn,
155  const std::string& delimeter = ", ");
156  template<class S, class T>
157  static std::string vectorToString(const std::vector<std::pair<S, T>>& setToReturn,
158  const std::string& primaryDelimeter = "; ",
159  const std::string& secondaryDelimeter = ":");
160 
161  static std::string demangleTypeName(const char* name);
162  static std::string stackTrace();
163  static std::string exec(const char* cmd);
164 
165 }; // end StringMarcos static class
166 
167 #include "otsdaq-core/Macros/StringMacros.icc" //define template functions
168 
169 } // namespace ots
170 #endif