| OLD | NEW |
| 1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
| 2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
| 3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
| 4 // | 4 // |
| 5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
| 6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
| 7 // met: | 7 // met: |
| 8 // | 8 // |
| 9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
| 10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 | 48 |
| 49 class ZeroCopyOutputStream; // zero_copy_stream.h | 49 class ZeroCopyOutputStream; // zero_copy_stream.h |
| 50 | 50 |
| 51 // Records annotations about a Printer's output. | 51 // Records annotations about a Printer's output. |
| 52 class LIBPROTOBUF_EXPORT AnnotationCollector { | 52 class LIBPROTOBUF_EXPORT AnnotationCollector { |
| 53 public: | 53 public: |
| 54 // Records that the bytes in file_path beginning with begin_offset and ending | 54 // Records that the bytes in file_path beginning with begin_offset and ending |
| 55 // before end_offset are associated with the SourceCodeInfo-style path. | 55 // before end_offset are associated with the SourceCodeInfo-style path. |
| 56 virtual void AddAnnotation(size_t begin_offset, size_t end_offset, | 56 virtual void AddAnnotation(size_t begin_offset, size_t end_offset, |
| 57 const string& file_path, | 57 const string& file_path, |
| 58 const vector<int>& path) = 0; | 58 const std::vector<int>& path) = 0; |
| 59 | 59 |
| 60 virtual ~AnnotationCollector() {} | 60 virtual ~AnnotationCollector() {} |
| 61 }; | 61 }; |
| 62 | 62 |
| 63 // Records annotations about a Printer's output to the given protocol buffer, | 63 // Records annotations about a Printer's output to the given protocol buffer, |
| 64 // assuming that the buffer has an ::Annotation message exposing path, | 64 // assuming that the buffer has an ::Annotation message exposing path, |
| 65 // source_file, begin and end fields. | 65 // source_file, begin and end fields. |
| 66 template <typename AnnotationProto> | 66 template <typename AnnotationProto> |
| 67 class AnnotationProtoCollector : public AnnotationCollector { | 67 class AnnotationProtoCollector : public AnnotationCollector { |
| 68 public: | 68 public: |
| 69 // annotation_proto is the protocol buffer to which new Annotations should be | 69 // annotation_proto is the protocol buffer to which new Annotations should be |
| 70 // added. It is not owned by the AnnotationProtoCollector. | 70 // added. It is not owned by the AnnotationProtoCollector. |
| 71 explicit AnnotationProtoCollector(AnnotationProto* annotation_proto) | 71 explicit AnnotationProtoCollector(AnnotationProto* annotation_proto) |
| 72 : annotation_proto_(annotation_proto) {} | 72 : annotation_proto_(annotation_proto) {} |
| 73 | 73 |
| 74 // Override for AnnotationCollector::AddAnnotation. | 74 // Override for AnnotationCollector::AddAnnotation. |
| 75 virtual void AddAnnotation(size_t begin_offset, size_t end_offset, | 75 virtual void AddAnnotation(size_t begin_offset, size_t end_offset, |
| 76 const string& file_path, const vector<int>& path) { | 76 const string& file_path, |
| 77 const std::vector<int>& path) { |
| 77 typename AnnotationProto::Annotation* annotation = | 78 typename AnnotationProto::Annotation* annotation = |
| 78 annotation_proto_->add_annotation(); | 79 annotation_proto_->add_annotation(); |
| 79 for (int i = 0; i < path.size(); ++i) { | 80 for (int i = 0; i < path.size(); ++i) { |
| 80 annotation->add_path(path[i]); | 81 annotation->add_path(path[i]); |
| 81 } | 82 } |
| 82 annotation->set_source_file(file_path); | 83 annotation->set_source_file(file_path); |
| 83 annotation->set_begin(begin_offset); | 84 annotation->set_begin(begin_offset); |
| 84 annotation->set_end(end_offset); | 85 annotation->set_end(end_offset); |
| 85 } | 86 } |
| 86 | 87 |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 // begins at begin_varname's value and ends after the last character of the | 189 // begins at begin_varname's value and ends after the last character of the |
| 189 // value substituted for end_varname. | 190 // value substituted for end_varname. |
| 190 template <typename SomeDescriptor> | 191 template <typename SomeDescriptor> |
| 191 void Annotate(const char* begin_varname, const char* end_varname, | 192 void Annotate(const char* begin_varname, const char* end_varname, |
| 192 const SomeDescriptor* descriptor) { | 193 const SomeDescriptor* descriptor) { |
| 193 if (annotation_collector_ == NULL) { | 194 if (annotation_collector_ == NULL) { |
| 194 // Annotations aren't turned on for this Printer, so don't pay the cost | 195 // Annotations aren't turned on for this Printer, so don't pay the cost |
| 195 // of building the location path. | 196 // of building the location path. |
| 196 return; | 197 return; |
| 197 } | 198 } |
| 198 vector<int> path; | 199 std::vector<int> path; |
| 199 descriptor->GetLocationPath(&path); | 200 descriptor->GetLocationPath(&path); |
| 200 Annotate(begin_varname, end_varname, descriptor->file()->name(), path); | 201 Annotate(begin_varname, end_varname, descriptor->file()->name(), path); |
| 201 } | 202 } |
| 202 | 203 |
| 204 // Link a subsitution variable emitted by the last call to Print to the file |
| 205 // with path file_name. |
| 206 void Annotate(const char* varname, const string& file_name) { |
| 207 Annotate(varname, varname, file_name); |
| 208 } |
| 209 |
| 210 // Link the output range defined by the substitution variables as emitted by |
| 211 // the last call to Print to the file with path file_name. The range begins |
| 212 // at begin_varname's value and ends after the last character of the value |
| 213 // substituted for end_varname. |
| 214 void Annotate(const char* begin_varname, const char* end_varname, |
| 215 const string& file_name) { |
| 216 if (annotation_collector_ == NULL) { |
| 217 // Annotations aren't turned on for this Printer. |
| 218 return; |
| 219 } |
| 220 std::vector<int> empty_path; |
| 221 Annotate(begin_varname, end_varname, file_name, empty_path); |
| 222 } |
| 223 |
| 203 // Print some text after applying variable substitutions. If a particular | 224 // Print some text after applying variable substitutions. If a particular |
| 204 // variable in the text is not defined, this will crash. Variables to be | 225 // variable in the text is not defined, this will crash. Variables to be |
| 205 // substituted are identified by their names surrounded by delimiter | 226 // substituted are identified by their names surrounded by delimiter |
| 206 // characters (as given to the constructor). The variable bindings are | 227 // characters (as given to the constructor). The variable bindings are |
| 207 // defined by the given map. | 228 // defined by the given map. |
| 208 void Print(const map<string, string>& variables, const char* text); | 229 void Print(const std::map<string, string>& variables, const char* text); |
| 209 | 230 |
| 210 // Like the first Print(), except the substitutions are given as parameters. | 231 // Like the first Print(), except the substitutions are given as parameters. |
| 211 void Print(const char* text); | 232 void Print(const char* text); |
| 212 // Like the first Print(), except the substitutions are given as parameters. | 233 // Like the first Print(), except the substitutions are given as parameters. |
| 213 void Print(const char* text, const char* variable, const string& value); | 234 void Print(const char* text, const char* variable, const string& value); |
| 214 // Like the first Print(), except the substitutions are given as parameters. | 235 // Like the first Print(), except the substitutions are given as parameters. |
| 215 void Print(const char* text, const char* variable1, const string& value1, | 236 void Print(const char* text, const char* variable1, const string& value1, |
| 216 const char* variable2, const string& value2); | 237 const char* variable2, const string& value2); |
| 217 // Like the first Print(), except the substitutions are given as parameters. | 238 // Like the first Print(), except the substitutions are given as parameters. |
| 218 void Print(const char* text, const char* variable1, const string& value1, | 239 void Print(const char* text, const char* variable1, const string& value1, |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 bool failed() const { return failed_; } | 302 bool failed() const { return failed_; } |
| 282 | 303 |
| 283 private: | 304 private: |
| 284 // Link the output range defined by the substitution variables as emitted by | 305 // Link the output range defined by the substitution variables as emitted by |
| 285 // the last call to Print to the object found at the SourceCodeInfo-style path | 306 // the last call to Print to the object found at the SourceCodeInfo-style path |
| 286 // in a file with path file_path. The range begins at the start of | 307 // in a file with path file_path. The range begins at the start of |
| 287 // begin_varname's value and ends after the last character of the value | 308 // begin_varname's value and ends after the last character of the value |
| 288 // substituted for end_varname. Note that begin_varname and end_varname | 309 // substituted for end_varname. Note that begin_varname and end_varname |
| 289 // may refer to the same variable. | 310 // may refer to the same variable. |
| 290 void Annotate(const char* begin_varname, const char* end_varname, | 311 void Annotate(const char* begin_varname, const char* end_varname, |
| 291 const string& file_path, const vector<int>& path); | 312 const string& file_path, const std::vector<int>& path); |
| 292 | 313 |
| 293 const char variable_delimiter_; | 314 const char variable_delimiter_; |
| 294 | 315 |
| 295 ZeroCopyOutputStream* const output_; | 316 ZeroCopyOutputStream* const output_; |
| 296 char* buffer_; | 317 char* buffer_; |
| 297 int buffer_size_; | 318 int buffer_size_; |
| 298 // The current position, in bytes, in the output stream. This is equivalent | 319 // The current position, in bytes, in the output stream. This is equivalent |
| 299 // to the total number of bytes that have been written so far. This value is | 320 // to the total number of bytes that have been written so far. This value is |
| 300 // used to calculate annotation ranges in the substitutions_ map below. | 321 // used to calculate annotation ranges in the substitutions_ map below. |
| 301 size_t offset_; | 322 size_t offset_; |
| 302 | 323 |
| 303 string indent_; | 324 string indent_; |
| 304 bool at_start_of_line_; | 325 bool at_start_of_line_; |
| 305 bool failed_; | 326 bool failed_; |
| 306 | 327 |
| 307 // A map from variable name to [start, end) offsets in the output buffer. | 328 // A map from variable name to [start, end) offsets in the output buffer. |
| 308 // These refer to the offsets used for a variable after the last call to | 329 // These refer to the offsets used for a variable after the last call to |
| 309 // Print. If a variable was used more than once, the entry used in | 330 // Print. If a variable was used more than once, the entry used in |
| 310 // this map is set to a negative-length span. For singly-used variables, the | 331 // this map is set to a negative-length span. For singly-used variables, the |
| 311 // start offset is the beginning of the substitution; the end offset is the | 332 // start offset is the beginning of the substitution; the end offset is the |
| 312 // last byte of the substitution plus one (such that (end - start) is the | 333 // last byte of the substitution plus one (such that (end - start) is the |
| 313 // length of the substituted string). | 334 // length of the substituted string). |
| 314 map<string, pair<size_t, size_t> > substitutions_; | 335 std::map<string, std::pair<size_t, size_t> > substitutions_; |
| 315 | 336 |
| 316 // Returns true and sets range to the substitution range in the output for | 337 // Returns true and sets range to the substitution range in the output for |
| 317 // varname if varname was used once in the last call to Print. If varname | 338 // varname if varname was used once in the last call to Print. If varname |
| 318 // was not used, or if it was used multiple times, returns false (and | 339 // was not used, or if it was used multiple times, returns false (and |
| 319 // fails a debug assertion). | 340 // fails a debug assertion). |
| 320 bool GetSubstitutionRange(const char* varname, pair<size_t, size_t>* range); | 341 bool GetSubstitutionRange(const char* varname, |
| 342 std::pair<size_t, size_t>* range); |
| 321 | 343 |
| 322 // If non-null, annotation_collector_ is used to store annotations about | 344 // If non-null, annotation_collector_ is used to store annotations about |
| 323 // generated code. | 345 // generated code. |
| 324 AnnotationCollector* const annotation_collector_; | 346 AnnotationCollector* const annotation_collector_; |
| 325 | 347 |
| 326 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Printer); | 348 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Printer); |
| 327 }; | 349 }; |
| 328 | 350 |
| 329 } // namespace io | 351 } // namespace io |
| 330 } // namespace protobuf | 352 } // namespace protobuf |
| 331 | 353 |
| 332 } // namespace google | 354 } // namespace google |
| 333 #endif // GOOGLE_PROTOBUF_IO_PRINTER_H__ | 355 #endif // GOOGLE_PROTOBUF_IO_PRINTER_H__ |
| OLD | NEW |