Loading...
Searching...
No Matches
transaction_focus.hxx
#ifndef PQXX_H_TRANSACTION_FOCUS
#define PQXX_H_TRANSACTION_FOCUS
#if !defined(PQXX_HEADER_PRE)
# error "Include libpqxx headers as <pqxx/header>, not <pqxx/header.hxx>."
#endif
#include "pqxx/util.hxx"
namespace pqxx
{
class PQXX_LIBEXPORT transaction_focus
{
public:
transaction_focus(
transaction_base &t, std::string_view cname, std::string_view oname) :
m_trans{&t}, m_classname{cname}, m_name{oname}
{}
transaction_focus(
transaction_base &t, std::string_view cname, std::string &&oname) :
m_trans{&t}, m_classname{cname}, m_name{std::move(oname)}
{}
transaction_focus(transaction_base &t, std::string_view cname) :
m_trans{&t}, m_classname{cname}
{}
transaction_focus() = delete;
transaction_focus(transaction_focus const &) = delete;
transaction_focus &operator=(transaction_focus const &) = delete;
[[nodiscard]] constexpr std::string_view classname() const noexcept
{
return m_classname;
}
[[nodiscard]] std::string_view name() const & noexcept { return m_name; }
[[nodiscard]] std::string description() const
{
return pqxx::internal::describe_object(m_classname, m_name);
}
transaction_focus(transaction_focus &&other) :
m_trans{other.m_trans},
m_registered{other.m_registered},
m_classname{other.m_classname},
// We can't move the name until later.
m_name{}
{
// This is a bit more complicated than you might expect. The transaction
// has a backpointer to the focus, and we need to transfer that to the new
// focus.
move_name_and_registration(other);
}
transaction_focus &operator=(transaction_focus &&other)
{
if (&other != this)
{
if (m_registered)
unregister_me();
m_trans = other.m_trans;
m_classname = other.m_classname;
move_name_and_registration(other);
}
return *this;
}
protected:
void register_me();
void unregister_me() noexcept;
void reg_pending_error(std::string const &) noexcept;
bool registered() const noexcept { return m_registered; }
transaction_base *m_trans;
private:
bool m_registered = false;
std::string_view m_classname;
std::string m_name;
void move_name_and_registration(transaction_focus &other)
{
bool const reg{other.m_registered};
// Unregister the original while it still owns its name.
if (reg)
other.unregister_me();
// Now! Quick! Steal that name.
m_name = std::move(other.m_name);
// Now that we own the name, register ourselves instead.
if (reg)
this->register_me();
}
};
} // namespace pqxx
#endif
pqxx::transaction_focus::name
std::string_view name() const &noexcept
Name for this object, if the caller passed one; empty string otherwise.
Definition transaction_focus.hxx:56
pqxx::transaction_focus::classname
constexpr std::string_view classname() const noexcept
Class name, for human consumption.
Definition transaction_focus.hxx:50
pqxx::internal::describe_object
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
pqxx
The home of all libpqxx classes, functions, templates, etc.
Definition array.cxx:27