Skip to content

Commit

Permalink
feat: Add an exception-less factory type and concept
Browse files Browse the repository at this point in the history
  • Loading branch information
BurningEnlightenment committed Aug 1, 2023
1 parent 9b5b29f commit 3329a60
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions sources.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ dplx_target_sources(concrete
cncr/type_utils
cncr/utils
cncr/uuid
make
overloaded
scope_guard
)
Expand Down
36 changes: 36 additions & 0 deletions src/dplx/make.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

// Copyright 2022-2023 Henrik Steffen Gaßmann
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE or copy at
// https://www.boost.org/LICENSE_1_0.txt)

#pragma once

#include <concepts>
#include <type_traits>

#include <dplx/cncr/disappointment.hpp>

namespace dplx
{

template <typename T>
struct make
{
};

// clang-format off
template <typename T>
concept makable
= requires {
typename T;
typename make<T>;
}
&& std::movable<T>
&& requires(make<T> const maker) {
{ maker() } noexcept -> cncr::tryable_result<T>;
};
// clang-format on

} // namespace dplx
43 changes: 43 additions & 0 deletions src/dplx/make.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

// Copyright 2022-2023 Henrik Steffen Gaßmann
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE or copy at
// https://www.boost.org/LICENSE_1_0.txt)

#include "dplx/make.hpp"

#include "test_utils.hpp"

namespace cncr_tests
{

class myhandle
{
private:
constexpr myhandle() noexcept = default;

public:
static auto my() noexcept -> result<myhandle>
{
return myhandle{};
}
};

} // namespace cncr_tests

template <>
struct dplx::make<cncr_tests::myhandle>
{
auto operator()() const noexcept -> result<cncr_tests::myhandle>
{
return cncr_tests::myhandle::my();
}
};

namespace cncr_tests
{

static_assert(makable<myhandle>);

}

0 comments on commit 3329a60

Please sign in to comment.