🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.5 KiB

  1. #ifndef ENTT_CORE_MONOSTATE_HPP
  2. #define ENTT_CORE_MONOSTATE_HPP
  3. #include "../config/config.h"
  4. #include "fwd.hpp"
  5. namespace entt {
  6. /**
  7. * @brief Minimal implementation of the monostate pattern.
  8. *
  9. * A minimal, yet complete configuration system built on top of the monostate
  10. * pattern. Thread safe by design, it works only with basic types like `int`s or
  11. * `bool`s.<br/>
  12. * Multiple types and therefore more than one value can be associated with a
  13. * single key. Because of this, users must pay attention to use the same type
  14. * both during an assignment and when they try to read back their data.
  15. * Otherwise, they can incur in unexpected results.
  16. */
  17. template<id_type>
  18. struct monostate {
  19. /**
  20. * @brief Assigns a value of a specific type to a given key.
  21. * @tparam Type Type of the value to assign.
  22. * @param val User data to assign to the given key.
  23. */
  24. template<typename Type>
  25. void operator=(Type val) const ENTT_NOEXCEPT {
  26. value<Type> = val;
  27. }
  28. /**
  29. * @brief Gets a value of a specific type for a given key.
  30. * @tparam Type Type of the value to get.
  31. * @return Stored value, if any.
  32. */
  33. template<typename Type>
  34. operator Type() const ENTT_NOEXCEPT {
  35. return value<Type>;
  36. }
  37. private:
  38. template<typename Type>
  39. inline static ENTT_MAYBE_ATOMIC(Type) value{};
  40. };
  41. /**
  42. * @brief Helper variable template.
  43. * @tparam Value Value used to differentiate between different variables.
  44. */
  45. template<id_type Value>
  46. inline monostate<Value> monostate_v = {};
  47. } // namespace entt
  48. #endif