RAII Helpers

RAII helpers for managing lifetime of AngelScript objects.

asIScriptEngine*

inline script_engine asbind20::make_script_engine(asDWORD version = ANGELSCRIPT_VERSION)

Create an AngelScript engine.

class script_engine

Script engine manager.

Public Types

using handle_type = asIScriptEngine*

Public Functions

inline script_engine() noexcept
script_engine(const script_engine&) = delete
inline script_engine(script_engine&&) noexcept
inline explicit script_engine(handle_type engine) noexcept
script_engine &operator=(const script_engine&) = delete
inline script_engine &operator=(script_engine &&other) noexcept
inline ~script_engine()
inline handle_type get() const noexcept
inline operator handle_type() const noexcept
inline handle_type operator->() const noexcept
inline handle_type release() noexcept
inline void reset(handle_type engine = nullptr) noexcept

asIScriptContext*

inline auto asbind20::current_context() -> asIScriptContext*

Get current script context from a function called by script.

Returns:

A pointer to the currently executing context, or null if no context is executing

class request_context

RAII helper for requesting script context from the engine.

Public Types

using handle_type = asIScriptContext*

Public Functions

request_context() = delete
request_context(const request_context&) = delete
request_context &operator=(const request_context&) = delete
inline explicit request_context(asIScriptEngine *engine)
inline ~request_context()
inline handle_type get() const noexcept
inline auto get_engine() const noexcept -> asIScriptEngine*
inline operator handle_type() const noexcept
inline handle_type operator->() const noexcept
class reuse_active_context

RAII helper for reusing active script context.

It will fallback to request context from the engine.

Public Types

using handle_type = asIScriptContext*

Public Functions

reuse_active_context() = delete
reuse_active_context(const reuse_active_context&) = delete
reuse_active_context &operator=(const reuse_active_context&) = delete
inline explicit reuse_active_context(asIScriptEngine *engine, bool propagate_error = true)
inline ~reuse_active_context()
inline handle_type get() const noexcept
inline auto get_engine() const noexcept -> asIScriptEngine*
inline operator handle_type() const noexcept
inline handle_type operator->() const noexcept
inline bool is_nested() const noexcept

Returns true if current context is reused.

inline bool will_propagate_error() const noexcept

asIScriptObject*

class script_object

Smart pointer for script object.

Public Types

using handle_type = asIScriptObject*

Public Functions

script_object() noexcept = default
inline script_object(script_object &&other) noexcept
script_object(const script_object&) = delete
inline explicit script_object(handle_type obj)
inline ~script_object()
inline handle_type get() const noexcept
inline explicit operator handle_type() const noexcept
inline explicit operator bool() const noexcept
inline handle_type operator->() const noexcept
inline handle_type release() noexcept

Release without decreasing reference count.

Warning

USE WITH CAUTION!

Returns:

Previously stored object

inline void reset(std::nullptr_t = nullptr) noexcept

Reset object the null pointer.

inline void reset(handle_type obj)

Reset object.

Parameters:

obj – New object to store

asILockableSharedBool*

class lockable_shared_bool

Helper for asILockableSharedBool*

This class can be helpful for implementing weak reference support.

Public Types

using handle_type = asILockableSharedBool*

Public Functions

lockable_shared_bool() noexcept = default
inline explicit lockable_shared_bool(handle_type bool_)
inline lockable_shared_bool(std::in_place_t, handle_type bool_) noexcept
inline lockable_shared_bool(const lockable_shared_bool &other)
inline lockable_shared_bool(lockable_shared_bool &&other) noexcept
inline ~lockable_shared_bool()
bool operator==(const lockable_shared_bool &other) const = default
inline void reset(std::nullptr_t = nullptr) noexcept
inline void reset(handle_type bool_) noexcept
inline void connect_object(void *obj, asITypeInfo *ti)

Connect to the weak reference flag of object.

Note

If it failed to connect, this helper will be reset to nullptr.

Parameters:
  • obj – Object to connect

  • ti – Type information

inline void reset(std::in_place_t, handle_type bool_) noexcept

See also

connect_object

Warning

If you get the lockable shared bool by GetWeakRefFlagOfScriptObject(), you should not use this function! Because it won’t increase the reference count.

inline lockable_shared_bool &operator=(const lockable_shared_bool &other)
inline lockable_shared_bool &operator=(lockable_shared_bool &&other)
inline void lock()

Lock the flag.

inline void unlock() noexcept

Unlock the flag.

inline bool get_flag() const
inline void set_flag(bool value = true)
inline handle_type get() const noexcept
inline handle_type operator->() const noexcept
inline operator handle_type() const noexcept
inline explicit operator bool() const noexcept
inline void swap(lockable_shared_bool &other) noexcept
inline lockable_shared_bool asbind20::make_lockable_shared_bool()

Create a lockable shared bool for implementing weak reference.

Note

Lock the exclusive lock in multithreading enviornment

IO Helpers

The IO support is in a standalone header <asbind20/io/stream.hpp>, which means you need to include it explicitly if you want to load/save byte code of AngelScript.

group Byte code manipulation

Functions

inline int save_byte_code(std::ostream &os, asIScriptModule *m, bool strip_debug_info = false)

Save byte code to std::ostream

Parameters:
  • os – Output stream

  • m – Script module to save

  • strip_debug_info – Strip debug information

Returns:

Result of asIScriptModule::SaveByteCode

template<typename OutputIteratorValueType = std::byte>
int save_byte_code(std::output_iterator<OutputIteratorValueType> auto out, asIScriptModule *m, bool strip_debug_info = false)

