option.h - エンジンの C API リファレンス

option.h
  1. #pragma once
  2. namespace stingray_plugin_foundation {
  3. // A class that can optionally hold a value.
  4. template <class T>
  5. class Option
  6. {
  7. bool _has_value;
  8. T _value;
  9. Option(bool has_value, const T &value) : _has_value(has_value), _value(value) {}
  10. public:
  11. // Initializes the object to hold no value.
  12. Option() : _has_value(false), _value() {}
  13. // Initializes the object to hold the specified value.
  14. Option(const T &value) : _has_value(true), _value(value) {}
  15. // Static constructor that returns an Option<T> object with no value.
  16. static Option<T> none() {return Option<T>();}
  17. // Static constructor that returns an Option<T> object with the specified value.
  18. static Option<T> some(const T &value) {return Option<T>(true, value);}
  19. // True if the object has a value.
  20. bool is_some() const {return _has_value;}
  21. // True if the object doesn't have a value.
  22. bool is_none() const {return !_has_value;}
  23. // Returns the object's value.
  24. const T &value() const {
  25. XASSERT(is_some(), "Cannot obtain value from a none Option<T>.");
  26. return _value;
  27. }
  28. };
  29. }