metadata_base.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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/histogram/axis/traits.hpp>
  9. #include <boost/histogram/detail/replace_type.hpp>
  10. #include <string>
  11. #include <type_traits>
  12. namespace boost {
  13. namespace histogram {
  14. namespace axis {
  15. /** Meta data holder with space optimization for empty meta data types.
  16. Allows write-access to metadata even if const.
  17. @tparam Metadata Wrapped meta data type.
  18. */
  19. template <class Metadata, bool Detail>
  20. class metadata_base {
  21. protected:
  22. using metadata_type = Metadata;
  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 : data_(std::move(o.data_)) {}
  32. metadata_base(metadata_type&& o) noexcept : data_(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. data_ = std::move(o.data_);
  36. return *this;
  37. }
  38. private:
  39. mutable metadata_type data_;
  40. public:
  41. /// Returns reference to metadata.
  42. metadata_type& metadata() noexcept { return data_; }
  43. /// Returns reference to mutable metadata from const axis.
  44. metadata_type& metadata() const noexcept { return data_; }
  45. };
  46. #ifndef BOOST_HISTOGRAM_DOXYGEN_INVOKED
  47. // specialization for empty metadata
  48. template <class Metadata>
  49. class metadata_base<Metadata, true> {
  50. protected:
  51. using metadata_type = Metadata;
  52. metadata_base() = default;
  53. metadata_base(metadata_type&&) {}
  54. metadata_base& operator=(metadata_type&&) { return *this; }
  55. public:
  56. metadata_type& metadata() noexcept {
  57. return static_cast<const metadata_base&>(*this).metadata();
  58. }
  59. metadata_type& metadata() const noexcept {
  60. static metadata_type data;
  61. return data;
  62. }
  63. };
  64. template <class Metadata, class Detail = detail::replace_default<Metadata, std::string>>
  65. using metadata_base_t =
  66. metadata_base<Detail, (std::is_empty<Detail>::value && std::is_final<Detail>::value)>;
  67. #endif
  68. } // namespace axis
  69. } // namespace histogram
  70. } // namespace boost
  71. #endif