-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathCommons.h
64 lines (51 loc) · 1.59 KB
/
Commons.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
* Commons.h
*
* Created on: Dec 29, 2020
* Author: <a href="mailto:damirlj@yahoo.com">Damir Ljubic</a>
*/
#ifndef COMMONS_COMMONS_H_
#define COMMONS_COMMONS_H_
#include <iostream>
#include <utility>
#include <type_traits>
#include <exception>
namespace utils
{
/**
* For converting the stronged-named enumeration value (enum class)
* into underlying type (enum class <name> : <underlying_type>)
*
* @tparam E stronged-named enumeration type (enum class)
* @param e stonged-named enumeration value
* @return The underlying type (enum class <name> : <underlying_type>{};)
*/
template<class E>
constexpr auto toUType(E e) noexcept
{
return static_cast<std::underlying_type_t<E>>(e);
}
template<typename ... Args>
std::string string_format( const std::string& format, Args&&...args )
{
const auto rv = std::snprintf( nullptr
, 0
, format.c_str()
, std::forward<Args>(args)... ) + 1; // Extra space for '\0'
if (rv <= 0 )
{
throw std::runtime_error( "Error while obtaining the size." );
}
const auto size = static_cast<std::size_t>(rv);
std::string buffer (size, 0);
if (std::snprintf(&buffer[0]
, size
, format.c_str()
, std::forward<Args>(args)...) <= 0)
{
throw std::runtime_error( "Error while formatting." );
}
return buffer.erase(size-1, 1); // We don't want the '\0' inside
}
}
#endif /* COMMONS_COMMONS_H_ */