1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2024 Google Inc. All rights reserved. 3 // 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file or at 6 // https://developers.google.com/open-source/licenses/bsd 7 8 // An RAII type for printing an ifdef guard. 9 // 10 // This can be used to ensure that appropriate ifdef guards are applied in 11 // a generated header file. 12 // 13 // Example: 14 // { 15 // Printer printer(output_stream.get(), '$'); 16 // const IfdefGuardPrinter ifdef_guard(&printer, output_path); 17 // // #ifdef guard will be emitted here 18 // ... 19 // // #endif will be emitted here 20 // } 21 // 22 // By default, the filename will be converted to a macro by substituting '/' and 23 // '.' characters with '_'. If a different transformation is required, an 24 // optional transformation function can be provided. 25 26 #ifndef GOOGLE_PROTOBUF_COMPILER_CPP_IFNDEF_GUARD_H__ 27 #define GOOGLE_PROTOBUF_COMPILER_CPP_IFNDEF_GUARD_H__ 28 29 #include <string> 30 31 #include "absl/functional/any_invocable.h" 32 #include "absl/strings/string_view.h" 33 #include "google/protobuf/io/printer.h" 34 35 // Must be included last. 36 #include "google/protobuf/port_def.inc" 37 38 namespace google { 39 namespace protobuf { 40 namespace compiler { 41 namespace cpp { 42 43 class PROTOC_EXPORT IfdefGuardPrinter final { 44 public: 45 explicit IfdefGuardPrinter(google::protobuf::io::Printer* p, 46 absl::string_view filename); 47 48 explicit IfdefGuardPrinter( 49 google::protobuf::io::Printer* p, absl::string_view filename, 50 absl::AnyInvocable<std::string(absl::string_view)> make_ifdef_identifier); 51 52 ~IfdefGuardPrinter(); 53 54 private: 55 google::protobuf::io::Printer* const p_; 56 const std::string ifdef_identifier_; 57 }; 58 59 #include "google/protobuf/port_undef.inc" 60 61 } // namespace cpp 62 } // namespace compiler 63 } // namespace protobuf 64 } // namespace google 65 66 #endif // GOOGLE_PROTOBUF_COMPILER_CPP_IFNDEF_GUARD_H__ 67