Chromium Code Reviews| Index: src/string-stream.h |
| diff --git a/src/string-stream.h b/src/string-stream.h |
| index 1c1d27a16beaa2404cddf6874e355a42318352b8..86697daf23c94ff925be135b341a4a222d18b6a0 100644 |
| --- a/src/string-stream.h |
| +++ b/src/string-stream.h |
| @@ -5,8 +5,6 @@ |
| #ifndef V8_STRING_STREAM_H_ |
| #define V8_STRING_STREAM_H_ |
| -#include <memory> |
| - |
| #include "src/allocation.h" |
| #include "src/handles.h" |
| #include "src/vector.h" |
| @@ -59,32 +57,33 @@ class FixedStringAllocator final : public StringAllocator { |
| class FmtElm final { |
| public: |
| - FmtElm(int value) : type_(INT) { // NOLINT |
| - data_.u_int_ = value; |
| - } |
| - explicit FmtElm(double value) : type_(DOUBLE) { |
| - data_.u_double_ = value; |
| - } |
| - FmtElm(const char* value) : type_(C_STR) { // NOLINT |
| - data_.u_c_str_ = value; |
| - } |
| - FmtElm(const Vector<const uc16>& value) : type_(LC_STR) { // NOLINT |
| - data_.u_lc_str_ = &value; |
| - } |
| - FmtElm(Object* value) : type_(OBJ) { // NOLINT |
| - data_.u_obj_ = value; |
| - } |
| - FmtElm(Handle<Object> value) : type_(HANDLE) { // NOLINT |
| - data_.u_handle_ = value.location(); |
| - } |
| - FmtElm(void* value) : type_(POINTER) { // NOLINT |
| - data_.u_pointer_ = value; |
| +#ifdef DEBUG |
|
titzer
2016/11/17 17:00:38
Could you do this macro stuff a different way? E.g
Clemens Hammacher
2016/11/17 18:40:01
Did it, and it's much cleaner indeed. Even though
|
| +#define FMT_ELM_CONS(attr, ctype, type, field, data) \ |
| + attr FmtElm(ctype value) : type_(type) { data_.field = data; } |
| +#else |
| +#define FMT_ELM_CONS(attr, ctype, type, field, data) \ |
| + attr FmtElm(ctype value) { data_.field = data; } |
| +#endif |
| + FMT_ELM_CONS(, int, INT, u_int_, value) |
| + FMT_ELM_CONS(explicit, double, DOUBLE, u_double_, value) |
| + FMT_ELM_CONS(, const char*, C_STR, u_c_str_, value) |
| + FMT_ELM_CONS(, const Vector<const uc16>&, LC_STR, u_lc_str_, &value) |
| + FMT_ELM_CONS(, Object*, OBJ, u_obj_, value) |
| + FMT_ELM_CONS(, Handle<Object>, HANDLE, u_handle_, value.location()) |
| + FMT_ELM_CONS(, void*, POINTER, u_pointer_, value) |
| +#undef FMT_ELM_CONS |
| + |
| + template <typename T> |
| + static FmtElm From(T t) { |
| + return t; |
| } |
| private: |
| friend class StringStream; |
| +#ifdef DEBUG |
| enum Type { INT, DOUBLE, C_STR, LC_STR, OBJ, HANDLE, POINTER }; |
| Type type_; |
| +#endif |
| union { |
| int u_int_; |
| double u_double_; |
| @@ -114,22 +113,19 @@ class StringStream final { |
| bool Put(String* str); |
| bool Put(String* str, int start, int end); |
| void Add(Vector<const char> format, Vector<FmtElm> elms); |
| - void Add(const char* format); |
| - void Add(Vector<const char> format); |
| - void Add(const char* format, FmtElm arg0); |
| - void Add(const char* format, FmtElm arg0, FmtElm arg1); |
| - void Add(const char* format, FmtElm arg0, FmtElm arg1, FmtElm arg2); |
| - void Add(const char* format, |
| - FmtElm arg0, |
| - FmtElm arg1, |
| - FmtElm arg2, |
| - FmtElm arg3); |
| - void Add(const char* format, |
| - FmtElm arg0, |
| - FmtElm arg1, |
| - FmtElm arg2, |
| - FmtElm arg3, |
| - FmtElm arg4); |
| + void Add(const char* format) { Add(CStrVector(format)); } |
| + void Add(Vector<const char> format) { Add(format, Vector<FmtElm>()); } |
| + |
| + template <typename... Args> |
| + void Add(const char* format, Args... args) { |
| + Add<Args...>(CStrVector(format), args...); |
| + } |
| + |
| + template <typename... Args> |
| + void Add(Vector<const char> format, Args... args) { |
| + FmtElm elems[]{FmtElm::From(args)...}; |
| + Add(format, ArrayVector(elems)); |
| + } |
| // Getting the message out. |
| void OutputToFile(FILE* out); |