to_string for c++
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.travis.yml Update .travis.yml Jan 7, 2017
Makefile Update Makefile Jan 7, 2017
README.md Update README.md Oct 6, 2017
catch.hpp Add files via upload Jan 7, 2017
tests.cpp Update tests.cpp Nov 24, 2016
to.h Update to.h Oct 8, 2018
to.png logo Jul 14, 2015

README.md

to.cpp

brings type conversion features in modern C++.

to::_types

std::cout<< to::_int("4132"); // 4132
std::cout<< to::_unsigned_int("9999999999999999999999999999"); // out_of_range exception
  • Supported Types
    • signed/unsigned int
    • signed/unsigned long
    • 나중에 추가

to::_string

Fundamental types

int v = 1234;
std::cout<< to::_string(v); // 1234
std::cout<< to::_string(4.44f); // 4.44

bool b = true;
std::cout<< to::_string(b); // true

// 포인터는 타입 이름 + 주소값으로 변환됩니다.
int *ptr = &v;
std::cout<< to::_string(v); // #<int * 0000abcd>

std::cout<< to::_string(nullptr); // #<std::nullptr_t 00000000>

// char *, const char *, char [], const char[] 타입은 포인터로 취급되지 않고 문자열로 취급됩니다.
char *s = "hello world";
std::cout<< to::_string(s); // hello world

STL Containers

std::vector<int> v({1,2,3,4});
std::cout<< to::_string(v); // [1, 2, 3, 4]

std::map<std::string, std::string> m({
  {"name", "park"},
  {"chicken", "good"}});
std::cout<< to::_string(m); // {name : park, chicken : good}

std::map<std::string, std::vector<std::string>> m2({
  {"fruits", {"apple", "banana", "orange"}},
	{"colors", {"yellow", "red", "blue"}}});
std::cout<< to::_string(m2); // {fruits : [apple, banana, orange], colors : [yellow, red, blue]}

std::string str = "hello world";
std::cout<< to::_string(str); // hello world

std::cout<< to::_string({1,2,3,4}); // [1, 2, 3, 4]
std::cout<< to::_string({{1,2}, {3,4}}); // not implemented yet

std::initializer_list<std::initializer_list<int>> l({{1,2}, {3,4}});
std::cout<< to::_string(l); // [[1, 2], [3, 4]]

Custom Classes
You can make a custom string converter for your class by implementing the to_string method.

class Foo{
public:
  int v;
  
  std::string to_string() const{
    return "#<Foo v:" + to::_string(v) + ">";
  }
}
Foo f;
f.v = 15;

std::cout<< to::_string(f); // #<Foo v:15>

ToString Generator
It also have a convenient macro which auto-implements to_string method.

class weapon {
public:
  CREATE_TO_STRING(damage, level)

  int damage = 123;
  int level = 1;
};
class item {
public:

CREATE_TO_STRING(name, qty)

  std::string name;
  int qty = 5;
};
class player {
public:
  CREATE_TO_STRING(hp, weapon, inventory)

  int hp = 100;
  weapon weapon;
  std::vector<item> inventory;
  int a_secret_one = 1234;
};
player a;
item item1, item2;
item1.name = "SWORD";
item2.name = "CHICKEN";
a.inventory.push_back(item1);
a.inventory.push_back(item2);
printf("%s\n", a.to_string().c_str());

출력 결과물 (가독성을 위해 수동으로 줄바꿈 하였습니다, 실제 결과는 줄바꿈이 없습니다.)

#<class player
inventory : [
  #<class item  qty : 5, name : SWORD>,
  #<class item  qty : 5, name : CHICKEN>], 
weapon : #<class weapon  level : 1, damage : 123>,
hp : 100>

If no conversion is available, type name and address value will be returned.
Bar b;
std::cout<< to::_string(b); // #<Bar 0x00001234>

has_string_converter
Checks whether there is a suitable available string converters or not.

/* int형은 내장 변환기가 있으므로 true */
std::cout<< to::has_string_converter<int>::value; // true

/* Foo 클래스는 to_string 메소드를 구현해 커스텀 변환기를 만들었으므로 true */
std::cout<< to::has_string_converter<Foo>::value; // true
/* Bar 클래스는 구현하지 않았으므로 false */
std::cout<< to::has_string_converter<Bar>::value; // false

Options

  • TO_STRING_WITH_QMARKS
    std::string, char *, const char *형에 대해 to::_string을 수행할 때 문자열의 앞뒤로 "를 삽입합니다.
    이 옵션을 활용하면 STL 컨테이너에서 json 스트링을 빌드할 수 있습니다.

    #define TO_STRING_WITH_QMARKS
    
    std::map<std::string, std::vector<std::string>> m2({
      {"fruits", {"apple", "banana", "orange"}},
    	{"colors", {"yellow", "red", "blue"}}});
    std::cout<< to::_string(m2); // {"fruits" : ["apple", "banana", "orange"], "colors" : ["yellow", "red", "blue"]}

TODO

  • ToString 생성기에 상속 지원