1 #include "google/protobuf/compiler/cpp/namespace_printer.h" 2 3 #include <string> 4 #include <utility> 5 #include <vector> 6 7 #include "absl/log/die_if_null.h" 8 #include "absl/strings/substitute.h" 9 #include "google/protobuf/io/printer.h" 10 11 namespace google { 12 namespace protobuf { 13 namespace compiler { 14 namespace cpp { 15 NamespacePrinter(google::protobuf::io::Printer * const p,std::vector<std::string> namespace_components)16NamespacePrinter::NamespacePrinter( 17 google::protobuf::io::Printer* const p, std::vector<std::string> namespace_components) 18 : p_(ABSL_DIE_IF_NULL(p)), 19 namespace_components_(std::move(namespace_components)) { 20 // Open the namespace. 21 for (const std::string& ns : namespace_components_) { 22 p_->Print(absl::Substitute("namespace $0 {\n", ns)); 23 } 24 p_->Print("\n"); 25 } 26 ~NamespacePrinter()27NamespacePrinter::~NamespacePrinter() { 28 // Close the namespace. 29 for (auto it = namespace_components_.rbegin(); 30 it != namespace_components_.rend(); ++it) { 31 p_->Print(absl::Substitute("} // namespace $0\n", *it)); 32 } 33 } 34 35 } // namespace cpp 36 } // namespace compiler 37 } // namespace protobuf 38 } // namespace google 39