BWAPI
|
00001 // Protocol Buffers - Google's data interchange format 00002 // Copyright 2008 Google Inc. All rights reserved. 00003 // http://code.google.com/p/protobuf/ 00004 // 00005 // Redistribution and use in source and binary forms, with or without 00006 // modification, are permitted provided that the following conditions are 00007 // met: 00008 // 00009 // * Redistributions of source code must retain the above copyright 00010 // notice, this list of conditions and the following disclaimer. 00011 // * Redistributions in binary form must reproduce the above 00012 // copyright notice, this list of conditions and the following disclaimer 00013 // in the documentation and/or other materials provided with the 00014 // distribution. 00015 // * Neither the name of Google Inc. nor the names of its 00016 // contributors may be used to endorse or promote products derived from 00017 // this software without specific prior written permission. 00018 // 00019 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00020 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00021 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00022 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00023 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00024 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00025 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00026 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00027 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00028 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00029 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00030 00031 // Author: kenton@google.com (Kenton Varda) 00032 // Based on original Protocol Buffers design by 00033 // Sanjay Ghemawat, Jeff Dean, and others. 00034 // 00035 // Utility class for writing text to a ZeroCopyOutputStream. 00036 00037 #ifndef GOOGLE_PROTOBUF_IO_PRINTER_H__ 00038 #define GOOGLE_PROTOBUF_IO_PRINTER_H__ 00039 00040 #include <string> 00041 #include <map> 00042 #include <google/protobuf/stubs/common.h> 00043 00044 namespace google { 00045 namespace protobuf { 00046 namespace io { 00047 00048 class ZeroCopyOutputStream; // zero_copy_stream.h 00049 00050 // This simple utility class assists in code generation. It basically 00051 // allows the caller to define a set of variables and then output some 00052 // text with variable substitutions. Example usage: 00053 // 00054 // Printer printer(output, '$'); 00055 // map<string, string> vars; 00056 // vars["name"] = "Bob"; 00057 // printer.Print(vars, "My name is $name$."); 00058 // 00059 // The above writes "My name is Bob." to the output stream. 00060 // 00061 // Printer aggressively enforces correct usage, crashing (with assert failures) 00062 // in the case of undefined variables in debug builds. This helps greatly in 00063 // debugging code which uses it. 00064 class LIBPROTOBUF_EXPORT Printer { 00065 public: 00066 // Create a printer that writes text to the given output stream. Use the 00067 // given character as the delimiter for variables. 00068 Printer(ZeroCopyOutputStream* output, char variable_delimiter); 00069 ~Printer(); 00070 00071 // Print some text after applying variable substitutions. If a particular 00072 // variable in the text is not defined, this will crash. Variables to be 00073 // substituted are identified by their names surrounded by delimiter 00074 // characters (as given to the constructor). The variable bindings are 00075 // defined by the given map. 00076 void Print(const map<string, string>& variables, const char* text); 00077 00078 // Like the first Print(), except the substitutions are given as parameters. 00079 void Print(const char* text); 00080 // Like the first Print(), except the substitutions are given as parameters. 00081 void Print(const char* text, const char* variable, const string& value); 00082 // Like the first Print(), except the substitutions are given as parameters. 00083 void Print(const char* text, const char* variable1, const string& value1, 00084 const char* variable2, const string& value2); 00085 // TODO(kenton): Overloaded versions with more variables? Two seems 00086 // to be enough. 00087 00088 // Indent text by two spaces. After calling Indent(), two spaces will be 00089 // inserted at the beginning of each line of text. Indent() may be called 00090 // multiple times to produce deeper indents. 00091 void Indent(); 00092 00093 // Reduces the current indent level by two spaces, or crashes if the indent 00094 // level is zero. 00095 void Outdent(); 00096 00097 // Write a string to the output buffer. 00098 // This method does not look for newlines to add indentation. 00099 void PrintRaw(const string& data); 00100 00101 // Write a zero-delimited string to output buffer. 00102 // This method does not look for newlines to add indentation. 00103 void PrintRaw(const char* data); 00104 00105 // Write some bytes to the output buffer. 00106 // This method does not look for newlines to add indentation. 00107 void WriteRaw(const char* data, int size); 00108 00109 // True if any write to the underlying stream failed. (We don't just 00110 // crash in this case because this is an I/O failure, not a programming 00111 // error.) 00112 bool failed() const { return failed_; } 00113 00114 private: 00115 const char variable_delimiter_; 00116 00117 ZeroCopyOutputStream* const output_; 00118 char* buffer_; 00119 int buffer_size_; 00120 00121 string indent_; 00122 bool at_start_of_line_; 00123 bool failed_; 00124 00125 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Printer); 00126 }; 00127 00128 } // namespace io 00129 } // namespace protobuf 00130 00131 } // namespace google 00132 #endif // GOOGLE_PROTOBUF_IO_PRINTER_H__