print_as_id.h 916 B

123456789101112131415161718192021222324252627282930313233343536
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #ifndef CARBON_EXPLORER_BASE_PRINT_AS_ID_H_
  5. #define CARBON_EXPLORER_BASE_PRINT_AS_ID_H_
  6. #include "common/ostream.h"
  7. namespace Carbon {
  8. // Helper to support printing the ID for a type that has a method
  9. // `void PrintID(llvm::raw_ostream& out) const`. Usage:
  10. //
  11. // out << PrintAsID(obj);
  12. template <typename T>
  13. class PrintAsID {
  14. public:
  15. explicit PrintAsID(const T& object) : object_(&object) {}
  16. friend auto operator<<(llvm::raw_ostream& out, const PrintAsID& self)
  17. -> llvm::raw_ostream& {
  18. self.object_->PrintID(out);
  19. return out;
  20. }
  21. private:
  22. const T* object_;
  23. };
  24. template <typename T>
  25. PrintAsID(const T&) -> PrintAsID<T>;
  26. } // namespace Carbon
  27. #endif // CARBON_EXPLORER_BASE_PRINT_AS_ID_H_