-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
246 additions
and
17 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 |
---|---|---|
@@ -1,23 +1,18 @@ | ||
#include <future> | ||
#include <iostream> | ||
|
||
class C | ||
int | ||
small_comp(const int i) | ||
{ | ||
public: | ||
void foo() const& { std::cout << "foo() const&\n"; } | ||
void foo() && { std::cout << "foo() &&\n"; } | ||
void foo() & { std::cout << "foo() &\n"; } | ||
void foo() const&& { std::cout << "foo() const&&\n"; } | ||
}; | ||
std::this_thread::sleep_for(std::chrono::seconds(2)); | ||
return i * 2; | ||
} | ||
|
||
int | ||
main() | ||
{ | ||
C x; | ||
x.foo(); // calls foo() & | ||
C{}.foo(); // calls foo() && | ||
std::move(x).foo(); // calls foo() && | ||
|
||
const C cx; | ||
cx.foo(); // calls foo() const& | ||
std::move(cx).foo(); // calls foo() const&& | ||
std::future<int> theAnswer = std::async(small_comp, 10); | ||
std::cout << "Here we are after starting that task.\n"; | ||
std::cout << "And we finished now: " << theAnswer.get() << '\n'; | ||
return 0; | ||
} |
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,91 @@ | ||
#pragma once | ||
#include <cstddef> | ||
#include <new> | ||
#include <utility> | ||
|
||
namespace hage { | ||
|
||
namespace details { | ||
|
||
template<std::size_t ExpectedSize, std::size_t ActualSize, std::size_t ExpectedAlignment, std::size_t ActualAlignment> | ||
constexpr inline void | ||
compare_size() | ||
{ | ||
static_assert(ExpectedSize == ActualSize, "The size for the ForwardDeclareStorage is wrong"); | ||
static_assert(ExpectedAlignment == ActualAlignment, "The alignment for the ForwardDeclareStorage is wrong"); | ||
} | ||
|
||
} // namespace details | ||
|
||
// Inspired by: | ||
// https://probablydance.com/2013/10/05/type-safe-pimpl-implementation-without-overhead/ | ||
|
||
template<typename T, std::size_t Size, std::size_t Alignment> | ||
class ForwardDeclaredStorage | ||
{ | ||
private: | ||
alignas(Alignment) std::byte m_data[Size]; | ||
|
||
constexpr T& get() { return *std::launder(reinterpret_cast<T*>(&m_data)); } | ||
constexpr const T& get() const { return *std::launder(reinterpret_cast<const T*>(&m_data)); } | ||
|
||
public: | ||
constexpr ForwardDeclaredStorage() { ::new (m_data) T; } | ||
|
||
constexpr ~ForwardDeclaredStorage() | ||
{ | ||
details::compare_size<Size, sizeof(T), Alignment, alignof(T)>(); | ||
get().~T(); | ||
} | ||
|
||
template<typename... Args> | ||
constexpr explicit ForwardDeclaredStorage(std::in_place_t, Args&&... args) | ||
{ | ||
::new (m_data) T(std::forward<Args>(args)...); | ||
} | ||
|
||
constexpr ForwardDeclaredStorage(ForwardDeclaredStorage&& other) noexcept(std::is_nothrow_move_constructible_v<T>) | ||
{ | ||
::new (m_data) T(std::move(other.get())); | ||
} | ||
constexpr ForwardDeclaredStorage(const ForwardDeclaredStorage& other) { ::new (m_data) T(other.get()); } | ||
|
||
constexpr ForwardDeclaredStorage(const T& other) { ::new (m_data) T(other); } | ||
constexpr explicit ForwardDeclaredStorage(T&& other) noexcept(std::is_nothrow_move_constructible_v<T>) | ||
{ | ||
::new (m_data) T(std::move(other)); | ||
} | ||
|
||
constexpr ForwardDeclaredStorage& operator=(const ForwardDeclaredStorage& other) | ||
{ | ||
get() = other.get(); | ||
return *this; | ||
} | ||
|
||
constexpr ForwardDeclaredStorage& operator=(const T& other) | ||
{ | ||
get() = other; | ||
return *this; | ||
} | ||
|
||
constexpr ForwardDeclaredStorage& operator=(T&& other) noexcept(std::is_nothrow_move_assignable_v<T>) | ||
{ | ||
get() = std::move(other); | ||
return *this; | ||
} | ||
|
||
constexpr ForwardDeclaredStorage& operator=(ForwardDeclaredStorage&& other) noexcept( | ||
std::is_nothrow_move_assignable_v<T>) | ||
{ | ||
get() = std::move(other.get()); | ||
return *this; | ||
} | ||
|
||
constexpr T* operator->() { return &get(); } | ||
constexpr const T* operator->() const { return &get(); }; | ||
|
||
constexpr T& operator*() { return get(); } | ||
constexpr const T& operator*() const { return get(); } | ||
}; | ||
|
||
} // namespace hage |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// | ||
// Created by rhermes on 2024-06-08. | ||
// | ||
|
||
#include "fast_pimpl_test.hpp" | ||
|
||
#include <string> | ||
|
||
using namespace hage::test; | ||
|
||
namespace hage::test::details { | ||
|
||
struct FastPimplTestImpl | ||
{ | ||
int a{ 2 }; | ||
std::string b{ "this is the default string" }; | ||
|
||
void setA(int a) { this->a = a; } | ||
|
||
int getA() { return this->a; } | ||
|
||
void setB(std::string b) { this->b = std::move(b); } | ||
std::string getB() { return this->b; } | ||
}; | ||
|
||
} // namespace hage::test::details | ||
FastPimplTest::FastPimplTest() = default; | ||
FastPimplTest::~FastPimplTest() = default; | ||
FastPimplTest::FastPimplTest(const FastPimplTest&) = default; | ||
FastPimplTest& | ||
FastPimplTest::operator=(const FastPimplTest&) = default; | ||
FastPimplTest::FastPimplTest(FastPimplTest&&) = default; | ||
FastPimplTest& | ||
FastPimplTest::operator=(FastPimplTest&&) = default; | ||
|
||
void | ||
FastPimplTest::setA(int a) | ||
{ | ||
m_impl->setA(a); | ||
} | ||
|
||
int | ||
FastPimplTest::getA() | ||
{ | ||
return m_impl->getA(); | ||
} | ||
|
||
void | ||
FastPimplTest::setB(std::string b) | ||
{ | ||
return m_impl->setB(std::move(b)); | ||
} | ||
|
||
std::string | ||
FastPimplTest::getB() | ||
{ | ||
return m_impl->getB(); | ||
} |
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,32 @@ | ||
#pragma once | ||
|
||
#include <hage/core/forward_declared_storage.hpp> | ||
#include <string> | ||
|
||
namespace hage::test { | ||
namespace details { | ||
struct FastPimplTestImpl; | ||
} | ||
|
||
class FastPimplTest | ||
{ | ||
private: | ||
hage::ForwardDeclaredStorage<details::FastPimplTestImpl, 40, 8> m_impl; | ||
|
||
public: | ||
FastPimplTest(); | ||
~FastPimplTest(); | ||
|
||
FastPimplTest(const FastPimplTest&); | ||
FastPimplTest& operator=(const FastPimplTest&); | ||
FastPimplTest(FastPimplTest&&); | ||
FastPimplTest& operator=(FastPimplTest&&); | ||
|
||
void setA(int a); | ||
int getA(); | ||
|
||
void setB(std::string b); | ||
std::string getB(); | ||
}; | ||
|
||
} // namespace hage::test |
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,43 @@ | ||
#include <doctest/doctest.h> | ||
|
||
#include "fast_pimpl_test.hpp" | ||
|
||
using namespace hage; | ||
|
||
TEST_SUITE_BEGIN("forward_declared_storage"); | ||
|
||
TEST_CASE("Does move work") | ||
{ | ||
test::FastPimplTest hey; | ||
|
||
hey.setB("pirate!"); | ||
REQUIRE_EQ(hey.getB(), "pirate!"); | ||
|
||
hey.setA(10); | ||
|
||
SUBCASE("Copy assignment") | ||
{ | ||
test::FastPimplTest hello; | ||
|
||
hello = hey; | ||
REQUIRE_EQ(hey.getB(), "pirate!"); | ||
REQUIRE_EQ(hello.getB(), "pirate!"); | ||
} | ||
|
||
SUBCASE("Move constructor") | ||
{ | ||
test::FastPimplTest hello = std::move(hey); | ||
REQUIRE_UNARY(hey.getB().empty()); | ||
REQUIRE_EQ(hello.getB(), "pirate!"); | ||
} | ||
|
||
SUBCASE("Move assignment") | ||
{ | ||
test::FastPimplTest hello; | ||
hello = std::move(hey); | ||
REQUIRE_UNARY(hey.getB().empty()); | ||
REQUIRE_EQ(hello.getB(), "pirate!"); | ||
} | ||
} | ||
|
||
TEST_SUITE_END(); |