Storing Script Objects

Sometimes you may want to store script objects and use them later, or to implement your own container for script objects. There are some tools provided by asbind20 for storing script objects.

Storing a Single Script Object

class single

A set of helper for storing a single script object.

Get the address of the data

This can be used to implemented a function that return reference of data to script

static inline void *data_address(data_type &data, int type_id)
static inline const void *data_address(const data_type &data, int type_id)

Public Static Functions

static inline void *object_ref(const data_type &data) noexcept

Get the referenced object.

This allows direct interaction with the stored object, whether it’s an object handle or not

Note

Only valid if the type of stored data is NOT a primitive value

static inline bool construct(data_type &data, asIScriptEngine *engine, int type_id)

Construct the stored value using its default constructor.

Parameters:
  • engine – Script engine

  • type_id – Type ID. Must NOT be void (asTYPEID_VOID)

Returns:

True if successful

static inline bool copy_construct(data_type &data, asIScriptEngine *engine, int type_id, const void *ref)

Copy construct the stored value from another value.

Note

Make sure this helper doesn’t contain a constructed object previously!

Parameters:
  • engine – Script engine

  • type_id – Type ID. Must NOT be void (asTYPEID_VOID)

  • ref – Address of the value. Must NOT be nullptr

Returns:

True if successful

static inline bool copy_assign_from(data_type &data, asIScriptEngine *engine, int type_id, const void *ref)

Copy assign the stored value from another value.

Note

Make sure the stored value is valid!

Parameters:
  • engine – Script engine

  • type_id – Type ID. Must NOT be void (asTYPEID_VOID)

  • ref – Address of the value. Must NOT be nullptr

Returns:

True if successful

static inline bool copy_assign_to(const data_type &data, asIScriptEngine *engine, int type_id, void *out)

Copy assign the stored value to destination.

Note

Make sure the stored value is valid!

Parameters:
  • engine – Script engine

  • type_id – Type ID. Must NOT be void (asTYPEID_VOID)

  • out – Address of the destination. Must NOT be nullptr

Returns:

True if successful

static inline void destroy(data_type &data, asIScriptEngine *engine, int type_id)

Destroy the stored object.

Parameters:
  • engine – Script engine

  • type_id – Type ID. Must NOT be void (asTYPEID_VOID)

static inline void enum_refs(data_type &data, asITypeInfo *ti)

Enumerate references of stored object for GC.

This function has no effect for non-garbage collected types

Parameters:

ti – Type information

union data_type

Helper for storing data.

Note

This helper needs an external type ID for correctly handle the stored data, so it is recommended to use this helper as a member of container class, together with a member for storing type ID.

Public Functions

inline data_type()
data_type(const data_type&) = delete
inline data_type(data_type &&other) noexcept
inline ~data_type()

Warning

Due to limitations of the AngelScript interface, it won’t properly release the stored object. Remember to manually clear the stored object before destroying the helper!

inline data_type &operator=(data_type &&other) noexcept

Public Members

std::byte primitive[8]

primitive value

void *handle

script handle

void *ptr

script object

You can check the script_optional from extension library as an example.

Containers

Policies of Type Information

Policies for how containers deal with the type information.

template<typename T>
concept typeinfo_policy
#include <options.hpp>

Concept of type information policies.

struct typeinfo_identity
#include <options.hpp>

The type information itself is for the element.

template<asUINT Idx>
struct typeinfo_subtype : public std::integral_constant<asUINT, Idx>
#include <options.hpp>

The subtype of type information is for the element.

Template Parameters:

Idx – Index of the subtype

Sequential Containers

template<typeinfo_policy TypeInfoPolicy, std::size_t StaticCapacityBytes = 4 * sizeof(void*), typename Allocator = as_allocator<void>>
class small_vector

Sequential container for AngelScript objects with small size optimization (SSO)

Most members have the same meaning as member functions of the same name in std::vector

