Skip to content

Commit

Permalink
Add Variant operators for Dictionary and Array
Browse files Browse the repository at this point in the history
  • Loading branch information
fwsGonzo committed Sep 11, 2024
1 parent 8e91614 commit 1ca559f
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 4 deletions.
4 changes: 4 additions & 0 deletions program/cpp/api/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ inline Variant::Variant(const Array& a) {
v.i = a.get_variant_index();
}

inline Variant::operator Array() const {
return as_array();
}

class ArrayIterator {
public:
ArrayIterator(const Array &array, unsigned idx) : m_array(array), m_idx(idx) {}
Expand Down
4 changes: 4 additions & 0 deletions program/cpp/api/dictionary.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ inline Variant::Variant(const Dictionary &d) {
v.i = d.get_variant_index();
}

inline Variant::operator Dictionary() const {
return as_dictionary();
}

struct DictAccessor {
DictAccessor(const Dictionary &dict, const Variant &key) : m_dict(dict), m_key(key) {}

Expand Down
5 changes: 1 addition & 4 deletions program/cpp/api/string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,7 @@ inline Variant::Variant(const String &s) {
}

inline Variant::operator String() const {
if (m_type != STRING) {
api_throw("std::bad_cast", "Failed to cast Variant to String", this);
}
return String::from_variant_index(v.i);
return as_string();
}

inline String Variant::as_string() const {
Expand Down
2 changes: 2 additions & 0 deletions program/cpp/api/variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ struct Variant
operator std::string() const; // String for STRING and PACKED_BYTE_ARRAY
operator std::u32string() const; // u32string for STRING, STRING_NAME
operator String() const;
operator Array() const;
operator Dictionary() const;

Object as_object() const;
Node as_node() const;
Expand Down

0 comments on commit 1ca559f

Please sign in to comment.