This repository has been archived by the owner on Jul 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfn.hpp
103 lines (78 loc) · 3.08 KB
/
fn.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2022 The Jule Programming Language.
// Use of this source code is governed by a BSD 3-Clause
// license that can be found in the LICENSE file.
#ifndef __JULE_FN_HPP
#define __JULE_FN_HPP
#include <stddef.h>
#include <functional>
#include <thread>
#include "builtin.hpp"
#include "error.hpp"
#define __JULE_CO(EXPR) \
( std::thread{[&](void) mutable -> void { EXPR; }}.detach() )
namespace jule {
// std::function wrapper of JuleC.
template <typename > struct Fn;
template<typename T, typename... U>
jule::Uintptr addr_of_fn(std::function<T(U...)> f) noexcept;
template <typename Function>
struct Fn {
public:
std::function<Function> buffer;
jule::Uintptr _addr;
Fn<Function>(void) noexcept {}
Fn<Function>(std::nullptr_t) noexcept {}
Fn<Function>(const std::function<Function> &function) noexcept {
this->_addr = jule::addr_of_fn(function);
if (this->_addr == 0)
this->_addr = (jule::Uintptr)(&function);
this->buffer = function;
}
Fn<Function>(const Function *function) noexcept {
this->buffer = function;
this->_addr = jule::addr_of_fn(this->buffer);
if (this->_addr == 0)
this->_addr = (jule::Uintptr)(function);
}
Fn<Function>(const Fn<Function> &fn) noexcept {
this->buffer = fn.buffer;
this->_addr = fn._addr;
}
template<typename ...Arguments>
auto operator()(Arguments... arguments) noexcept {
if (this->buffer == nullptr)
jule::panic(jule::ERROR_INVALID_MEMORY);
return this->buffer(arguments...);
}
jule::Uintptr addr(void) const noexcept
{ return this->_addr; }
inline void operator=(std::nullptr_t) noexcept
{ this->buffer = nullptr; }
inline void operator=(const std::function<Function> &function) noexcept
{ this->buffer = function; }
inline void operator=(const Function &function) noexcept
{ this->buffer = function; }
inline jule::Bool operator==(const Fn<Function> &fn) const noexcept
{ return this->addr() == fn.addr(); }
inline jule::Bool operator!=(const Fn<Function> &fn) const noexcept
{ return !this->operator==(fn); }
inline jule::Bool operator==(std::nullptr_t) const noexcept
{ return this->buffer == nullptr; }
inline jule::Bool operator!=(std::nullptr_t) const noexcept
{ return !this->operator==(nullptr); }
friend std::ostream &operator<<(std::ostream &stream,
const Fn<Function> &src) noexcept {
stream << "<fn>";
return stream;
}
};
template<typename T, typename... U>
jule::Uintptr addr_of_fn(std::function<T(U...)> f) noexcept {
typedef T(FnType)(U...);
FnType **fn_ptr{ f.template target<FnType*>() };
if (!fn_ptr)
return 0;
return (jule::Uintptr)(*fn_ptr);
}
} // namespace jule
#endif // ifndef __JULE_FN_HPP