File: stringify.hpp

package info (click to toggle)
wf-config 0.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 424 kB
  • sloc: cpp: 5,743; xml: 37; makefile: 4
file content (56 lines) | stat: -rw-r--r-- 978 bytes parent folder | download | duplicates (3)
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
#pragma once

#include <string>
#include <sstream>

namespace wf
{
namespace log
{
/**
 * Convert the given parameter to a string which can be logged.
 * This function can be specialized for custom types.
 */
template<class T>
std::string to_string(T arg)
{
    std::ostringstream out;
    out << arg;
    return out.str();
}

/** Specialization for boolean arguments - print true or false. */
template<>
std::string to_string(bool arg);

/* Specialization for pointers - print the address */
template<class T>
std::string to_string(T *arg)
{
    if (!arg)
    {
        return "(null)";
    }

    return to_string<T*>(arg);
}

namespace detail
{
/**
 * Convert each argument to a string and then concatenate them.
 */
template<class First>
std::string format_concat(First arg)
{
    return wf::log::to_string(arg);
}

template<class First, class... Args>
std::string format_concat(First first, Args... args)
{
    return format_concat(first) + format_concat(args...);
}
}
}
}