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 std::vector<int>& path) = 0; | 58 const 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, | 76 const string& file_path, const vector<int>& path) { |
77 const std::vector<int>& path) { | |
78 typename AnnotationProto::Annotation* annotation = | 77 typename AnnotationProto::Annotation* annotation = |
79 annotation_proto_->add_annotation(); | 78 annotation_proto_->add_annotation(); |
80 for (int i = 0; i < path.size(); ++i) { | 79 for (int i = 0; i < path.size(); ++i) { |
81 annotation->add_path(path[i]); | 80 annotation->add_path(path[i]); |
82 } | 81 } |
83 annotation->set_source_file(file_path); | 82 annotation->set_source_file(file_path); |
84 annotation->set_begin(begin_offset); | 83 annotation->set_begin(begin_offset); |
85 annotation->set_end(end_offset); | 84 annotation->set_end(end_offset); |
86 } | 85 } |
87 | 86 |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 // begins at begin_varname's value and ends after the last character of the | 188 // begins at begin_varname's value and ends after the last character of the |
190 // value substituted for end_varname. | 189 // value substituted for end_varname. |
191 template <typename SomeDescriptor> | 190 template <typename SomeDescriptor> |
192 void Annotate(const char* begin_varname, const char* end_varname, | 191 void Annotate(const char* begin_varname, const char* end_varname, |
193 const SomeDescriptor* descriptor) { | 192 const SomeDescriptor* descriptor) { |
194 if (annotation_collector_ == NULL) { | 193 if (annotation_collector_ == NULL) { |
195 // Annotations aren't turned on for this Printer, so don't pay the cost | 194 // Annotations aren't turned on for this Printer, so don't pay the cost |
196 // of building the location path. | 195 // of building the location path. |
197 return; | 196 return; |
198 } | 197 } |
199 std::vector<int> path; | 198 vector<int> path; |
200 descriptor->GetLocationPath(&path); | 199 descriptor->GetLocationPath(&path); |
201 Annotate(begin_varname, end_varname, descriptor->file()->name(), path); | 200 Annotate(begin_varname, end_varname, descriptor->file()->name(), path); |
202 } | 201 } |
203 | 202 |
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 | |
224 // Print some text after applying variable substitutions. If a particular | 203 // Print some text after applying variable substitutions. If a particular |
225 // variable in the text is not defined, this will crash. Variables to be | 204 // variable in the text is not defined, this will crash. Variables to be |
226 // substituted are identified by their names surrounded by delimiter | 205 // substituted are identified by their names surrounded by delimiter |
227 // characters (as given to the constructor). The variable bindings are | 206 // characters (as given to the constructor). The variable bindings are |
228 // defined by the given map. | 207 // defined by the given map. |
229 void Print(const std::map<string, string>& variables, const char* text); | 208 void Print(const map<string, string>& variables, const char* text); |
230 | 209 |
231 // Like the first Print(), except the substitutions are given as parameters. | 210 // Like the first Print(), except the substitutions are given as parameters. |
232 void Print(const char* text); | 211 void Print(const char* text); |
233 // Like the first Print(), except the substitutions are given as parameters. | 212 // Like the first Print(), except the substitutions are given as parameters. |
234 void Print(const char* text, const char* variable, const string& value); | 213 void Print(const char* text, const char* variable, const string& value); |
235 // Like the first Print(), except the substitutions are given as parameters. | 214 // Like the first Print(), except the substitutions are given as parameters. |
236 void Print(const char* text, const char* variable1, const string& value1, | 215 void Print(const char* text, const char* variable1, const string& value1, |
237 const char* variable2, const string& value2); | 216 const char* variable2, const string& value2); |
238 // Like the first Print(), except the substitutions are given as parameters. | 217 // Like the first Print(), except the substitutions are given as parameters. |
239 void Print(const char* text, const char* variable1, const string& value1, | 218 void Print(const char* text, const char* variable1, const string& value1, |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
302 bool failed() const { return failed_; } | 281 bool failed() const { return failed_; } |
303 | 282 |
304 private: | 283 private: |
305 // Link the output range defined by the substitution variables as emitted by | 284 // Link the output range defined by the substitution variables as emitted by |
306 // the last call to Print to the object found at the SourceCodeInfo-style path | 285 // the last call to Print to the object found at the SourceCodeInfo-style path |
307 // in a file with path file_path. The range begins at the start of | 286 // in a file with path file_path. The range begins at the start of |
308 // begin_varname's value and ends after the last character of the value | 287 // begin_varname's value and ends after the last character of the value |
309 // substituted for end_varname. Note that begin_varname and end_varname | 288 // substituted for end_varname. Note that begin_varname and end_varname |
310 // may refer to the same variable. | 289 // may refer to the same variable. |
311 void Annotate(const char* begin_varname, const char* end_varname, | 290 void Annotate(const char* begin_varname, const char* end_varname, |
312 const string& file_path, const std::vector<int>& path); | 291 const string& file_path, const vector<int>& path); |
313 | 292 |
314 const char variable_delimiter_; | 293 const char variable_delimiter_; |
315 | 294 |
316 ZeroCopyOutputStream* const output_; | 295 ZeroCopyOutputStream* const output_; |
317 char* buffer_; | 296 char* buffer_; |
318 int buffer_size_; | 297 int buffer_size_; |
319 // The current position, in bytes, in the output stream. This is equivalent | 298 // The current position, in bytes, in the output stream. This is equivalent |
320 // to the total number of bytes that have been written so far. This value is | 299 // to the total number of bytes that have been written so far. This value is |
321 // used to calculate annotation ranges in the substitutions_ map below. | 300 // used to calculate annotation ranges in the substitutions_ map below. |
322 size_t offset_; | 301 size_t offset_; |
323 | 302 |
324 string indent_; | 303 string indent_; |
325 bool at_start_of_line_; | 304 bool at_start_of_line_; |
326 bool failed_; | 305 bool failed_; |
327 | 306 |
328 // A map from variable name to [start, end) offsets in the output buffer. | 307 // A map from variable name to [start, end) offsets in the output buffer. |
329 // These refer to the offsets used for a variable after the last call to | 308 // These refer to the offsets used for a variable after the last call to |
330 // Print. If a variable was used more than once, the entry used in | 309 // Print. If a variable was used more than once, the entry used in |
331 // this map is set to a negative-length span. For singly-used variables, the | 310 // this map is set to a negative-length span. For singly-used variables, the |
332 // start offset is the beginning of the substitution; the end offset is the | 311 // start offset is the beginning of the substitution; the end offset is the |
333 // last byte of the substitution plus one (such that (end - start) is the | 312 // last byte of the substitution plus one (such that (end - start) is the |
334 // length of the substituted string). | 313 // length of the substituted string). |
335 std::map<string, std::pair<size_t, size_t> > substitutions_; | 314 map<string, pair<size_t, size_t> > substitutions_; |
336 | 315 |
337 // Returns true and sets range to the substitution range in the output for | 316 // Returns true and sets range to the substitution range in the output for |
338 // varname if varname was used once in the last call to Print. If varname | 317 // varname if varname was used once in the last call to Print. If varname |
339 // was not used, or if it was used multiple times, returns false (and | 318 // was not used, or if it was used multiple times, returns false (and |
340 // fails a debug assertion). | 319 // fails a debug assertion). |
341 bool GetSubstitutionRange(const char* varname, | 320 bool GetSubstitutionRange(const char* varname, pair<size_t, size_t>* range); |
342 std::pair<size_t, size_t>* range); | |
343 | 321 |
344 // If non-null, annotation_collector_ is used to store annotations about | 322 // If non-null, annotation_collector_ is used to store annotations about |
345 // generated code. | 323 // generated code. |
346 AnnotationCollector* const annotation_collector_; | 324 AnnotationCollector* const annotation_collector_; |
347 | 325 |
348 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Printer); | 326 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Printer); |
349 }; | 327 }; |
350 | 328 |
351 } // namespace io | 329 } // namespace io |
352 } // namespace protobuf | 330 } // namespace protobuf |
353 | 331 |
354 } // namespace google | 332 } // namespace google |
355 #endif // GOOGLE_PROTOBUF_IO_PRINTER_H__ | 333 #endif // GOOGLE_PROTOBUF_IO_PRINTER_H__ |
OLD | NEW |