Utopia  2
Framework for studying models of complex & adaptive systems.
utils.hh
Go to the documentation of this file.
1 #ifndef UTOPIA_CORE_TESTTOOLS_UTILS_HH
2 #define UTOPIA_CORE_TESTTOOLS_UTILS_HH
3 
4 #include <iostream>
5 #include <filesystem>
6 #include <string>
7 #include <string_view>
8 
9 #include <spdlog/spdlog.h> // fmt library included here
10 
16 namespace Utopia::TestTools {
17 
19 bool contains (const std::string_view s, const std::string_view match) {
20  return not (s.find(match) == std::string_view::npos);
21 }
22 
23 
25 struct LocationInfo {
27  std::size_t line = 0;
28 
30  std::filesystem::path file_path = "";
31 
33 
35  std::string_view fstr = "@ {file_name:}::{line:d} : ";
36 
38  LocationInfo() = default;
39 
41  LocationInfo(const std::size_t line, const std::string_view file_path)
42  : line(line)
44  {}
45 
47 
49  std::string string () const {
50  if (file_path.empty()) {
51  return "";
52  }
53 
54  using namespace fmt::literals;
55  return fmt::vformat(
56  fstr,
57  fmt::make_format_args(
58  "file_path"_a=file_path.string(),
59  "file_name"_a=file_path.filename().string(),
60  "line"_a=line
61  )
62  );
63  }
64 };
65 
67 std::ostream& operator<< (std::ostream& out, const LocationInfo& loc) {
68  out << loc.string();
69  return out;
70 }
71 
72 } // namespace Utopia::TestTools
73 
74 // end group TestTools
79 #endif // UTOPIA_CORE_TESTTOOLS_UTILS_HH
Definition: config.hh:19
std::ostream & operator<<(std::ostream &out, const LocationInfo &loc)
Overload for allowing LocationInfo output streams.
Definition: utils.hh:67
bool contains(const std::string_view s, const std::string_view match)
Returns true if the match string is contained within the given string.
Definition: utils.hh:19
Bundles and handles file location information: file path and line number.
Definition: utils.hh:25
std::string_view fstr
A fmt library format string.
Definition: utils.hh:35
std::string string() const
A string representation of the location, using the fstr member.
Definition: utils.hh:49
std::size_t line
Some line, e.g. as provided by LINE macro.
Definition: utils.hh:27
LocationInfo()=default
Constructs a location object without information.
std::filesystem::path file_path
Some file path, e.g. as provided by FILE macro.
Definition: utils.hh:30
LocationInfo(const std::size_t line, const std::string_view file_path)
Constructs a location object from line and file path information.
Definition: utils.hh:41