libpqxx 7.8.1
transaction_focus.hxx
1
9#ifndef PQXX_H_TRANSACTION_FOCUS
10#define PQXX_H_TRANSACTION_FOCUS
11
12#if !defined(PQXX_HEADER_PRE)
13# error "Include libpqxx headers as <pqxx/header>, not <pqxx/header.hxx>."
14#endif
15
16#include "pqxx/util.hxx"
17
18namespace pqxx
19{
21
28class PQXX_LIBEXPORT transaction_focus
29{
30public:
32 transaction_base &t, std::string_view cname, std::string_view oname) :
33 m_trans{&t}, m_classname{cname}, m_name{oname}
34 {}
35
37 transaction_base &t, std::string_view cname, std::string &&oname) :
38 m_trans{&t}, m_classname{cname}, m_name{std::move(oname)}
39 {}
40
41 transaction_focus(transaction_base &t, std::string_view cname) :
42 m_trans{&t}, m_classname{cname}
43 {}
44
48
50 [[nodiscard]] constexpr std::string_view classname() const noexcept
51 {
52 return m_classname;
53 }
54
56 [[nodiscard]] std::string_view name() const &noexcept { return m_name; }
57
58 [[nodiscard]] std::string description() const
59 {
60 return pqxx::internal::describe_object(m_classname, m_name);
61 }
62
64 m_trans{other.m_trans},
65 m_registered{other.m_registered},
66 m_classname{other.m_classname},
67 // We can't move the name until later.
68 m_name{}
69 {
70 // This is a bit more complicated than you might expect. The transaction
71 // has a backpointer to the focus, and we need to transfer that to the new
72 // focus.
73 move_name_and_registration(other);
74 }
75
77 {
78 if (&other != this)
79 {
80 if (m_registered)
81 unregister_me();
82 m_trans = other.m_trans;
83 m_classname = other.m_classname;
84 move_name_and_registration(other);
85 }
86 return *this;
87 }
88
89protected:
90 void register_me();
91 void unregister_me() noexcept;
92 void reg_pending_error(std::string const &) noexcept;
93 bool registered() const noexcept { return m_registered; }
94
96
97private:
98 bool m_registered = false;
99 std::string_view m_classname;
100 std::string m_name;
101
103 void move_name_and_registration(transaction_focus &other)
104 {
105 bool const reg{other.m_registered};
106 // Unregister the original while it still owns its name.
107 if (reg)
108 other.unregister_me();
109 // Now! Quick! Steal that name.
110 m_name = std::move(other.m_name);
111 // Now that we own the name, register ourselves instead.
112 if (reg)
113 this->register_me();
114 }
115};
116} // namespace pqxx
117#endif
The home of all libpqxx classes, functions, templates, etc.
Definition array.hxx:33
std::string describe_object(std::string_view class_name, std::string_view name)
Describe an object for humans, based on class name and optional name.
Definition util.cxx:51
Interface definition (and common code) for "transaction" classes.
Definition transaction_base.hxx:88
Base class for things that monopolise a transaction's attention.
Definition transaction_focus.hxx:29
std::string description() const
Definition transaction_focus.hxx:58
transaction_focus(transaction_base &t, std::string_view cname)
Definition transaction_focus.hxx:41
void unregister_me() noexcept
Definition transaction_base.cxx:527
std::string_view name() const &noexcept
Name for this object, if the caller passed one; empty string otherwise.
Definition transaction_focus.hxx:56
constexpr std::string_view classname() const noexcept
Class name, for human consumption.
Definition transaction_focus.hxx:50
transaction_focus(transaction_base &t, std::string_view cname, std::string &&oname)
Definition transaction_focus.hxx:36
transaction_focus(transaction_focus const &)=delete
transaction_focus(transaction_focus &&other)
Definition transaction_focus.hxx:63
transaction_focus & operator=(transaction_focus &&other)
Definition transaction_focus.hxx:76
transaction_focus(transaction_base &t, std::string_view cname, std::string_view oname)
Definition transaction_focus.hxx:31
transaction_base * m_trans
Definition transaction_focus.hxx:95
transaction_focus & operator=(transaction_focus const &)=delete