ArkScript
A small, fast, functional and scripting language for video games
UserType.hpp
Go to the documentation of this file.
1/**
2 * @file UserType.hpp
3 * @author Alexandre Plateau ([email protected])
4 * @brief Subtype of the value, capable of handling any C++ type
5 * @date 2020-10-27
6 *
7 * @copyright Copyright (c) 2020-2025
8 *
9 */
10
11#ifndef VM_VALUE_USERTYPE_HPP
12#define VM_VALUE_USERTYPE_HPP
13
14#include <iostream>
15#include <cinttypes>
16
17#include <Ark/Platform.hpp>
18
19namespace Ark
20{
21 namespace internal
22 {
24 {
25 static inline uint16_t id = 0;
26 static uint16_t next()
27 {
28 return id++;
29 }
30 };
31
32 template <typename T>
33 struct type_uid
34 {
35 static inline const uint16_t value = type_uid_impl::next();
36 };
37 }
38
39 /**
40 * @brief A class to be use C++ objects in ArkScript
41 *
42 * A pointer to the value you want to store
43 * must be sent, thus the value must not be destroyed while the UserType lives,
44 * otherwise it would result in an UB when trying to use the object
45 */
47 {
48 public:
49 /**
50 * @brief A structure holding a bunch of pointers to different useful functions related to this usertype
51 *
52 */
54 {
55 std::ostream& (*ostream_func)(std::ostream&, const UserType&) = nullptr;
56 void (*deleter)(void*) = nullptr;
57 };
58
59 /**
60 * @brief Construct a new User Type object
61 *
62 * @tparam T the type of the pointer
63 * @param data a pointer to the data to store in the object
64 * @param block control function block for the usertype, can be nullptr
65 */
66 template <typename T>
67 explicit UserType(T* data = nullptr, ControlFuncs* block = nullptr) noexcept :
68 m_type_id(internal::type_uid<T>::value),
69 m_data(reinterpret_cast<void*>(data)),
70 m_funcs(block)
71 {}
72
73 /**
74 * @brief Free memory through the control functions block
75 *
76 */
77 void del();
78
79 /**
80 * @brief Get the pointer to the object
81 *
82 * @return void*
83 */
84 [[nodiscard]] void* data() const noexcept
85 {
86 return m_data;
87 }
88
89 /**
90 * @brief Check if the object held is of a given type
91 * @details Usage example:
92 * @code
93 * MyType object;
94 * UserType a(&object);
95 * if (a.is<MyType>())
96 * // then ...
97 * else
98 * // otherwise...
99 * @endcode
100 *
101 * @tparam T the type to use for the test
102 * @return true
103 * @return false
104 */
105 template <typename T>
106 [[nodiscard]] bool is() const noexcept
107 {
109 }
110
111 /**
112 * @brief Return the underlying object as a given type
113 *
114 * @tparam T the type in which the underlying data pointer should be converted to
115 * @return T&
116 */
117 template <typename T>
118 T& as() noexcept
119 {
120 return *static_cast<T*>(m_data);
121 }
122
123 /**
124 * @brief Return the underlying object as a given type
125 *
126 * @tparam T the type in which the underlying data pointer should be converted to
127 * @return T&
128 */
129 template <typename T>
130 const T& as() const noexcept
131 {
132 return *static_cast<T*>(m_data);
133 }
134
135 friend ARK_API bool operator==(const UserType& A, const UserType& B) noexcept;
136 friend ARK_API bool operator<(const UserType& A, const UserType& B) noexcept;
137 friend ARK_API std::ostream& operator<<(std::ostream& os, const UserType& A) noexcept;
138
139 private:
140 uint16_t m_type_id;
141 void* m_data;
143 };
144
145}
146
147#endif
#define ARK_API
Definition Module.hpp:28
ArkScript configuration macros.
A class to be use C++ objects in ArkScript.
Definition UserType.hpp:47
UserType(T *data=nullptr, ControlFuncs *block=nullptr) noexcept
Construct a new User Type object.
Definition UserType.hpp:67
void del()
Free memory through the control functions block.
Definition UserType.cpp:5
const T & as() const noexcept
Return the underlying object as a given type.
Definition UserType.hpp:130
friend ARK_API std::ostream & operator<<(std::ostream &os, const UserType &A) noexcept
Definition UserType.cpp:21
uint16_t m_type_id
Definition UserType.hpp:140
ControlFuncs * m_funcs
Definition UserType.hpp:142
T & as() noexcept
Return the underlying object as a given type.
Definition UserType.hpp:118
void * data() const noexcept
Get the pointer to the object.
Definition UserType.hpp:84
bool is() const noexcept
Check if the object held is of a given type.
Definition UserType.hpp:106
friend ARK_API bool operator==(const UserType &A, const UserType &B) noexcept
Definition UserType.cpp:11
friend ARK_API bool operator<(const UserType &A, const UserType &B) noexcept
Definition UserType.cpp:16
A structure holding a bunch of pointers to different useful functions related to this usertype.
Definition UserType.hpp:54
void(* deleter)(void *)
Definition UserType.hpp:56
static uint16_t next()
Definition UserType.hpp:26
static const uint16_t value
Definition UserType.hpp:35