sapphirec
The Sapphire documentation
utils.h
Go to the documentation of this file.
1 /*
2  * @file utils.h
3  * Miscellaneous utilities to use in the compiler, such as string manipulation/conversion
4  * functions, and benchmarking utilities.
5  */
6 
7 #pragma once
9 #include "sapphirepch.h"
10 #include <chrono>
11 
12 /*
13  * Replaces all ocurrences of a substring with another one in a string.
14  * @param[in] str The input string.
15  * @param[in] from The substring to remove.
16  * @param[in] to The substring to add.
17  * @param[out] replaced Whether anything was replaced.
18  */
19 bool replace(std::string& str, const std::string& from, const std::string& to);
20 
21 /* Easy way of converting an int to a string without writing 5 lines of code every time you want to do it. */
22 std::string to_string(const int& value);
23 
24 /* Easy way of converting a float to a string without writing 5 lines of code every time you want to do it. */
25 std::string to_string(const float& value);
26 
27 /* Benchmarking utilities. */
28 
30 {
31  public:
32  __benchmark_impl(std::string_view __function_name);
34 
35  static void enable();
36  static void disable();
37 
38  static void init();
39 
40  private:
41  std::chrono::time_point<std::chrono::high_resolution_clock> m_StartTimePoint;
42  std::string_view m_FunctionName;
43  static bool m_BenchmarkingEnabled;
44  bool m_InternalBenchmarkingEnabled;
45 };
46 
47 #define benchmark(message) __benchmark_impl __benchmark_impl_timer(message)
Definition: utils.h:30
__benchmark_impl(std::string_view __function_name)
Definition: utils.cpp:26
static void disable()
Definition: utils.cpp:52
static void init()
Definition: utils.cpp:47
~__benchmark_impl()
Definition: utils.cpp:33
static void enable()
Definition: utils.cpp:57
bool replace(std::string &str, const std::string &from, const std::string &to)
Definition: utils.cpp:4
std::string to_string(const int &value)
Definition: utils.cpp:12