structured_objectwriter.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #ifndef GOOGLE_PROTOBUF_UTIL_CONVERTER_STRUCTURED_OBJECTWRITER_H__
  31. #define GOOGLE_PROTOBUF_UTIL_CONVERTER_STRUCTURED_OBJECTWRITER_H__
  32. #include <memory>
  33. #include <google/protobuf/stubs/casts.h>
  34. #include <google/protobuf/stubs/common.h>
  35. #include <google/protobuf/util/internal/object_writer.h>
  36. namespace google {
  37. namespace protobuf {
  38. namespace util {
  39. namespace converter {
  40. // An StructuredObjectWriter is an ObjectWriter for writing
  41. // tree-structured data in a stream of events representing objects
  42. // and collections. Implementation of this interface can be used to
  43. // write an object stream to an in-memory structure, protobufs,
  44. // JSON, XML, or any other output format desired. The ObjectSource
  45. // interface is typically used as the source of an object stream.
  46. //
  47. // See JsonObjectWriter for a sample implementation of
  48. // StructuredObjectWriter and its use.
  49. //
  50. // Derived classes could be thread-unsafe.
  51. class LIBPROTOBUF_EXPORT StructuredObjectWriter : public ObjectWriter {
  52. public:
  53. virtual ~StructuredObjectWriter() {}
  54. protected:
  55. // A base element class for subclasses to extend, makes tracking state easier.
  56. //
  57. // StructuredObjectWriter behaves as a visitor. BaseElement represents a node
  58. // in the input tree. Implementation of StructuredObjectWriter should also
  59. // extend BaseElement to keep track of the location in the input tree.
  60. class LIBPROTOBUF_EXPORT BaseElement {
  61. public:
  62. // Takes ownership of the parent Element.
  63. explicit BaseElement(BaseElement* parent)
  64. : parent_(parent), level_(parent == NULL ? 0 : parent->level() + 1) {}
  65. virtual ~BaseElement() {}
  66. // Releases ownership of the parent and returns a pointer to it.
  67. template <typename ElementType>
  68. ElementType* pop() {
  69. return down_cast<ElementType*>(parent_.release());
  70. }
  71. // Returns true if this element is the root.
  72. bool is_root() const { return parent_ == nullptr; }
  73. // Returns the number of hops from this element to the root element.
  74. int level() const { return level_; }
  75. protected:
  76. // Returns pointer to parent element without releasing ownership.
  77. virtual BaseElement* parent() const { return parent_.get(); }
  78. private:
  79. // Pointer to the parent Element.
  80. std::unique_ptr<BaseElement> parent_;
  81. // Number of hops to the root Element.
  82. // The root Element has nullptr parent_ and a level_ of 0.
  83. const int level_;
  84. GOOGLE_DISALLOW_IMPLICIT_CONSTRUCTORS(BaseElement);
  85. };
  86. StructuredObjectWriter() {}
  87. // Returns the current element. Used for indentation and name overrides.
  88. virtual BaseElement* element() = 0;
  89. private:
  90. // Do not add any data members to this class.
  91. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(StructuredObjectWriter);
  92. };
  93. } // namespace converter
  94. } // namespace util
  95. } // namespace protobuf
  96. } // namespace google
  97. #endif // GOOGLE_PROTOBUF_UTIL_CONVERTER_STRUCTURED_OBJECTWRITER_H__