| William A. Kennington III | fc955ab | 2020-08-07 14:42:54 -0700 | [diff] [blame] | 1 | #include <stdplus/debug/lifetime.hpp> | 
|  | 2 | #include <stdplus/print.hpp> | 
|  | 3 |  | 
|  | 4 | using std::literals::string_view_literals::operator""sv; | 
|  | 5 |  | 
|  | 6 | template <typename CharT> | 
|  | 7 | struct 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 |  | 
|  | 23 | namespace stdplus::debug | 
|  | 24 | { | 
|  | 25 |  | 
|  | 26 | static std::size_t next_id = 0; | 
|  | 27 |  | 
|  | 28 | Lifetime::Lifetime(std::source_location loc) : loc(loc), id(next_id++) | 
|  | 29 | { | 
|  | 30 | stdplus::print(stderr, "Lifetime Construct {} {}\n", loc, id); | 
|  | 31 | } | 
|  | 32 |  | 
|  | 33 | Lifetime::Lifetime(const Lifetime& other) : loc(other.loc), id(next_id++) | 
|  | 34 | { | 
|  | 35 | stdplus::print(stderr, "Lifetime Copy {} {}->{}\n", loc, other.id, id); | 
|  | 36 | } | 
|  | 37 |  | 
|  | 38 | Lifetime::Lifetime(Lifetime&& other) : loc(other.loc), id(next_id++) | 
|  | 39 | { | 
|  | 40 | stdplus::print(stderr, "Lifetime Move {} {}->{}\n", loc, other.id, id); | 
|  | 41 | } | 
|  | 42 |  | 
|  | 43 | Lifetime& 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 |  | 
|  | 52 | Lifetime& 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 |  | 
|  | 61 | Lifetime::~Lifetime() | 
|  | 62 | { | 
|  | 63 | stdplus::print(stderr, "Lifetime Destroy {} {}\n", loc, id); | 
|  | 64 | } | 
|  | 65 |  | 
|  | 66 | } // namespace stdplus::debug |