X-Git-Url: https://git.efficios.com/?a=blobdiff_plain;f=src%2Fcpp-common%2Fbt2%2Fclock-class.hpp;h=f9814a78b58c0df128cf5a214d2f835012288592;hb=aa5aac94c5ec89c7f361cf37c4df668229704180;hp=c312bdbfb71468ac9cfd79c638f791fe47e49d7f;hpb=b5f55e9f83cf782b3d423912cfffece103177fe1;p=babeltrace.git diff --git a/src/cpp-common/bt2/clock-class.hpp b/src/cpp-common/bt2/clock-class.hpp index c312bdbf..f9814a78 100644 --- a/src/cpp-common/bt2/clock-class.hpp +++ b/src/cpp-common/bt2/clock-class.hpp @@ -7,31 +7,32 @@ #ifndef BABELTRACE_CPP_COMMON_BT2_CLOCK_CLASS_HPP #define BABELTRACE_CPP_COMMON_BT2_CLOCK_CLASS_HPP -#include #include -#include +#include + #include -#include "internal/borrowed-obj.hpp" -#include "internal/shared-obj.hpp" -#include "cpp-common/optional.hpp" -#include "cpp-common/string_view.hpp" -#include "cpp-common/uuid-view.hpp" -#include "lib-error.hpp" +#include "cpp-common/bt2c/c-string-view.hpp" +#include "cpp-common/bt2c/uuid.hpp" +#include "cpp-common/bt2s/optional.hpp" + +#include "borrowed-object.hpp" +#include "exc.hpp" +#include "internal/utils.hpp" +#include "shared-object.hpp" #include "value.hpp" namespace bt2 { - namespace internal { struct ClockClassRefFuncs final { - static void get(const bt_clock_class * const libObjPtr) + static void get(const bt_clock_class * const libObjPtr) noexcept { bt_clock_class_get_ref(libObjPtr); } - static void put(const bt_clock_class * const libObjPtr) + static void put(const bt_clock_class * const libObjPtr) noexcept { bt_clock_class_put_ref(libObjPtr); } @@ -62,17 +63,14 @@ struct CommonClockClassSpec final } /* namespace internal */ -class ClockClassOffset final +class ClockOffset final { public: - explicit ClockClassOffset(const std::int64_t seconds, const std::uint64_t cycles) : + explicit ClockOffset(const std::int64_t seconds, const std::uint64_t cycles) : _mSeconds {seconds}, _mCycles {cycles} { } - ClockClassOffset(const ClockClassOffset&) noexcept = default; - ClockClassOffset& operator=(const ClockClassOffset&) noexcept = default; - std::int64_t seconds() const noexcept { return _mSeconds; @@ -89,188 +87,162 @@ private: }; template -class CommonClockClass final : public internal::BorrowedObj +class CommonClockClass final : public BorrowedObject { private: - using typename internal::BorrowedObj::_ThisBorrowedObj; - using typename internal::BorrowedObj::_LibObjPtr; - using _ThisCommonClockClass = CommonClockClass; + using typename BorrowedObject::_ThisBorrowedObject; public: - using Shared = - internal::SharedObj<_ThisCommonClockClass, LibObjT, internal::ClockClassRefFuncs>; - - using UserAttributes = - typename std::conditional::value, ConstMapValue, MapValue>::type; + using typename BorrowedObject::LibObjPtr; + using Shared = SharedObject; + using UserAttributes = internal::DepUserAttrs; - explicit CommonClockClass(const _LibObjPtr libObjPtr) noexcept : _ThisBorrowedObj {libObjPtr} + explicit CommonClockClass(const LibObjPtr libObjPtr) noexcept : _ThisBorrowedObject {libObjPtr} { } template - CommonClockClass(const CommonClockClass& clkClass) noexcept : - _ThisBorrowedObj {clkClass} + CommonClockClass(const CommonClockClass clkClass) noexcept : + _ThisBorrowedObject {clkClass} { } template - _ThisCommonClockClass& operator=(const CommonClockClass& clkClass) noexcept + CommonClockClass& operator=(const CommonClockClass clkClass) noexcept { - _ThisBorrowedObj::operator=(clkClass); + _ThisBorrowedObject::operator=(clkClass); return *this; } - void frequency(const std::uint64_t frequency) noexcept + CommonClockClass asConst() const noexcept + { + return CommonClockClass {*this}; + } + + void frequency(const std::uint64_t frequency) const noexcept { - static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); + static_assert(!std::is_const::value, "Not available with `bt2::ConstClockClass`."); - bt_clock_class_set_frequency(this->_libObjPtr(), frequency); + bt_clock_class_set_frequency(this->libObjPtr(), frequency); } std::uint64_t frequency() const noexcept { - return bt_clock_class_get_frequency(this->_libObjPtr()); + return bt_clock_class_get_frequency(this->libObjPtr()); } - void offset(const ClockClassOffset& offset) noexcept + void offsetFromOrigin(const ClockOffset& offsetFromOrigin) const noexcept { - static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); + static_assert(!std::is_const::value, "Not available with `bt2::ConstClockClass`."); - bt_clock_class_set_offset(this->_libObjPtr(), offset.seconds(), offset.cycles()); + bt_clock_class_set_offset(this->libObjPtr(), offsetFromOrigin.seconds(), + offsetFromOrigin.cycles()); } - ClockClassOffset offset() const noexcept + ClockOffset offsetFromOrigin() const noexcept { std::int64_t seconds; std::uint64_t cycles; - bt_clock_class_get_offset(this->_libObjPtr(), &seconds, &cycles); - return ClockClassOffset {seconds, cycles}; + bt_clock_class_get_offset(this->libObjPtr(), &seconds, &cycles); + return ClockOffset {seconds, cycles}; } - void precision(const std::uint64_t precision) noexcept + void precision(const std::uint64_t precision) const noexcept { - static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); + static_assert(!std::is_const::value, "Not available with `bt2::ConstClockClass`."); - bt_clock_class_set_precision(this->_libObjPtr(), precision); + bt_clock_class_set_precision(this->libObjPtr(), precision); } std::uint64_t precision() const noexcept { - return bt_clock_class_get_precision(this->_libObjPtr()); + return bt_clock_class_get_precision(this->libObjPtr()); } - void originIsUnixEpoch(const bool originIsUnixEpoch) noexcept + void originIsUnixEpoch(const bool originIsUnixEpoch) const noexcept { - static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); + static_assert(!std::is_const::value, "Not available with `bt2::ConstClockClass`."); - bt_clock_class_set_origin_is_unix_epoch(this->_libObjPtr(), + bt_clock_class_set_origin_is_unix_epoch(this->libObjPtr(), static_cast(originIsUnixEpoch)); } bool originIsUnixEpoch() const noexcept { - return static_cast(bt_clock_class_origin_is_unix_epoch(this->_libObjPtr())); + return static_cast(bt_clock_class_origin_is_unix_epoch(this->libObjPtr())); } - void name(const char * const name) + void name(const bt2c::CStringView name) const { - static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); + static_assert(!std::is_const::value, "Not available with `bt2::ConstClockClass`."); - const auto status = bt_clock_class_set_name(this->_libObjPtr(), name); + const auto status = bt_clock_class_set_name(this->libObjPtr(), name); if (status == BT_CLOCK_CLASS_SET_NAME_STATUS_MEMORY_ERROR) { - throw LibMemoryError {}; + throw MemoryError {}; } } - void name(const std::string& name) + bt2c::CStringView name() const noexcept { - this->name(name.data()); + return bt_clock_class_get_name(this->libObjPtr()); } - nonstd::optional name() const noexcept + void description(const bt2c::CStringView description) const { - const auto name = bt_clock_class_get_name(this->_libObjPtr()); + static_assert(!std::is_const::value, "Not available with `bt2::ConstClockClass`."); - if (name) { - return name; - } - - return nonstd::nullopt; - } - - void description(const char * const description) - { - static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); - - const auto status = bt_clock_class_set_description(this->_libObjPtr(), description); + const auto status = bt_clock_class_set_description(this->libObjPtr(), description); if (status == BT_CLOCK_CLASS_SET_DESCRIPTION_STATUS_MEMORY_ERROR) { - throw LibMemoryError {}; + throw MemoryError {}; } } - void description(const std::string& description) + bt2c::CStringView description() const noexcept { - this->description(description.data()); + return bt_clock_class_get_description(this->libObjPtr()); } - nonstd::optional description() const noexcept + void uuid(const std::uint8_t * const uuid) const noexcept { - const auto description = bt_clock_class_get_description(this->_libObjPtr()); - - if (description) { - return description; - } - - return nonstd::nullopt; + bt_clock_class_set_uuid(this->libObjPtr(), uuid); } - void uuid(const std::uint8_t * const uuid) noexcept + bt2s::optional uuid() const noexcept { - bt_clock_class_set_uuid(this->_libObjPtr(), uuid); - } - - nonstd::optional uuid() const noexcept - { - const auto uuid = bt_clock_class_get_uuid(this->_libObjPtr()); + const auto uuid = bt_clock_class_get_uuid(this->libObjPtr()); if (uuid) { - return bt2_common::UuidView {uuid}; + return bt2c::UuidView {uuid}; } - return nonstd::nullopt; + return bt2s::nullopt; } template - void userAttributes(const CommonMapValue& userAttrs) + void userAttributes(const CommonMapValue userAttrs) const noexcept { - static_assert(!std::is_const::value, "`LibObjT` must NOT be `const`."); + static_assert(!std::is_const::value, "Not available with `bt2::ConstClockClass`."); - bt_clock_class_set_user_attributes(this->_libObjPtr(), userAttrs._libObjPtr()); - } - - ConstMapValue userAttributes() const noexcept - { - return ConstMapValue {internal::CommonClockClassSpec::userAttributes( - this->_libObjPtr())}; + bt_clock_class_set_user_attributes(this->libObjPtr(), userAttrs.libObjPtr()); } - UserAttributes userAttributes() noexcept + UserAttributes userAttributes() const noexcept { return UserAttributes { - internal::CommonClockClassSpec::userAttributes(this->_libObjPtr())}; + internal::CommonClockClassSpec::userAttributes(this->libObjPtr())}; } std::int64_t cyclesToNsFromOrigin(const std::uint64_t value) const { std::int64_t nsFromOrigin; const auto status = - bt_clock_class_cycles_to_ns_from_origin(this->_libObjPtr(), value, &nsFromOrigin); + bt_clock_class_cycles_to_ns_from_origin(this->libObjPtr(), value, &nsFromOrigin); if (status == BT_CLOCK_CLASS_CYCLES_TO_NS_FROM_ORIGIN_STATUS_OVERFLOW_ERROR) { - throw LibOverflowError {}; + throw OverflowError {}; } return nsFromOrigin; @@ -278,13 +250,32 @@ public: Shared shared() const noexcept { - return Shared {*this}; + return Shared::createWithRef(*this); } }; using ClockClass = CommonClockClass; using ConstClockClass = CommonClockClass; +namespace internal { + +struct ClockClassTypeDescr +{ + using Const = ConstClockClass; + using NonConst = ClockClass; +}; + +template <> +struct TypeDescr : public ClockClassTypeDescr +{ +}; + +template <> +struct TypeDescr : public ClockClassTypeDescr +{ +}; + +} /* namespace internal */ } /* namespace bt2 */ #endif /* BABELTRACE_CPP_COMMON_BT2_CLOCK_CLASS_HPP */