Save byte code to an output iterator.

Template Parameters:

OutputIteratorValueType – Value type of output iterator. It can be any integral type whose size equals to std::byte.

Parameters:
  • out – Output iterator

  • m – Script module

  • strip_debug_info – Strip debug information

Returns:

Result of asIScriptModule::SaveByteCode

inline io::load_byte_code_result load_byte_code(std::istream &is, asIScriptModule *m)

Load byte code from std::istream

Parameters:
  • is – Input stream

  • m – Script module

Returns:

Loading result

inline io::load_byte_code_result load_byte_code(const void *mem, std::size_t size, asIScriptModule *m)

Load byte code from memory buffer.

Parameters:
  • mem – Memory buffer

  • size – Buffer size

  • m – Script module

Returns:

Loading result

struct load_byte_code_result

Result of loading byte code.

Public Functions

inline explicit operator bool() const noexcept

Will return true if r indicates the byte code was successfully loaded.

Public Members

int r

Loading result.

bool debug_info_stripped

Miscellaneous Utilities

inline std::string asbind20::to_string(asEContextState state)

Convert context state enum to string.

Parameters:

state – Context state

Returns:

String representation of the state. If the state value is invalid, the result will be "asEContextState({state})", e.g. "asEContextState(-1)".

inline std::string asbind20::to_string(asERetCodes ret)

Convert return code to string.

Parameters:

ret – Return code

Returns:

String representation of the return code. If the value is invalid, the result will be "asERetCodes({ret})", e.g. "asERetCodes(1)".

template<detail::concat_accepted... Args>
constexpr std::string asbind20::string_concat(Args&&... args)

Concatenate strings.

Parameters:

args – String-like inputs

Returns:

std::string Result

template<typename Fn, typename ...Args>
decltype(auto) asbind20::with_cstr(Fn &&fn, Args&&... args)

This function will convert string and string_view in parameters to null-terminated const char* for APIs receiving C-style string.

This function will make a copy of string view if it is not null-terminated.

template<std::size_t Size>
class fixed_string

Public Types

using value_type = char
using size_type = std::size_t

Public Functions

constexpr fixed_string(const fixed_string&) noexcept = default
template<std::convertible_to<char>... Chars>
inline explicit constexpr fixed_string(Chars... chs)
inline constexpr fixed_string(const char (&str)[Size + 1])
constexpr bool operator==(const fixed_string&) const noexcept = default
template<std::size_t N>
inline constexpr bool operator==(const fixed_string<N>&) const noexcept
inline constexpr const value_type *data() const noexcept
inline constexpr const value_type *c_str() const noexcept
inline constexpr operator const char*() const noexcept
inline constexpr std::string_view view() const noexcept
inline constexpr operator std::string_view() const noexcept

Public Members

char internal_data[Size + 1] = {}

INTERNAL DATA. DO NOT USE!

This member is exposed for satisfying the NTTP requirements of C++.

Note

It includes \0 at the end.

Public Static Functions

static inline constexpr size_type size() noexcept
static inline constexpr bool empty() noexcept
template<typename T1, typename T2>
class compressed_pair

Compressed pair for saving storage space.

This class will use the empty base optimization (EBO) to reduce the size of compressed pair.

Template Parameters:
  • T1 – First member type

  • T2 – Second member type

Public Types

using first_type = T1
using second_type = T2
template<std::size_t Idx>
using element_type = std::conditional_t<Idx == 0, T1, T2>

Public Functions

compressed_pair() = default
compressed_pair(const compressed_pair&) = default
compressed_pair(compressed_pair&&) noexcept(std::is_nothrow_move_constructible_v<T1> && std::is_nothrow_move_constructible_v<T2>) = default
template<typename Tuple1, typename Tuple2>
inline compressed_pair(std::piecewise_construct_t, Tuple1 &&tuple1, Tuple2 &&tuple2)
inline compressed_pair(const T1 &t1, const T2 &t2)
inline compressed_pair(T1 &&t1, T2 &&t2) noexcept(std::is_nothrow_move_constructible_v<T1> && std::is_nothrow_move_constructible_v<T2>)
inline void swap(compressed_pair &other) noexcept(std::is_nothrow_swappable_v<T1> && std::is_nothrow_swappable_v<T2>)

Friends

template<std::size_t Idx>
inline friend element_type<Idx> &get(compressed_pair &cp) noexcept
template<std::size_t Idx>
inline friend const element_type<Idx> &get(const compressed_pair &cp) noexcept
template<std::size_t Idx>
inline friend element_type<Idx> &&get(compressed_pair &&cp) noexcept
template<std::size_t Idx>
inline friend const element_type<Idx> &&get(const compressed_pair &&cp) noexcept

Debugging

inline const char *asbind20::debugging::get_function_section_name(const asIScriptFunction *func)

Get script section name of function.

This helper can handle the different interfaces for getting the section name across AngelScript versions.

Parameters:

func – Script function. It cannot be nullptr.

struct gc_statistics

GC statistics.

Public Types

using value_type = asUINT

Public Functions

bool operator==(const gc_statistics&) const noexcept = default

Public Members

value_type current_size
value_type total_destroyed
value_type total_detected
value_type new_objects
value_type total_new_destroyed
inline gc_statistics asbind20::debugging::get_gc_statistics(asIScriptEngine *engine)

Get the GC statistics.

Parameters:

engine – Script engine. It cannot be nullptr.