libpqxx
The C++ client library for PostgreSQL
transaction_focus.hxx
Go to the documentation of this file.
1 
9 #ifndef PQXX_TRANSACTION_FOCUS_HXX
10 #define PQXX_TRANSACTION_FOCUS_HXX
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 
29 {
30 public:
32 
34  transaction_base &t, std::string_view cname, std::string_view oname) :
35  m_trans{&t}, m_classname{cname}, m_name{oname}
36  {}
37 
39  transaction_base &t, std::string_view cname, std::string &&oname) :
40  m_trans{&t}, m_classname{cname}, m_name{std::move(oname)}
41  {}
42 
43  transaction_focus(transaction_base &t, std::string_view cname) :
44  m_trans{&t}, m_classname{cname}
45  {}
46 
48 
49  transaction_focus() = delete;
52  ~transaction_focus() = default;
53 
55  [[nodiscard]] constexpr std::string_view classname() const noexcept
56  {
57  return m_classname;
58  }
59 
61  [[nodiscard]] std::string_view name() const & noexcept { return m_name; }
62 
63  [[nodiscard]] std::string description() const
64  {
65  return pqxx::internal::describe_object(m_classname, m_name);
66  }
67 
70  m_trans{other.m_trans},
71  m_classname{other.m_classname},
72  m_registered{other.m_registered}
73  {
74  // This is a bit more complicated than you might expect. The transaction
75  // has a backpointer to the focus, and we need to transfer that to the new
76  // focus.
77  move_name_and_registration(other);
78  }
79 
81  {
82  if (&other != this)
83  {
84  if (m_registered)
85  unregister_me();
86  m_trans = other.m_trans;
87  m_classname = other.m_classname;
88  move_name_and_registration(other);
89  }
90  return *this;
91  }
93 
94 protected:
95  void register_me();
96  void unregister_me() noexcept;
97  void reg_pending_error(std::string const &, sl) noexcept;
98  [[nodiscard]] bool registered() const noexcept { return m_registered; }
99 
102  [[nodiscard]] transaction_base &trans() noexcept { return *m_trans; }
104  [[nodiscard]] transaction_base const &trans() const noexcept
105  {
106  return *m_trans;
107  }
109 
110  // NOLINTBEGIN(
111  // cppcoreguidelines-non-private-member-variables-in-classes,
112  // misc-non-private-member-variables-in-classes
113  // )
114  [[deprecated(
115  "This will become private. Use trans() instead.")]] transaction_base
117  // NOLINTEND(
118  // cppcoreguidelines-non-private-member-variables-in-classes,
119  // misc-non-private-member-variables-in-classes
120  // )
121 
122 private:
124  void move_name_and_registration(transaction_focus &other)
125  {
126  bool const reg{other.m_registered};
127  // Unregister the original while it still owns its name.
128  if (reg)
129  other.unregister_me();
130  // Now! Quick! Steal that name.
131  m_name = std::move(other.m_name);
132  // Now that we own the name, register ourselves instead.
133  if (reg)
134  this->register_me();
135  }
136 
137  std::string_view m_classname;
138  std::string m_name;
139  bool m_registered = false;
140 };
141 } // namespace pqxx
142 #endif
Base class for things that monopolise a transaction's attention.
Definition: transaction_focus.hxx:29
std::string description() const
Definition: transaction_focus.hxx:63
transaction_focus(transaction_base &t, std::string_view cname)
Definition: transaction_focus.hxx:43
void unregister_me() noexcept
Definition: transaction_base.cxx:568
std::string_view name() const &noexcept
Name for this object, if the caller passed one; empty string otherwise.
Definition: transaction_focus.hxx:61
constexpr std::string_view classname() const noexcept
Class name, for human consumption.
Definition: transaction_focus.hxx:55
transaction_focus(transaction_base &t, std::string_view cname, std::string &&oname)
Definition: transaction_focus.hxx:38
transaction_focus(transaction_focus const &)=delete
transaction_focus(transaction_focus &&other)
Definition: transaction_focus.hxx:69
transaction_focus & operator=(transaction_focus &&other)
Definition: transaction_focus.hxx:80
transaction_base & trans() noexcept
The transaction focused on this transaction_focus.
Definition: transaction_focus.hxx:102
transaction_focus(transaction_base &t, std::string_view cname, std::string_view oname)
Definition: transaction_focus.hxx:33
transaction_base * m_trans
Definition: transaction_focus.hxx:116
transaction_base const & trans() const noexcept
The transaction focused on this transaction_focus.
Definition: transaction_focus.hxx:104
transaction_focus & operator=(transaction_focus const &)=delete
Interface definition (and common code) for "transaction" classes.
Definition: transaction_base.hxx:151
#define PQXX_LIBEXPORT
Definition: header-pre.hxx:206
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:53
The home of all libpqxx classes, functions, templates, etc.
Definition: array.cxx:26
std::source_location sl
Convenience alias for std::source_location. It's just too long.
Definition: types.hxx:38