printer.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. //
  34. // Utility class for writing text to a ZeroCopyOutputStream.
  35. #ifndef GOOGLE_PROTOBUF_IO_PRINTER_H__
  36. #define GOOGLE_PROTOBUF_IO_PRINTER_H__
  37. #include <string>
  38. #include <map>
  39. #include <google/protobuf/stubs/common.h>
  40. namespace google {
  41. namespace protobuf {
  42. namespace io {
  43. class ZeroCopyOutputStream; // zero_copy_stream.h
  44. // This simple utility class assists in code generation. It basically
  45. // allows the caller to define a set of variables and then output some
  46. // text with variable substitutions. Example usage:
  47. //
  48. // Printer printer(output, '$');
  49. // map<string, string> vars;
  50. // vars["name"] = "Bob";
  51. // printer.Print(vars, "My name is $name$.");
  52. //
  53. // The above writes "My name is Bob." to the output stream.
  54. //
  55. // Printer aggressively enforces correct usage, crashing (with assert failures)
  56. // in the case of undefined variables in debug builds. This helps greatly in
  57. // debugging code which uses it.
  58. class LIBPROTOBUF_EXPORT Printer {
  59. public:
  60. // Create a printer that writes text to the given output stream. Use the
  61. // given character as the delimiter for variables.
  62. Printer(ZeroCopyOutputStream* output, char variable_delimiter);
  63. ~Printer();
  64. // Print some text after applying variable substitutions. If a particular
  65. // variable in the text is not defined, this will crash. Variables to be
  66. // substituted are identified by their names surrounded by delimiter
  67. // characters (as given to the constructor). The variable bindings are
  68. // defined by the given map.
  69. void Print(const map<string, string>& variables, const char* text);
  70. // Like the first Print(), except the substitutions are given as parameters.
  71. void Print(const char* text);
  72. // Like the first Print(), except the substitutions are given as parameters.
  73. void Print(const char* text, const char* variable, const string& value);
  74. // Like the first Print(), except the substitutions are given as parameters.
  75. void Print(const char* text, const char* variable1, const string& value1,
  76. const char* variable2, const string& value2);
  77. // Like the first Print(), except the substitutions are given as parameters.
  78. void Print(const char* text, const char* variable1, const string& value1,
  79. const char* variable2, const string& value2,
  80. const char* variable3, const string& value3);
  81. // Like the first Print(), except the substitutions are given as parameters.
  82. void Print(const char* text, const char* variable1, const string& value1,
  83. const char* variable2, const string& value2,
  84. const char* variable3, const string& value3,
  85. const char* variable4, const string& value4);
  86. // Like the first Print(), except the substitutions are given as parameters.
  87. void Print(const char* text, const char* variable1, const string& value1,
  88. const char* variable2, const string& value2,
  89. const char* variable3, const string& value3,
  90. const char* variable4, const string& value4,
  91. const char* variable5, const string& value5);
  92. // Like the first Print(), except the substitutions are given as parameters.
  93. void Print(const char* text, const char* variable1, const string& value1,
  94. const char* variable2, const string& value2,
  95. const char* variable3, const string& value3,
  96. const char* variable4, const string& value4,
  97. const char* variable5, const string& value5,
  98. const char* variable6, const string& value6);
  99. // Like the first Print(), except the substitutions are given as parameters.
  100. void Print(const char* text, const char* variable1, const string& value1,
  101. const char* variable2, const string& value2,
  102. const char* variable3, const string& value3,
  103. const char* variable4, const string& value4,
  104. const char* variable5, const string& value5,
  105. const char* variable6, const string& value6,
  106. const char* variable7, const string& value7);
  107. // Like the first Print(), except the substitutions are given as parameters.
  108. void Print(const char* text, const char* variable1, const string& value1,
  109. const char* variable2, const string& value2,
  110. const char* variable3, const string& value3,
  111. const char* variable4, const string& value4,
  112. const char* variable5, const string& value5,
  113. const char* variable6, const string& value6,
  114. const char* variable7, const string& value7,
  115. const char* variable8, const string& value8);
  116. // Indent text by two spaces. After calling Indent(), two spaces will be
  117. // inserted at the beginning of each line of text. Indent() may be called
  118. // multiple times to produce deeper indents.
  119. void Indent();
  120. // Reduces the current indent level by two spaces, or crashes if the indent
  121. // level is zero.
  122. void Outdent();
  123. // Write a string to the output buffer.
  124. // This method does not look for newlines to add indentation.
  125. void PrintRaw(const string& data);
  126. // Write a zero-delimited string to output buffer.
  127. // This method does not look for newlines to add indentation.
  128. void PrintRaw(const char* data);
  129. // Write some bytes to the output buffer.
  130. // This method does not look for newlines to add indentation.
  131. void WriteRaw(const char* data, int size);
  132. // True if any write to the underlying stream failed. (We don't just
  133. // crash in this case because this is an I/O failure, not a programming
  134. // error.)
  135. bool failed() const { return failed_; }
  136. private:
  137. const char variable_delimiter_;
  138. ZeroCopyOutputStream* const output_;
  139. char* buffer_;
  140. int buffer_size_;
  141. string indent_;
  142. bool at_start_of_line_;
  143. bool failed_;
  144. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Printer);
  145. };
  146. } // namespace io
  147. } // namespace protobuf
  148. } // namespace google
  149. #endif // GOOGLE_PROTOBUF_IO_PRINTER_H__