2
2
// Created by Local on 20.02.24.
3
3
//
4
4
5
- #ifndef NAUTILUS_LIB_VALPTR_HPP
6
- #define NAUTILUS_LIB_VALPTR_HPP
5
+ #pragma once
7
6
8
- namespace nautilus {
7
+ # include " Val.hpp "
9
8
10
- template <typename ValuePtrType> requires std::is_pointer_v<ValuePtrType>
11
- class val_ptr {
9
+ namespace nautilus {
10
+
11
+ template <is_fundamental_ref ValueType>
12
+ class val <ValueType> : public base_value {
12
13
public:
13
- using ValType = std::remove_pointer_t <ValuePtrType>;
14
+ using baseType = std::remove_cvref_t <ValueType>;
15
+ using ptrType = baseType *;
16
+
17
+ val (ptrType ptr, int8_t alignment) : ptr(ptr), alignment(alignment) {};
14
18
15
- class ref {
16
- public:
17
- ref (ValuePtrType ptr, int8_t alignment) : ptr(ptr), alignment(alignment) {};
19
+ template <class T >
20
+ void operator =(T other) noexcept {
21
+ auto value = make_value<baseType>(other);
22
+ // store value
23
+ *ptr = value.value ;
24
+ }
18
25
19
- void operator =( val<ValType> &other) noexcept {
20
- // store value
21
- *ptr = other. value ;
22
- };
26
+ operator val<baseType>() const {
27
+ // load
28
+ return val<baseType>( *ptr) ;
29
+ }
23
30
24
- operator val<ValType>() const {
25
- // load
26
- return val<ValType>(*ptr);
27
- }
31
+ private:
32
+ ptrType ptr;
33
+ int8_t alignment;
34
+
35
+ };
36
+
37
+ template <typename ValuePtrType> requires std::is_pointer_v<ValuePtrType>
38
+ class val_ptr {
39
+ public:
40
+ using ValType = std::remove_pointer_t <ValuePtrType>;
28
41
29
- private:
30
- ValuePtrType ptr;
31
- int8_t alignment;
32
- };
33
42
34
43
val_ptr (ValuePtrType ptr, int8_t alignment = 1 ) : ptr(ptr), alignment(alignment) {};
35
44
36
- val_ptr<ValuePtrType>::ref operator *() {
37
- return ref (ptr, alignment);
45
+ val<ValType &> operator *() {
46
+ return val<ValType &> (ptr, alignment);
38
47
};
39
48
49
+ val<ValType &> operator [](int index) {
50
+ auto res = ptr + index;
51
+ return val<ValType &>(res, alignment);
52
+ }
53
+
40
54
41
55
template <typename OtherType>
42
56
requires std::is_pointer_v<OtherType>
@@ -49,30 +63,59 @@ namespace nautilus{
49
63
int8_t alignment;
50
64
};
51
65
66
+ #define TRAC_BINARY_PTR_OP (OP ) \
67
+ if (tracing::inTracer()) {\
68
+ auto tc = tracing::traceBinaryOp<tracing::OP, ValueType>(left.state , right.state );\
69
+ return val_ptr<ValueType>(tc);\
70
+ }
71
+
72
+
73
+ template <typename ValueType>
74
+ auto inline operator +(val_ptr<ValueType> left, val<ValueType> right) {
75
+ TRAC_BINARY_PTR_OP (ADD)
76
+ return left.ptr + details::getRawValue (right);
77
+ }
78
+
79
+
80
+ template <typename ValueType>
81
+ auto inline operator -(val_ptr<ValueType> left, val<ValueType> right) {
82
+ TRAC_BINARY_PTR_OP (SUB)
83
+ return left.ptr - details::getRawValue (right);
84
+ }
85
+
86
+ template <typename ValueType>
87
+ auto inline operator ==(val_ptr<ValueType> left, val_ptr<ValueType> right) {
88
+ return val<bool >(left.ptr == right.ptr );
89
+ }
90
+
91
+ template <typename ValueType>
92
+ auto inline operator <=(val_ptr<ValueType> left, val_ptr<ValueType> right) {
93
+ return val<bool >(left.ptr == right.ptr );
94
+ }
95
+
96
+ template <typename ValueType>
97
+ auto inline operator <(val_ptr<ValueType> left, val_ptr<ValueType> right) {
98
+ return val<bool >(left.ptr == right.ptr );
99
+ }
100
+
101
+ template <typename ValueType>
102
+ auto inline operator >(val_ptr<ValueType> left, val_ptr<ValueType> right) {
103
+ return val<bool >(left.ptr == right.ptr );
104
+ }
105
+
106
+ template <typename ValueType>
107
+ auto inline operator >=(val_ptr<ValueType> left, val_ptr<ValueType> right) {
108
+ return val<bool >(left.ptr == right.ptr );
109
+ }
110
+
111
+ template <typename ValueType>
112
+ auto inline operator !=(val_ptr<ValueType> left, val_ptr<ValueType> right) {
113
+ return val<bool >(left.ptr != right.ptr );
114
+ }
115
+
52
116
53
117
template <int8_t alignment, typename ValuePtrType>
54
118
val_ptr<ValuePtrType> assume_aligned (val_ptr<ValuePtrType> ptr) {
55
119
return val_ptr<ValuePtrType>(ptr.ptr , alignment);
56
120
}
57
-
58
-
59
- template <typename ValueType> requires std::is_base_of_v<std::string_view, ValueType>
60
- class val <ValueType> {
61
- public:
62
- explicit val (std::string value, const std::source_location loc = std::source_location::current())
63
- : value(value), loc(loc) {};
64
- explicit val (val_ptr<char *> ptr, const std::source_location loc = std::source_location::current())
65
- : value(ptr.ptr), loc(loc) {};
66
- val<ValueType> operator +(val<ValueType> right) {
67
- return value + right.value ;
68
- }
69
- val_ptr<char *> data (){
70
- return value.data ();
71
- }
72
- private:
73
- ValueType value;
74
- std::source_location loc;
75
- };
76
121
}
77
-
78
- #endif // NAUTILUS_LIB_VALPTR_HPP
0 commit comments