Files
carbon-lang/explorer/base/print_as_id.h
T
Jon Ross-Perkins f63834c71d Move PrintAsID into explorer. (#3163)
#2569 added PrintAsID to //common/ostream.h, but given it's
explorer-specific behavior, I don't think it's the right home for it.

Noticed this while pondering better ostream interfaces.
2023-08-28 23:39:56 +00:00

37 lines
916 B
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#ifndef CARBON_EXPLORER_BASE_PRINT_AS_ID_H_
#define CARBON_EXPLORER_BASE_PRINT_AS_ID_H_
#include "common/ostream.h"
namespace Carbon {
// Helper to support printing the ID for a type that has a method
// `void PrintID(llvm::raw_ostream& out) const`. Usage:
//
// out << PrintAsID(obj);
template <typename T>
class PrintAsID {
public:
explicit PrintAsID(const T& object) : object_(&object) {}
friend auto operator<<(llvm::raw_ostream& out, const PrintAsID& self)
-> llvm::raw_ostream& {
self.object_->PrintID(out);
return out;
}
private:
const T* object_;
};
template <typename T>
PrintAsID(const T&) -> PrintAsID<T>;
} // namespace Carbon
#endif // CARBON_EXPLORER_BASE_PRINT_AS_ID_H_