-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for offset based serialization
offset_ptr: stores the offset as difference between `this` and the object it points to Serializing this allows to skip the deserialization step and just use a `reinterpret_cast<T>(buf.begin())`. This is useful for example in situations with shared memory.
- Loading branch information
1 parent
1dd9c13
commit c829cd0
Showing
10 changed files
with
472 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#pragma once | ||
|
||
#include "cista/offset_t.h" | ||
|
||
namespace cista { | ||
|
||
template <typename T> | ||
struct offset_ptr { | ||
offset_ptr() = default; | ||
offset_ptr(std::nullptr_t) : offset_{NULLPTR_OFFSET} {} | ||
offset_ptr(T const* p) : offset_{ptr_to_offset(p)} {} | ||
|
||
offset_ptr& operator=(T const* p) { | ||
offset_ = ptr_to_offset(p); | ||
return *this; | ||
} | ||
offset_ptr& operator=(std::nullptr_t) { | ||
offset_ = NULLPTR_OFFSET; | ||
return *this; | ||
} | ||
|
||
offset_ptr(offset_ptr const& o) : offset_ptr{o.get()} {} | ||
offset_ptr(offset_ptr&& o) : offset_ptr{o.get()} {} | ||
offset_ptr& operator=(offset_ptr const& o) { | ||
offset_ = ptr_to_offset(o.get()); | ||
} | ||
offset_ptr& operator=(offset_ptr&& o) { offset_ = ptr_to_offset(o.get()); } | ||
|
||
offset_t ptr_to_offset(T const* p) const { | ||
return p == nullptr | ||
? NULLPTR_OFFSET | ||
: static_cast<offset_t>(reinterpret_cast<uintptr_t>(p) - | ||
reinterpret_cast<uintptr_t>(this)); | ||
} | ||
|
||
operator T*() { return get(); } | ||
operator T const*() const { return get(); } | ||
T& operator*() { return *get(); } | ||
T const& operator*() const { return *get(); } | ||
|
||
T* operator->() { return get(); } | ||
T const* operator->() const { return get(); } | ||
|
||
T const* get() const { | ||
auto const ptr = offset_ == NULLPTR_OFFSET | ||
? nullptr | ||
: reinterpret_cast<T const*>( | ||
reinterpret_cast<uintptr_t>(this) + offset_); | ||
return ptr; | ||
} | ||
T* get() { | ||
auto const ptr = | ||
offset_ == NULLPTR_OFFSET | ||
? nullptr | ||
: reinterpret_cast<T*>(reinterpret_cast<uintptr_t>(this) + offset_); | ||
return ptr; | ||
} | ||
|
||
template <typename Int> | ||
offset_ptr operator+(Int i) const { | ||
offset_ptr r = *this; | ||
r.offset_ += i * sizeof(T); | ||
return r; | ||
} | ||
|
||
friend bool operator==(std::nullptr_t, offset_ptr const& o) { | ||
return o.offset_ == NULLPTR_OFFSET; | ||
} | ||
friend bool operator==(offset_ptr const& o, std::nullptr_t) { | ||
return o.offset_ == NULLPTR_OFFSET; | ||
} | ||
friend bool operator!=(std::nullptr_t, offset_ptr const& o) { | ||
return o.offset_ != NULLPTR_OFFSET; | ||
} | ||
friend bool operator!=(offset_ptr const& o, std::nullptr_t) { | ||
return o.offset_ != NULLPTR_OFFSET; | ||
} | ||
|
||
offset_t offset_; | ||
}; | ||
|
||
} // namespace cista |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.