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