Struct Struct

Nested Relationships

Nested Types

Struct Documentation

struct Struct

This type represents a runtime instance of structure in .slint.

This can either be an instance of a name structure introduced with the struct keyword in the .slint file, or an anonymous struct written with the { key: value, } notation.

It can be constructed with the range constructor or initializer lst, and converted into or from a Value with the Value constructor and Value::to_struct().

Public Functions

inline Struct()

Constructs a new empty struct. You can add fields with set_field() and read them with get_field().

inline Struct(const Struct &other)

Creates a new Struct as a copy from other. All fields are copied as well.

inline Struct(Struct &&other)

Creates a new Struct by moving all fields from other into this struct.

inline Struct &operator=(const Struct &other)

Assigns all the fields of other to this struct.

inline Struct &operator=(Struct &&other)

Moves all the fields of other to this struct.

inline ~Struct()

Destroys this struct.

inline Struct(std::initializer_list<std::pair<std::string_view, Value>> args)

Creates a new struct with the fields of the std::initializer_list given by args.

template<typename InputIterator>
inline Struct(InputIterator it, InputIterator end)

Creates a new struct with the fields produced by the iterator it. it is advanced until it equals end.

inline iterator begin() const

Returns an iterator over the fields of the struct.

inline iterator end() const

Returns an iterator that when compared with an iterator returned by begin() can be used to detect when all fields have been visited.

inline std::optional<Value> get_field(std::string_view name) const

Returns the value of the field with the given name; Returns an std::optional without value if the field does not exist.

inline void set_field(std::string_view name, const Value &value)

Sets the value of the field with the given name to the specified value. If the field does not exist yet, it is created; otherwise the existing field is updated to hold the new value.

struct iterator

The Struct::iterator class implements the typical C++ iterator protocol and conveniently provides access to the field names and values of a Struct. It is created by calling either Struct::begin() or Struct::end().

Make sure to compare the iterator to the iterator returned by Struct::end() before de-referencing it. The value returned when de-referencing is a std::pair that holds a std::string_view of the field name as well as a const reference of the value. Both references become invalid when the iterator or the Struct is changed, so make sure to make copies if you want to retain the name or value.

Note that the order in which the iterator exposes the fields is not defined.

If you’re using C++ 17, you can use the convenience destructuring syntax to extract the name and value in one go:

Struct stru = ...;
auto it = stru.begin();
...
++it; // advance iterator to the next field
...
// Check iterator before dereferencing it
if (it != stru.end()) {
    // Extract a view of the name and a const reference to the value in one go.
    auto [field_name, field_value] = *it;
}

Public Types

using value_type = std::pair<std::string_view, const Value&>

A typedef for std::pair<std::string_view, const Value &> that’s returned when dereferencing the iterator.

Public Functions

inline ~iterator()

Destroys this field iterator.

iterator(const iterator&) = delete
iterator &operator=(const iterator&) = delete
iterator(iterator &&other) = default

Move-constructs a new iterator from other.

iterator &operator=(iterator &&other) = default

Move-assigns the iterator other to this and returns a reference to this.

inline iterator &operator++()

The prefix ++ operator advances the iterator to the next entry and returns a reference to this.

inline value_type operator*() const

Dereferences the iterator to return a pair of the key and value.

Friends

inline friend bool operator==(const iterator &a, const iterator &b)

Returns true if a is pointing to the same entry as b; false otherwise.

inline friend bool operator!=(const iterator &a, const iterator &b)

Returns false if a is pointing to the same entry as b; true otherwise.