ArkScript
A small, lisp-inspired, functional scripting language
Procedure.hpp
Go to the documentation of this file.
1/**
2 * @file Procedure.hpp
3 * @author Justin Andreas Lacoste (me@justin.cx)
4 * @brief Wrapper object for user-defined functions
5 * @date 2025-06-09
6 *
7 * @copyright Copyright (c) 2025
8 *
9 */
10
11#ifndef ARK_VM_PROCEDURE_HPP
12#define ARK_VM_PROCEDURE_HPP
13
14#include <functional>
15#include <vector>
16
17namespace Ark
18{
19 class Value;
20 class VM;
21
22 /**
23 * @brief Storage class to hold custom functions
24 */
26 {
27 public:
28 using PointerType = Value (*)(std::vector<Value>&, VM*);
29 using CallbackType = std::function<Value(std::vector<Value>&, VM*)>;
30
31 ///
32 /// Due to clang (sometimes) rejecting forward declared types
33 /// in templates (`Value` causes issues), we have to implement
34 /// the constructor for the actual `CallbackType` using SFINAE
35 /// and a templated constructor, such that when clang
36 /// encounters the constructor, it knows the actual
37 /// declaration of `Value`.
38 ///
39 /**
40 * @brief Create a new procedure.
41 */
42 template <typename T>
43 // cppcheck-suppress noExplicitConstructor ; we explicitly want implicit conversion to Procedure
44 Procedure(T&& cb) :
45 m_procedure(cb)
46 {}
47
48 /**
49 * @brief Create a new procedure from a stateless C function pointer.
50 */
51 Procedure(PointerType c_ptr); // cppcheck-suppress noExplicitConstructor ; we explicitly want implicit conversion to Procedure
52
53 Value operator()(std::vector<Value>&, VM*) const;
54
55 bool operator<(const Procedure& other) const noexcept;
56 bool operator==(const Procedure& other) const noexcept;
57
58 friend struct std::hash<Ark::Procedure>;
59
60 private:
61 CallbackType m_procedure;
62 };
63}
64
65template <>
66struct std::hash<Ark::Procedure>
67{
68 [[nodiscard]] std::size_t operator()(const Ark::Procedure& s) const noexcept
69 {
70 return std::hash<const void*> {}(static_cast<const void*>(&s.m_procedure));
71 }
72};
73
74#endif
#define ARK_API
Definition Module.hpp:22
Storage class to hold custom functions.
Definition Procedure.hpp:26
std::function< Value(std::vector< Value > &, VM *)> CallbackType
Definition Procedure.hpp:29
Value(*)(std::vector< Value > &, VM *) PointerType
Definition Procedure.hpp:28
Procedure(T &&cb)
Create a new procedure.
Definition Procedure.hpp:44
The ArkScript virtual machine, executing ArkScript bytecode.
Definition VM.hpp:46
bool operator<(const Namespace &, const Namespace &)
Definition Namespace.hpp:27
bool operator==(const Namespace &A, const Namespace &B)
Definition Namespace.hpp:21
std::size_t operator()(const Ark::Procedure &s) const noexcept
Definition Procedure.hpp:68