blob: 16f135deaf5cd9b74e91061e899f9dc6fec18cb2 [file] [log] [blame]
William A. Kennington IIIfc955ab2020-08-07 14:42:54 -07001#include <stdplus/debug/lifetime.hpp>
2#include <stdplus/print.hpp>
3
4using std::literals::string_view_literals::operator""sv;
5
6template <typename CharT>
7struct std::formatter<std::source_location, CharT>
8{
9 template <typename ParseContext>
10 constexpr auto parse(ParseContext& ctx)
11 {
12 return ctx.begin();
13 }
14
15 template <typename FormatContext>
16 constexpr auto format(const auto& v, FormatContext& ctx) const
17 {
18 return std::format_to(ctx.out(), "{}:{}({})", v.file_name(), v.line(),
19 v.function_name());
20 }
21};
22
23namespace stdplus::debug
24{
25
26static std::size_t next_id = 0;
27
28Lifetime::Lifetime(std::source_location loc) : loc(loc), id(next_id++)
29{
30 stdplus::print(stderr, "Lifetime Construct {} {}\n", loc, id);
31}
32
33Lifetime::Lifetime(const Lifetime& other) : loc(other.loc), id(next_id++)
34{
35 stdplus::print(stderr, "Lifetime Copy {} {}->{}\n", loc, other.id, id);
36}
37
38Lifetime::Lifetime(Lifetime&& other) : loc(other.loc), id(next_id++)
39{
40 stdplus::print(stderr, "Lifetime Move {} {}->{}\n", loc, other.id, id);
41}
42
43Lifetime& Lifetime::operator=(const Lifetime& other)
44{
45 auto old_id = id;
46 id = next_id++;
47 stdplus::print(stderr, "Lifetime Copy {} {}->{} drop {}\n", loc, other.id,
48 id, old_id);
49 return *this;
50}
51
52Lifetime& Lifetime::operator=(Lifetime&& other)
53{
54 auto old_id = id;
55 id = next_id++;
56 stdplus::print(stderr, "Lifetime Move {} {}->{} drop {}\n", loc, other.id,
57 id, old_id);
58 return *this;
59}
60
61Lifetime::~Lifetime()
62{
63 stdplus::print(stderr, "Lifetime Destroy {} {}\n", loc, id);
64}
65
66} // namespace stdplus::debug