Skip to content

Commit d1ef1e7

Browse files
committed
refactor: move strfmt to own file Fmt.hpp
1 parent df8990a commit d1ef1e7

5 files changed

Lines changed: 29 additions & 17 deletions

File tree

include/CubicBezier.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#pragma once
2+
#include "Fmt.hpp"
23
#include "Math.hpp"
34
#include "Utils.hpp"
45

include/Fmt.hpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#pragma once
2+
3+
#include <cstdio>
4+
#include <stdexcept>
5+
#include <string>
6+
7+
namespace odr
8+
{
9+
10+
inline std::string strfmt(const char* fmt)
11+
{
12+
return fmt ? std::string(fmt) : std::string();
13+
}
14+
15+
template<class... Args, typename std::enable_if<(sizeof...(Args) > 0), int>::type = 0>
16+
std::string strfmt(const char* fmt, Args&&... args)
17+
{
18+
const int n = std::snprintf(nullptr, 0, fmt, std::forward<Args>(args)...);
19+
if (n < 0)
20+
throw std::runtime_error("formatting failed");
21+
std::string out(static_cast<size_t>(n), '\0'); // since c++11: str[str.size()] is '\0'
22+
std::snprintf(&out[0], out.size() + 1, fmt, std::forward<Args>(args)...);
23+
return out;
24+
}
25+
26+
} // namespace odr

include/Log.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#pragma once
2-
#include "Utils.hpp"
2+
#include "Fmt.hpp"
33

44
#include <atomic>
55
#include <cstdio>

include/Utils.hpp

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -276,22 +276,6 @@ inline std::vector<T> get_triangle_strip_outline_indices(const std::size_t num_v
276276
return out_indices;
277277
}
278278

279-
inline std::string strfmt(const char* fmt)
280-
{
281-
return fmt ? std::string(fmt) : std::string();
282-
}
283-
284-
template<class... Args, typename std::enable_if<(sizeof...(Args) > 0), int>::type = 0>
285-
std::string strfmt(const char* fmt, Args&&... args)
286-
{
287-
const int n = std::snprintf(nullptr, 0, fmt, std::forward<Args>(args)...);
288-
if (n < 0)
289-
throw std::runtime_error("formatting failed");
290-
std::string out(static_cast<size_t>(n), '\0'); // since c++11: str[str.size()] is '\0'
291-
std::snprintf(&out[0], out.size() + 1, fmt, std::forward<Args>(args)...);
292-
return out;
293-
}
294-
295279
template<class T, typename F>
296280
bool compare_class_members(const T& obj_a, const T& obj_b, F cmp)
297281
{

src/Lane.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "Lane.h"
2+
#include "Fmt.hpp"
23

34
#include <algorithm>
45
#include <iterator>

0 commit comments

Comments
 (0)