metadata_base.hpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2019 Hans Dembinski
  2. //
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt
  5. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_HISTOGRAM_AXIS_METADATA_BASE_HPP
  7. #define BOOST_HISTOGRAM_AXIS_METADATA_BASE_HPP
  8. #include <boost/core/empty_value.hpp>
  9. #include <boost/histogram/detail/relaxed_equal.hpp>
  10. #include <boost/histogram/detail/replace_type.hpp>
  11. #include <string>
  12. #include <type_traits>
  13. namespace boost {
  14. namespace histogram {
  15. namespace axis {
  16. /// Meta data holder with space optimization for empty meta data types.
  17. template <class Metadata,
  18. class DetailMetadata = detail::replace_default<Metadata, std::string>>
  19. class metadata_base : empty_value<DetailMetadata> {
  20. using base_t = empty_value<DetailMetadata>;
  21. protected:
  22. using metadata_type = DetailMetadata;
  23. // std::string explicitly guarantees nothrow only in C++17
  24. static_assert(std::is_same<metadata_type, std::string>::value ||
  25. std::is_nothrow_move_constructible<metadata_type>::value,
  26. "metadata must be nothrow move constructible");
  27. metadata_base() = default;
  28. metadata_base(const metadata_base&) = default;
  29. metadata_base& operator=(const metadata_base&) = default;
  30. // make noexcept because std::string is nothrow move constructible only in C++17
  31. metadata_base(metadata_base&& o) noexcept : base_t(std::move(o)) {}
  32. metadata_base(metadata_type&& o) noexcept : base_t(empty_init_t{}, std::move(o)) {}
  33. // make noexcept because std::string is nothrow move constructible only in C++17
  34. metadata_base& operator=(metadata_base&& o) noexcept {
  35. base_t::operator=(o);
  36. return *this;
  37. }
  38. public:
  39. /// Returns reference to metadata.
  40. metadata_type& metadata() noexcept { return base_t::get(); }
  41. /// Returns reference to const metadata.
  42. const metadata_type& metadata() const noexcept { return base_t::get(); }
  43. bool operator==(const metadata_base& o) const noexcept {
  44. return detail::relaxed_equal(metadata(), o.metadata());
  45. }
  46. bool operator!=(const metadata_base& o) const noexcept {
  47. return operator==(o.metadata());
  48. }
  49. };
  50. } // namespace axis
  51. } // namespace histogram
  52. } // namespace boost
  53. #endif