Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippGrulich committed Mar 9, 2024
1 parent 2e9c8f4 commit c8945e6
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 49 deletions.
6 changes: 4 additions & 2 deletions nautilus-api/include/Interface/DataTypes/Val.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ namespace nautilus {
concept is_base_type = std::is_base_of<base_value, T>::value;

template<class T>
concept is_fundamental = std::is_fundamental_v<T>;
concept is_fundamental = std::is_fundamental_v<T> && !std::is_reference_v<T>;

template<class T>
concept is_fundamental_ref = std::is_reference_v<T>;

template<class T>
concept is_integral = std::is_integral_v<T>;
Expand Down Expand Up @@ -324,7 +327,6 @@ namespace nautilus {
typedef typename std::common_type<LHS, RHS>::type commonType;
auto lValue = cast_value<LHS, commonType>(left);
auto rValue = cast_value<RHS, commonType>(right);

TRAC_BINARY_OP(ADD)
return lValue.value + rValue.value;
}
Expand Down
131 changes: 87 additions & 44 deletions nautilus-api/include/Interface/DataTypes/ValPtr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,55 @@
// Created by Local on 20.02.24.
//

#ifndef NAUTILUS_LIB_VALPTR_HPP
#define NAUTILUS_LIB_VALPTR_HPP
#pragma once

namespace nautilus{
#include "Val.hpp"

template<typename ValuePtrType> requires std::is_pointer_v<ValuePtrType>
class val_ptr {
namespace nautilus {

template<is_fundamental_ref ValueType>
class val<ValueType> : public base_value {
public:
using ValType = std::remove_pointer_t<ValuePtrType>;
using baseType = std::remove_cvref_t<ValueType>;
using ptrType = baseType *;

val(ptrType ptr, int8_t alignment) : ptr(ptr), alignment(alignment) {};

class ref {
public:
ref(ValuePtrType ptr, int8_t alignment) : ptr(ptr), alignment(alignment) {};
template<class T>
void operator=(T other) noexcept {
auto value = make_value<baseType>(other);
// store value
*ptr = value.value;
}

void operator=(val<ValType> &other) noexcept {
// store value
*ptr = other.value;
};
operator val<baseType>() const {
// load
return val<baseType>(*ptr);
}

operator val<ValType>() const {
// load
return val<ValType>(*ptr);
}
private:
ptrType ptr;
int8_t alignment;

};

template<typename ValuePtrType> requires std::is_pointer_v<ValuePtrType>
class val_ptr {
public:
using ValType = std::remove_pointer_t<ValuePtrType>;

private:
ValuePtrType ptr;
int8_t alignment;
};

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

val_ptr<ValuePtrType>::ref operator*() {
return ref(ptr, alignment);
val<ValType &> operator*() {
return val<ValType &>(ptr, alignment);
};

val<ValType &> operator[](int index) {
auto res = ptr + index;
return val<ValType &>(res, alignment);
}


template<typename OtherType>
requires std::is_pointer_v<OtherType>
Expand All @@ -49,30 +63,59 @@ namespace nautilus{
int8_t alignment;
};

#define TRAC_BINARY_PTR_OP(OP) \
if (tracing::inTracer()) {\
auto tc = tracing::traceBinaryOp<tracing::OP, ValueType>(left.state, right.state);\
return val_ptr<ValueType>(tc);\
}


template<typename ValueType>
auto inline operator+(val_ptr<ValueType> left, val<ValueType> right) {
TRAC_BINARY_PTR_OP(ADD)
return left.ptr + details::getRawValue(right);
}


template<typename ValueType>
auto inline operator-(val_ptr<ValueType> left, val<ValueType> right) {
TRAC_BINARY_PTR_OP(SUB)
return left.ptr - details::getRawValue(right);
}

template<typename ValueType>
auto inline operator==(val_ptr<ValueType> left, val_ptr<ValueType> right) {
return val<bool>(left.ptr == right.ptr);
}

template<typename ValueType>
auto inline operator<=(val_ptr<ValueType> left, val_ptr<ValueType> right) {
return val<bool>(left.ptr == right.ptr);
}

template<typename ValueType>
auto inline operator<(val_ptr<ValueType> left, val_ptr<ValueType> right) {
return val<bool>(left.ptr == right.ptr);
}

template<typename ValueType>
auto inline operator>(val_ptr<ValueType> left, val_ptr<ValueType> right) {
return val<bool>(left.ptr == right.ptr);
}

template<typename ValueType>
auto inline operator>=(val_ptr<ValueType> left, val_ptr<ValueType> right) {
return val<bool>(left.ptr == right.ptr);
}

template<typename ValueType>
auto inline operator!=(val_ptr<ValueType> left, val_ptr<ValueType> right) {
return val<bool>(left.ptr != right.ptr);
}


template<int8_t alignment, typename ValuePtrType>
val_ptr<ValuePtrType> assume_aligned(val_ptr<ValuePtrType> ptr) {
return val_ptr<ValuePtrType>(ptr.ptr, alignment);
}


template<typename ValueType> requires std::is_base_of_v<std::string_view, ValueType>
class val<ValueType> {
public:
explicit val(std::string value, const std::source_location loc = std::source_location::current())
: value(value), loc(loc) {};
explicit val(val_ptr<char*> ptr, const std::source_location loc = std::source_location::current())
: value(ptr.ptr), loc(loc) {};
val<ValueType> operator+(val<ValueType> right) {
return value + right.value;
}
val_ptr<char*> data(){
return value.data();
}
private:
ValueType value;
std::source_location loc;
};
}

#endif //NAUTILUS_LIB_VALPTR_HPP
1 change: 1 addition & 0 deletions nautilus-api/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ add_executable(nautilus-api-tests
# Interface/BooleanTypeTest.cpp
Interface/IntegerValTypeTest.cpp
Interface/FloatValTypeTest.cpp
Interface/PtrValTypeTest.cpp
Interface/FunctionTest.cpp
# Interface/IntegerTypeTest.cpp
# Interface/FloatTypeTest.cpp
Expand Down
11 changes: 8 additions & 3 deletions nautilus-api/test/ExecutionTests/RunctimeCallFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,14 @@ namespace nautilus::engine {
return x;
}

class Clazz {
class ClazzBase {
public:
int32_t add(int32_t x) {
virtual int32_t add(int32_t x) = 0;
};

class Clazz : public ClazzBase {
public:
int32_t add(int32_t x) override {
return x + i;
}

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

val<int32_t> memberFuncCall(val<int32_t> x) {
return invoke<>(&Clazz::add, &clazz, x);
return invoke<>(&ClazzBase::add, (ClazzBase*)&clazz, x);
}

val<int32_t> constMemberFuncCall(val<int32_t> x) {
Expand Down
41 changes: 41 additions & 0 deletions nautilus-api/test/Interface/PtrValTypeTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

#include <catch2/catch_all.hpp>
#include <Interface/DataTypes/Val.hpp>
#include <Interface/DataTypes/ValPtr.hpp>
#include <iostream>

namespace nautilus {

TEMPLATE_TEST_CASE("Ptr Val Operation Test", "[value][template]", int8_t) {
SECTION("comparison operators") {
TestType value = 42;

int32_t x = 32;
int32_t *z = &x;

[[maybe_unused]]int32_t& h = z[0];
h = 54;



SECTION("==") {
auto f1 = val_ptr<TestType *>(&value);
val_ptr<TestType *> f2 = &value;
REQUIRE(f2 == f1);
}

SECTION("*") {
auto f1 = val_ptr<TestType *>(&value);
[[maybe_unused]] val<TestType> v = *f1;
(*f1) = 54;
REQUIRE(v == 42);
}

SECTION("[]") {
auto f1 = val_ptr<TestType *>(&value);
val<TestType> v = f1[0];
REQUIRE(v == 42);
}
}
}
}

0 comments on commit c8945e6

Please sign in to comment.