Skip to content

Commit

Permalink
jule: reimplement traits
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Mar 27, 2024
1 parent 675385a commit cd624f1
Show file tree
Hide file tree
Showing 18 changed files with 848 additions and 370 deletions.
17 changes: 5 additions & 12 deletions api/any.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,12 @@
#include <ostream>

#include "str.hpp"
#include "builtin.hpp"
#include "ptr.hpp"

namespace jule
{

// Built-in any type.
class Any;

class Any
{
private:
public:
template <typename T>
struct DynamicType
{
Expand Down Expand Up @@ -135,7 +129,7 @@ namespace jule
template <typename T>
void __assign(const T &expr) noexcept
{
T *alloc = new (std::nothrow) T;
auto alloc = new (std::nothrow) T;
if (!alloc)
jule::panic(__JULE_ERROR__MEMORY_ALLOCATION_FAILED
"\nruntime: memory allocation failed for heap data of type any");
Expand All @@ -149,7 +143,6 @@ namespace jule
{
if (this->data)
this->type->dealloc(this->data);

this->type = nullptr;
this->data = nullptr;
}
Expand All @@ -167,7 +160,7 @@ namespace jule
}

template <typename T>
Any& operator=(const T &expr) noexcept
Any &operator=(const T &expr) noexcept
{
jule::Any::Type *type = jule::Any::new_type<T>();
if (this->type != nullptr && this->type == type)
Expand All @@ -181,7 +174,7 @@ namespace jule
return *this;
}

Any& operator=(const jule::Any &src) noexcept
Any &operator=(const jule::Any &src) noexcept
{
// Assignment to itself.
if (this->data != nullptr && this->data == src.data)
Expand All @@ -192,7 +185,7 @@ namespace jule
return *this;
}

inline Any& operator=(const std::nullptr_t) noexcept
inline Any &operator=(const std::nullptr_t) noexcept
{
this->dealloc();
return *this;
Expand Down
16 changes: 14 additions & 2 deletions api/derive/clone.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ namespace jule
jule::Ptr<T> clone(const jule::Ptr<T> &r);
template <typename T>
jule::Trait<T> clone(const jule::Trait<T> &t);
template <typename Mask>
jule::Trait2<Mask> clone(const jule::Trait2<Mask> &t);
template <typename T>
jule::Fn<T> clone(const jule::Fn<T> &fn) noexcept;
template <typename T>
Expand Down Expand Up @@ -91,8 +93,7 @@ namespace jule
{
if (r == nullptr)
return r;

return jule::Ptr<T>::make(jule::clone(r.operator T()));
return jule::Ptr<T>::make(jule::clone(*r));
}

template <typename T>
Expand All @@ -103,6 +104,17 @@ namespace jule
return t;
}

template <typename Mask>
jule::Trait2<Mask> clone(const jule::Trait2<Mask> &t) {
if (t == nullptr)
return t;
jule::Trait2<Mask> clone;
clone.type = t.type;
clone.type_offset = t.type_offset;
clone.data = jule::Ptr<jule::Uintptr>::make(t.type->clone(t.data.alloc));
return clone;
}

template <typename T>
jule::Fn<T> clone(const jule::Fn<T> &fn) noexcept
{
Expand Down
15 changes: 14 additions & 1 deletion api/ptr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,19 @@ namespace jule
return *this->alloc;
}

template <typename T2>
jule::Ptr<T2> as(void) const noexcept
{
jule::Ptr<T2> ptr;
ptr.ref = this->ref;
#ifndef __JULE_DISABLE__REFERENCE_COUNTING
if (this->ref)
this->add_ref();
#endif
ptr.alloc = reinterpret_cast<T2 *>(this->alloc);
return ptr;
}

inline T *operator->(void) const noexcept
{
return this->ptr(
Expand Down Expand Up @@ -272,7 +285,7 @@ namespace jule
}
}

Ptr& operator=(const jule::Ptr<T> &src) noexcept
Ptr &operator=(const jule::Ptr<T> &src) noexcept
{
// Assignment to itself.
if (this->alloc != nullptr && this->alloc == src.alloc)
Expand Down
13 changes: 5 additions & 8 deletions api/slice.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,8 @@ namespace jule
*(this->_slice + i) = def;
}

using Iterator = Item*;
using ConstIterator = const Item*;
using Iterator = Item *;
using ConstIterator = const Item *;

constexpr Iterator begin(void) noexcept
{
Expand Down Expand Up @@ -323,10 +323,7 @@ namespace jule
{
jule::Slice<Item> _new;
_new.alloc_new(this->_len + 1, (this->_len + 1) << 1);
std::move(
this->_slice,
this->_slice + this->_len,
_new._slice);
std::move(this->_slice, this->_slice + this->_len, _new._slice);
*(_new._slice + this->_len) = item;

this->operator=(_new);
Expand Down Expand Up @@ -441,7 +438,7 @@ namespace jule
index);
}

Slice& operator=(const jule::Slice<Item> &src) noexcept
Slice &operator=(const jule::Slice<Item> &src) noexcept
{
// Assignment to itself.
if (this->data.alloc != nullptr && this->data.alloc == src.data.alloc)
Expand All @@ -457,7 +454,7 @@ namespace jule
return *this;
}

inline Slice& operator=(const std::nullptr_t) noexcept
inline Slice &operator=(const std::nullptr_t) noexcept
{
this->dealloc();
return *this;
Expand Down
Loading

0 comments on commit cd624f1

Please sign in to comment.