libpqxx  7.8.0
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 
18 namespace pqxx
19 {
21 
28 class PQXX_LIBEXPORT transaction_focus
29 {
30 public:
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 
45  transaction_focus() = delete;
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) unregister_me();
81  m_trans = other.m_trans;
82  m_classname = other.m_classname;
83  move_name_and_registration(other);
84  }
85  return *this;
86  }
87 
88 protected:
89  void register_me();
90  void unregister_me() noexcept;
91  void reg_pending_error(std::string const &) noexcept;
92  bool registered() const noexcept { return m_registered; }
93 
95 
96 private:
97  bool m_registered = false;
98  std::string_view m_classname;
99  std::string m_name;
100 
102  void move_name_and_registration(transaction_focus &other)
103  {
104  bool const reg{other.m_registered};
105  // Unregister the original while it still owns its name.
106  if (reg) other.unregister_me();
107  // Now! Quick! Steal that name.
108  m_name = std::move(other.m_name);
109  // Now that we own the name, register ourselves instead.
110  if (reg) this->register_me();
111  }
112 };
113 } // namespace pqxx
114 #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:94
transaction_focus & operator=(transaction_focus const &)=delete