123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431 |
- #ifndef BASE_TRACE_EVENT_TRACED_VALUE_H_
- #define BASE_TRACE_EVENT_TRACED_VALUE_H_
- #include <stddef.h>
- #include <memory>
- #include <sstream>
- #include <string>
- #include <vector>
- #include "base/macros.h"
- #include "base/pickle.h"
- #include "base/strings/string_piece.h"
- #include "base/trace_event/trace_event_impl.h"
- namespace base {
- class Value;
- namespace trace_event {
- class BASE_EXPORT TracedValue : public ConvertableToTraceFormat {
- public:
-
-
- explicit TracedValue(size_t capacity = 0);
- ~TracedValue() override;
- void EndDictionary();
- void EndArray();
-
- void SetInteger(const char* name, int value);
- void SetDouble(const char* name, double value);
- void SetBoolean(const char* name, bool value);
- void SetString(const char* name, base::StringPiece value);
- void SetValue(const char* name, TracedValue* value);
- void SetPointer(const char* name, void* value);
- void BeginDictionary(const char* name);
- void BeginArray(const char* name);
-
- void SetIntegerWithCopiedName(base::StringPiece name, int value);
- void SetDoubleWithCopiedName(base::StringPiece name, double value);
- void SetBooleanWithCopiedName(base::StringPiece name, bool value);
- void SetStringWithCopiedName(base::StringPiece name, base::StringPiece value);
- void SetValueWithCopiedName(base::StringPiece name, TracedValue* value);
- void SetPointerWithCopiedName(base::StringPiece name, void* value);
- void BeginDictionaryWithCopiedName(base::StringPiece name);
- void BeginArrayWithCopiedName(base::StringPiece name);
- void AppendInteger(int);
- void AppendDouble(double);
- void AppendBoolean(bool);
- void AppendString(base::StringPiece);
- void AppendPointer(void*);
- void BeginArray();
- void BeginDictionary();
-
- void AppendAsTraceFormat(std::string* out) const override;
- bool AppendToProto(ProtoAppender* appender) override;
- void EstimateTraceMemoryOverhead(TraceEventMemoryOverhead* overhead) override;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- class BASE_EXPORT ArrayScope {
- public:
- ArrayScope(const ArrayScope&) = delete;
- ArrayScope(ArrayScope&&) = default;
- ArrayScope& operator=(const ArrayScope&) = delete;
- ArrayScope& operator=(ArrayScope&&) = default;
- ~ArrayScope();
- private:
- explicit ArrayScope(TracedValue* value);
- TracedValue* value_;
- friend class TracedValue;
- };
-
-
- ArrayScope AppendArrayScoped() WARN_UNUSED_RESULT;
- ArrayScope BeginArrayScoped(const char* name) WARN_UNUSED_RESULT;
- ArrayScope BeginArrayScopedWithCopiedName(base::StringPiece name)
- WARN_UNUSED_RESULT;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- class BASE_EXPORT DictionaryScope {
- public:
- DictionaryScope(const DictionaryScope&) = delete;
- DictionaryScope(DictionaryScope&&) = default;
- DictionaryScope& operator=(const DictionaryScope&) = delete;
- DictionaryScope& operator=(DictionaryScope&&) = default;
- ~DictionaryScope();
- private:
- explicit DictionaryScope(TracedValue* value);
- TracedValue* value_;
- friend class TracedValue;
- };
-
-
- DictionaryScope AppendDictionaryScoped() WARN_UNUSED_RESULT;
- DictionaryScope BeginDictionaryScoped(const char* name) WARN_UNUSED_RESULT;
- DictionaryScope BeginDictionaryScopedWithCopiedName(base::StringPiece name)
- WARN_UNUSED_RESULT;
- class BASE_EXPORT Array;
- class BASE_EXPORT Dictionary;
- class BASE_EXPORT ValueHolder;
- class BASE_EXPORT ArrayItem;
- class BASE_EXPORT DictionaryItem;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- static std::unique_ptr<TracedValue> Build(
- const std::initializer_list<DictionaryItem> items);
-
-
-
-
-
-
-
-
- class Array {
- public:
-
-
- Array(const std::initializer_list<ArrayItem> items);
- Array(Array&&);
- void WriteToValue(TracedValue* value) const;
- private:
- std::initializer_list<ArrayItem> items_;
- };
-
- class Dictionary {
- public:
-
-
- Dictionary(const std::initializer_list<DictionaryItem> items);
- Dictionary(Dictionary&&);
- void WriteToValue(TracedValue* value) const;
- private:
- std::initializer_list<DictionaryItem> items_;
- };
-
-
-
-
-
- class ValueHolder {
- public:
-
-
- ValueHolder(int value);
- ValueHolder(double value);
- ValueHolder(bool value);
- ValueHolder(void* value);
-
-
-
- ValueHolder(base::StringPiece value);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- ValueHolder(std::string value);
-
-
-
- ValueHolder(const char* value);
- ValueHolder(Array& value);
- ValueHolder(Dictionary& value);
- ValueHolder(ValueHolder&&);
- protected:
- void WriteToValue(TracedValue* value) const;
- void WriteToValue(const char* name, TracedValue* value) const;
- private:
- union KeptValue {
-
-
- int int_value;
- double double_value;
- bool bool_value;
- base::StringPiece string_piece_value;
- std::string std_string_value;
- void* void_ptr_value;
- Array array_value;
- Dictionary dictionary_value;
-
-
- KeptValue() {}
- ~KeptValue() {}
- };
-
- enum class KeptValueType {
- kIntType,
- kDoubleType,
- kBoolType,
- kStringPieceType,
- kStdStringType,
- kVoidPtrType,
- kArrayType,
- kDictionaryType,
- };
- KeptValue kept_value_;
- KeptValueType kept_value_type_;
- };
-
- class ArrayItem : public ValueHolder {
- public:
-
-
-
- template <typename T>
-
- ArrayItem(T value) : ValueHolder(value) {}
- void WriteToValue(TracedValue* value) const;
- };
-
-
-
- class DictionaryItem : public ValueHolder {
- public:
-
- template <typename T>
- DictionaryItem(const char* name, T value)
- : ValueHolder(value), name_(name) {}
- void WriteToValue(TracedValue* value) const;
- private:
- const char* name_;
- };
-
-
-
-
-
- class BASE_EXPORT Writer {
- public:
- virtual ~Writer() = default;
- virtual void BeginArray() = 0;
- virtual void BeginDictionary() = 0;
- virtual void EndDictionary() = 0;
- virtual void EndArray() = 0;
-
- virtual void SetInteger(const char* name, int value) = 0;
- virtual void SetDouble(const char* name, double value) = 0;
- virtual void SetBoolean(const char* name, bool value) = 0;
- virtual void SetString(const char* name, base::StringPiece value) = 0;
- virtual void SetValue(const char* name, Writer* value) = 0;
- virtual void BeginDictionary(const char* name) = 0;
- virtual void BeginArray(const char* name) = 0;
-
- virtual void SetIntegerWithCopiedName(base::StringPiece name,
- int value) = 0;
- virtual void SetDoubleWithCopiedName(base::StringPiece name,
- double value) = 0;
- virtual void SetBooleanWithCopiedName(base::StringPiece name,
- bool value) = 0;
- virtual void SetStringWithCopiedName(base::StringPiece name,
- base::StringPiece value) = 0;
- virtual void SetValueWithCopiedName(base::StringPiece name,
- Writer* value) = 0;
- virtual void BeginDictionaryWithCopiedName(base::StringPiece name) = 0;
- virtual void BeginArrayWithCopiedName(base::StringPiece name) = 0;
- virtual void AppendInteger(int) = 0;
- virtual void AppendDouble(double) = 0;
- virtual void AppendBoolean(bool) = 0;
- virtual void AppendString(base::StringPiece) = 0;
- virtual void AppendAsTraceFormat(std::string* out) const = 0;
- virtual bool AppendToProto(ProtoAppender* appender);
- virtual void EstimateTraceMemoryOverhead(
- TraceEventMemoryOverhead* overhead) = 0;
- virtual bool IsPickleWriter() const = 0;
- virtual bool IsProtoWriter() const = 0;
- };
- typedef std::unique_ptr<Writer> (*WriterFactoryCallback)(size_t capacity);
- static void SetWriterFactoryCallback(WriterFactoryCallback callback);
- protected:
- TracedValue(size_t capacity, bool forced_json);
- std::unique_ptr<base::Value> ToBaseValue() const;
- private:
- std::unique_ptr<Writer> writer_;
- #ifndef NDEBUG
-
- std::vector<bool> nesting_stack_;
- #endif
- DISALLOW_COPY_AND_ASSIGN(TracedValue);
- };
- class BASE_EXPORT TracedValueJSON : public TracedValue {
- public:
- explicit TracedValueJSON(size_t capacity = 0)
- : TracedValue(capacity, true) {}
- using TracedValue::ToBaseValue;
-
-
- std::string ToJSON() const;
-
-
- std::string ToFormattedJSON() const;
- };
- }
- }
- #endif
|