27 */ |
27 */ |
28 |
28 |
29 #ifndef BOTC_FORMAT_H |
29 #ifndef BOTC_FORMAT_H |
30 #define BOTC_FORMAT_H |
30 #define BOTC_FORMAT_H |
31 |
31 |
32 #include "string.h" |
32 #include "stringClass.h" |
33 #include "list.h" |
33 #include "list.h" |
|
34 #include "enumstrings.h" |
|
35 |
|
36 String MakeFormatArgument (const String& a) |
|
37 { |
|
38 return a; |
|
39 } |
|
40 |
|
41 String MakeFormatArgument (char a) |
|
42 { |
|
43 return String (a); |
|
44 } |
|
45 |
|
46 String MakeFormatArgument (int a) |
|
47 { |
|
48 return String::fromNumber (a); |
|
49 } |
|
50 |
|
51 String MakeFormatArgument (long a) |
|
52 { |
|
53 return String::fromNumber (a); |
|
54 } |
|
55 |
|
56 String MakeFormatArgument (size_t a) |
|
57 { |
|
58 return String::fromNumber (long (a)); |
|
59 } |
|
60 |
|
61 String MakeFormatArgument (const char* a) |
|
62 { |
|
63 return a; |
|
64 } |
|
65 |
|
66 String MakeFormatArgument (const void* a) |
|
67 { |
|
68 String text; |
|
69 text.sprintf ("%p", a); |
|
70 return text; |
|
71 } |
|
72 |
|
73 String MakeFormatArgument (std::nullptr_t) |
|
74 { |
|
75 return "(nullptr)"; |
|
76 } |
|
77 |
|
78 template<class T> |
|
79 String MakeFormatArgument (List<T> const& list) |
|
80 { |
|
81 String result; |
|
82 |
|
83 if (list.isEmpty()) |
|
84 return "{}"; |
|
85 |
|
86 result = "{ "; |
|
87 |
|
88 for (auto it = list.begin(); it != list.end(); ++it) |
|
89 { |
|
90 if (it != list.begin()) |
|
91 result += ", "; |
|
92 |
|
93 result += MakeFormatArgument (*it); |
|
94 } |
|
95 |
|
96 result += " }"; |
|
97 return result; |
|
98 } |
34 |
99 |
35 class FormatArgument |
100 class FormatArgument |
36 { |
101 { |
37 public: |
102 public: |
38 FormatArgument (const String& a) : m_text (a) {} |
103 template<typename T> |
39 FormatArgument (char a) : m_text (a) {} |
104 FormatArgument (const T& a) : |
40 FormatArgument (int a) : m_text (String::fromNumber (a)) {} |
105 m_text (MakeFormatArgument (a)) {} |
41 FormatArgument (long a) : m_text (String::fromNumber (a)) {} |
|
42 FormatArgument (size_t a) : m_text (String::fromNumber ((long) a)) {} |
|
43 FormatArgument (const char* a) : m_text (a) {} |
|
44 |
|
45 FormatArgument (void* a) |
|
46 { |
|
47 m_text.sprintf ("%p", a); |
|
48 } |
|
49 |
|
50 FormatArgument (const void* a) |
|
51 { |
|
52 m_text.sprintf ("%p", a); |
|
53 } |
|
54 |
|
55 FormatArgument (std::nullptr_t) : |
|
56 m_text (FormatArgument ((void*) 0).text()) {} |
|
57 |
|
58 template<class T> FormatArgument (const List<T>& list) |
|
59 { |
|
60 if (list.isEmpty()) |
|
61 { |
|
62 m_text = "{}"; |
|
63 return; |
|
64 } |
|
65 |
|
66 m_text = "{ "; |
|
67 |
|
68 for (const T& a : list) |
|
69 { |
|
70 if (&a != &list[0]) |
|
71 m_text += ", "; |
|
72 |
|
73 m_text += FormatArgument (a).text(); |
|
74 } |
|
75 |
|
76 m_text += " }"; |
|
77 } |
|
78 |
106 |
79 inline const String& text() const |
107 inline const String& text() const |
80 { |
108 { |
81 return m_text; |
109 return m_text; |
82 } |
110 } |