Skip to content

Commit c8945e6

Browse files
first commit
1 parent 2e9c8f4 commit c8945e6

File tree

5 files changed

+141
-49
lines changed

5 files changed

+141
-49
lines changed

nautilus-api/include/Interface/DataTypes/Val.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ namespace nautilus {
3131
concept is_base_type = std::is_base_of<base_value, T>::value;
3232

3333
template<class T>
34-
concept is_fundamental = std::is_fundamental_v<T>;
34+
concept is_fundamental = std::is_fundamental_v<T> && !std::is_reference_v<T>;
35+
36+
template<class T>
37+
concept is_fundamental_ref = std::is_reference_v<T>;
3538

3639
template<class T>
3740
concept is_integral = std::is_integral_v<T>;
@@ -324,7 +327,6 @@ namespace nautilus {
324327
typedef typename std::common_type<LHS, RHS>::type commonType;
325328
auto lValue = cast_value<LHS, commonType>(left);
326329
auto rValue = cast_value<RHS, commonType>(right);
327-
328330
TRAC_BINARY_OP(ADD)
329331
return lValue.value + rValue.value;
330332
}

nautilus-api/include/Interface/DataTypes/ValPtr.hpp

Lines changed: 87 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,55 @@
22
// Created by Local on 20.02.24.
33
//
44

5-
#ifndef NAUTILUS_LIB_VALPTR_HPP
6-
#define NAUTILUS_LIB_VALPTR_HPP
5+
#pragma once
76

8-
namespace nautilus{
7+
#include "Val.hpp"
98

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 {
1213
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) {};
1418

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+
}
1825

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+
}
2330

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>;
2841

29-
private:
30-
ValuePtrType ptr;
31-
int8_t alignment;
32-
};
3342

3443
val_ptr(ValuePtrType ptr, int8_t alignment = 1) : ptr(ptr), alignment(alignment) {};
3544

36-
val_ptr<ValuePtrType>::ref operator*() {
37-
return ref(ptr, alignment);
45+
val<ValType &> operator*() {
46+
return val<ValType &>(ptr, alignment);
3847
};
3948

49+
val<ValType &> operator[](int index) {
50+
auto res = ptr + index;
51+
return val<ValType &>(res, alignment);
52+
}
53+
4054

4155
template<typename OtherType>
4256
requires std::is_pointer_v<OtherType>
@@ -49,30 +63,59 @@ namespace nautilus{
4963
int8_t alignment;
5064
};
5165

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+
52116

53117
template<int8_t alignment, typename ValuePtrType>
54118
val_ptr<ValuePtrType> assume_aligned(val_ptr<ValuePtrType> ptr) {
55119
return val_ptr<ValuePtrType>(ptr.ptr, alignment);
56120
}
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-
};
76121
}
77-
78-
#endif //NAUTILUS_LIB_VALPTR_HPP

nautilus-api/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ add_executable(nautilus-api-tests
1515
# Interface/BooleanTypeTest.cpp
1616
Interface/IntegerValTypeTest.cpp
1717
Interface/FloatValTypeTest.cpp
18+
Interface/PtrValTypeTest.cpp
1819
Interface/FunctionTest.cpp
1920
# Interface/IntegerTypeTest.cpp
2021
# Interface/FloatTypeTest.cpp

nautilus-api/test/ExecutionTests/RunctimeCallFunctions.hpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,14 @@ namespace nautilus::engine {
2929
return x;
3030
}
3131

32-
class Clazz {
32+
class ClazzBase {
3333
public:
34-
int32_t add(int32_t x) {
34+
virtual int32_t add(int32_t x) = 0;
35+
};
36+
37+
class Clazz : public ClazzBase {
38+
public:
39+
int32_t add(int32_t x) override {
3540
return x + i;
3641
}
3742

@@ -45,7 +50,7 @@ namespace nautilus::engine {
4550
static auto clazz = Clazz();
4651

4752
val<int32_t> memberFuncCall(val<int32_t> x) {
48-
return invoke<>(&Clazz::add, &clazz, x);
53+
return invoke<>(&ClazzBase::add, (ClazzBase*)&clazz, x);
4954
}
5055

5156
val<int32_t> constMemberFuncCall(val<int32_t> x) {
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
#include <catch2/catch_all.hpp>
3+
#include <Interface/DataTypes/Val.hpp>
4+
#include <Interface/DataTypes/ValPtr.hpp>
5+
#include <iostream>
6+
7+
namespace nautilus {
8+
9+
TEMPLATE_TEST_CASE("Ptr Val Operation Test", "[value][template]", int8_t) {
10+
SECTION("comparison operators") {
11+
TestType value = 42;
12+
13+
int32_t x = 32;
14+
int32_t *z = &x;
15+
16+
[[maybe_unused]]int32_t& h = z[0];
17+
h = 54;
18+
19+
20+
21+
SECTION("==") {
22+
auto f1 = val_ptr<TestType *>(&value);
23+
val_ptr<TestType *> f2 = &value;
24+
REQUIRE(f2 == f1);
25+
}
26+
27+
SECTION("*") {
28+
auto f1 = val_ptr<TestType *>(&value);
29+
[[maybe_unused]] val<TestType> v = *f1;
30+
(*f1) = 54;
31+
REQUIRE(v == 42);
32+
}
33+
34+
SECTION("[]") {
35+
auto f1 = val_ptr<TestType *>(&value);
36+
val<TestType> v = f1[0];
37+
REQUIRE(v == 42);
38+
}
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)