Skip to content

Commit 22c3bba

Browse files
Centralizing the work of converting numbers to strings correctly.
1 parent 11ed8cf commit 22c3bba

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ set ( SRC_FILES
2727
renderer.cpp
2828
renderer.hpp
2929
statistics.cpp
30-
statistics.hpp )
30+
statistics.hpp
31+
string_util.cpp
32+
string_util.hpp )
3133

3234
add_executable( morebin ${SRC_FILES} )
3335
target_link_libraries ( morebin ${Boost_LIBRARIES} )

string_util.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include "string_util.hpp"
2+
3+
#include <stdint.h>
4+
#include <sstream>
5+
6+
using std::string;
7+
using std::stringstream;
8+
9+
template <typename NumT>
10+
const std::string toStr(const NumT number)
11+
{
12+
stringstream result;
13+
result << number;
14+
return result.str();
15+
}
16+
17+
template <>
18+
const std::string toStr(const uint8_t number)
19+
{
20+
stringstream result;
21+
result << static_cast<uint16_t>(number);
22+
return result.str();
23+
}
24+
25+
template <>
26+
const std::string toStr(const int8_t number)
27+
{
28+
stringstream result;
29+
result << static_cast<int16_t>(number);
30+
return result.str();
31+
}
32+
33+
//template const string toStr(const uint8_t number);
34+
//template const string toStr(const int8_t number);
35+
template const string toStr(const uint16_t number);
36+
template const string toStr(const int16_t number);
37+
template const string toStr(const uint32_t number);
38+
template const string toStr(const int32_t number);
39+
template const string toStr(const uint64_t number);
40+
template const string toStr(const int64_t number);
41+
template const string toStr(const float number);
42+
template const string toStr(const double number);

string_util.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef _STRING_UTIL_HPP
2+
#define _STRING_UTIL_HPP 1
3+
4+
#include <string>
5+
6+
template <typename NumT>
7+
const std::string toStr(const NumT number);
8+
9+
#endif

0 commit comments

Comments
 (0)