1- // Copyright 2017-2021 the nyan authors, LGPLv3+. See copying.md for legal info.
1+ // Copyright 2017-2025 the nyan authors, LGPLv3+. See copying.md for legal info.
22#pragma once
33
44#include < memory>
55
6+ #include " ../api_error.h"
7+ #include " ../concept.h"
8+ #include " ../util.h"
9+
610
711namespace nyan {
812
@@ -26,12 +30,26 @@ class ValueHolder {
2630 ValueHolder &operator =(const std::shared_ptr<Value> &value);
2731
2832 /* *
29- * Get the shared pointer of this ValueHolder .
33+ * Get the shared pointer to the value wrapped by this holder .
3034 *
3135 * @return Shared pointer to this holder's value.
3236 */
3337 const std::shared_ptr<Value> &get_ptr () const ;
3438
39+ /* *
40+ * Get a shared pointer to the value stored by this holder.
41+ *
42+ * Auto-converts the value to type T, which must be a nyan::Value type.
43+ *
44+ * @tparam T Type of the value to retrieve.
45+ *
46+ * @return Value stored by this holder.
47+ *
48+ * @throws InternalError if the value is not of type T.
49+ */
50+ template <ValueLike T>
51+ const std::shared_ptr<T> get_value_ptr () const ;
52+
3553 /* *
3654 * Check if this holder points to a value.
3755 *
@@ -74,6 +92,20 @@ class ValueHolder {
7492 std::shared_ptr<Value> value;
7593};
7694
95+
96+ template <ValueLike T>
97+ const std::shared_ptr<T> ValueHolder::get_value_ptr () const {
98+ auto ret = std::dynamic_pointer_cast<T>(this ->value );
99+
100+ if (not ret) {
101+ throw APIError{" ValueHolder does not contain a value of type "
102+ + util::typestring<T>() + " , but got "
103+ + util::typestring (this ->value .get ())};
104+ }
105+
106+ return ret;
107+ }
108+
77109} // namespace nyan
78110
79111
0 commit comments