Skip to content

Commit

Permalink
feat: HasStdToString
Browse files Browse the repository at this point in the history
  • Loading branch information
Dup4 committed Apr 20, 2022
1 parent fe82e97 commit dd32841
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 61 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ debug:
cmake --build build -j

tests:
find ./build/test -name "*.gcda" -print0 | xargs -0 rm
cmake -S. -Bbuild -DCMAKE_BUILD_TYPE=Release -DBENCHMARK_ENABLE_TESTING=OFF -DCMAKE_EXPORT_COMPILE_COMMANDS=1 -Dsnapshot_build_tests=ON
cmake --build build -j

Expand Down
68 changes: 19 additions & 49 deletions include/snapshot/snapshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,25 @@ class StringUtility {

public:
template <typename T>
class HasStdToString {
private:
template <typename U>
static auto check(int) -> decltype(std::to_string(std::declval<U>()), std::true_type());

template <typename U>
static std::false_type check(...);

public:
enum { value = std::is_same<decltype(check<T>(0)), std::true_type>::value };
};

public:
template <typename T, std::enable_if_t<HasStdToString<T>::value, bool> = true>
static std::string ToString(const T& t) {
return std::to_string(t);
}

template <typename T, std::enable_if_t<!HasStdToString<T>::value, bool> = true>
static std::string ToString(const T& t) {
std::stringstream ss;
std::string s;
Expand All @@ -71,42 +90,6 @@ class StringUtility {
return ss.str();
}

static std::string ToString(int value) {
return std::to_string(value);
}

static std::string ToString(long value) {
return std::to_string(value);
}

static std::string ToString(long long value) {
return std::to_string(value);
}

static std::string ToString(unsigned value) {
return std::to_string(value);
}

static std::string ToString(unsigned long value) {
return std::to_string(value);
}

static std::string ToString(unsigned long long value) {
return std::to_string(value);
}

static std::string ToString(float value) {
return std::to_string(value);
}

static std::string ToString(double value) {
return std::to_string(value);
}

static std::string ToString(long double value) {
return std::to_string(value);
}

static std::string ToString(bool value) {
return value ? "true" : "false";
}
Expand Down Expand Up @@ -220,19 +203,6 @@ class StringUtility {

return false;
}

template <typename T>
class hasStdToString {
private:
template <typename U>
static auto check(int) -> decltype(std::to_string(std::declval<U>()), std::true_type());

template <typename U>
static std::false_type check(...);

public:
enum { value = std::is_same<decltype(check<T>(0)), std::true_type>::value };
};
};

class FileUtility {
Expand Down
40 changes: 28 additions & 12 deletions test/string_utility_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@
#include <iostream>
#include <limits>

#include "snapshot/snapshot.h"

using namespace std;

namespace snapshot {

class StringUtilityTest : public testing::Test {
public:
template <typename T>
using HasStdToString = StringUtility::hasStdToString<T>;
};

class CustomToString {
Expand All @@ -26,6 +22,27 @@ class CustomToString {
}
};

class CustomToString1 {
public:
int x;
int y;
int z;
};

} // namespace snapshot

using namespace snapshot;

namespace std {

string to_string(const CustomToString1& c) {
return to_string(c.x) + " " + to_string(c.y) + " " + to_string(c.z) + "\n";
}

} // namespace std

#include "snapshot/snapshot.h"

TEST_F(StringUtilityTest, to_string) {
EXPECT_EQ(StringUtility::ToString(numeric_limits<int>::min()), std::string("-2147483648"));
EXPECT_EQ(StringUtility::ToString(numeric_limits<int>::max()), std::string("2147483647"));
Expand Down Expand Up @@ -86,6 +103,13 @@ TEST_F(StringUtilityTest, to_string) {
c.z = 3;

EXPECT_EQ(StringUtility::ToString(c), "1 2 3\n");

CustomToString1 c1;
c1.x = 1;
c1.y = 2;
c1.z = 3;

EXPECT_EQ(StringUtility::ToString(c1), "1 2 3\n");
}

TEST_F(StringUtilityTest, split_and_join) {
Expand Down Expand Up @@ -133,11 +157,3 @@ TEST_F(StringUtilityTest, split_and_join) {
EXPECT_EQ(StringUtility::Join(v, '.'), s);
}
}

TEST_F(StringUtilityTest, has_std_to_string) {
EXPECT_TRUE(StringUtilityTest::HasStdToString<int>::value);

EXPECT_FALSE(StringUtilityTest::HasStdToString<CustomToString>::value);
}

} // namespace snapshot

0 comments on commit dd32841

Please sign in to comment.