Template Parameters:
  • TypeInfoPolicy – Type information policy for element type

  • StaticCapcityBytes – Static capacity in bytes. Must be aligned with the size of pointer, e.g. 4 * sizeof(void*).

  • Allocator – Allocator type which is able to rebind to multiple types. Its member pointer_type must be compatible with raw pointers.

Type information

inline auto get_type_info() const noexcept -> asITypeInfo*
inline auto element_type_info() const -> asITypeInfo*
inline int element_type_id() const

Capacity

inline size_type static_capacity() const noexcept
inline size_type capacity() const noexcept
inline void reserve(size_type new_cap)
inline void shrink_to_fit()
inline size_type size() const noexcept
inline bool empty() const noexcept

Element access

inline void *data() noexcept
inline const void *data() const noexcept
inline void *operator[](size_type idx) noexcept
inline const void *operator[](size_type idx) const noexcept

Iterators of small vector

inline const_iterator cbegin() const noexcept
inline const_iterator cend() const noexcept
inline const_iterator begin() const noexcept
inline const_iterator end() const noexcept

Modifiers

inline void resize(size_type new_size)
inline void clear() noexcept
inline void push_back(const void *ref)
inline void emplace_back()
inline void push_back_n(size_type n, const void *ref)
inline void emplace_back_n(size_type n)
inline void pop_back() noexcept
inline void insert(size_type where, const void *ref)
inline void insert(const_iterator where, const void *ref)
inline void erase(size_type where, size_type count)
inline void erase(size_type where)
inline void erase(const_iterator start, const_iterator stop)
inline void erase(const_iterator where)
inline size_type remove(size_type where)
inline void assign(size_type where, const void *ref)
inline void assign(const_iterator where, const void *ref)
inline void reverse(size_type start, size_type n = -1)
inline void *data_at(size_type idx) noexcept
inline const void *data_at(size_type idx) const noexcept

Visiting members

template<typename Visitor>
inline decltype(auto) visit(Visitor &&vis, size_type start, size_type count)
template<typename Visitor>
inline decltype(auto) visit(Visitor &&vis, const_iterator start, const_iterator stop)

Public Types

using size_type = std::size_t
using difference_type = std::ptrdiff_t
using allocator_type = Allocator
using pointer = void*
using const_pointer = const void*

Public Functions

small_vector() = delete
inline ~small_vector()
inline small_vector(const small_vector &other)
inline explicit small_vector(asITypeInfo *ti)
inline small_vector(asITypeInfo *ti, script_init_list_repeat ilist)
inline small_vector(asIScriptEngine *engine, int type_id)
inline void enum_refs()

Enumerate references for GC.

class const_iterator

Const iterator of small vector.

Public Types

using value_type = const void*
using iterator_category = std::random_access_iterator_tag
using size_type = std::size_t
using difference_type = std::ptrdiff_t
using pointer = const void*
using reference = const void*
using const_reference = const void*

Public Functions

const_iterator() noexcept = default
const_iterator(const const_iterator&) noexcept = default
inline reference operator*() const noexcept
inline bool operator==(const const_iterator &rhs) const noexcept
inline std::strong_ordering operator<=>(const const_iterator &rhs) const noexcept
inline const_iterator &operator++() noexcept
inline const_iterator &operator--() noexcept
inline const_iterator &operator++(int) noexcept
inline const_iterator &operator--(int) noexcept
inline const_iterator &operator+=(difference_type diff) noexcept
inline const_iterator &operator-=(difference_type diff) noexcept
inline reference operator[](difference_type off) const noexcept
inline const small_vector *get_container() const noexcept
inline explicit operator bool() const noexcept

Friends

inline friend const_iterator operator+(const_iterator lhs, const_iterator rhs) noexcept
inline friend const_iterator operator+(const_iterator lhs, difference_type rhs) noexcept
inline friend const_iterator operator+(difference_type lhs, const_iterator rhs) noexcept
inline friend difference_type operator-(const_iterator lhs, const_iterator rhs) noexcept
inline friend const_iterator operator-(const_iterator lhs, difference_type rhs) noexcept

You can check the script_array from extension library as an example.

Associative Containers

Not implemented yet