Skip to content

Commit 12a7f29

Browse files
committed
own
1 parent cc4cf4d commit 12a7f29

File tree

9 files changed

+812
-13
lines changed

9 files changed

+812
-13
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ add_library(subspace STATIC
7272
"num/__private/signed_integer_macros.h"
7373
"num/__private/unsigned_integer_macros.h"
7474
"num/float.h"
75+
"num/float_concepts.h"
7576
"num/fp_category.h"
7677
"num/signed_integer.h"
7778
"num/integer_concepts.h"
@@ -81,6 +82,7 @@ add_library(subspace STATIC
8182
"option/__private/storage.h"
8283
"option/option.h"
8384
"option/state.h"
85+
"ptr/own.h"
8486
"result/__private/is_result_type.h"
8587
"result/result.h"
8688
"tuple/__private/storage.h"
@@ -136,6 +138,7 @@ add_executable(subspace_unittests
136138
"num/usize_unittest.cc"
137139
"option/option_unittest.cc"
138140
"option/option_types_unittest.cc"
141+
"ptr/own_unittest.cc"
139142
"result/result_unittest.cc"
140143
"result/result_types_unittest.cc"
141144
"tuple/tuple_unittest.cc"

construct/make_default.h

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,17 @@ namespace sus::construct {
2121
namespace __private {
2222

2323
template <class T>
24-
constexpr inline bool has_with_default(...) {
25-
return false;
26-
}
27-
28-
template <class T, class... Args>
29-
requires(std::same_as<decltype(T::with_default(std::declval<Args>()...)), T>)
30-
constexpr inline bool has_with_default(int) {
31-
return true;
32-
}
24+
concept HasWithDefault = requires {
25+
{ T::with_default() } -> std::same_as<T>;
26+
};
3327

3428
} // namespace __private
3529

3630
// clang-format off
3731
template <class T>
3832
concept MakeDefault =
39-
(std::constructible_from<T> && !__private::has_with_default<T>(0)) ||
40-
(!std::constructible_from<T> && __private::has_with_default<T>(0));
33+
(std::constructible_from<T> && !__private::HasWithDefault<T>) ||
34+
(!std::constructible_from<T> && __private::HasWithDefault<T>);
4135
// clang-format on
4236

4337
template <MakeDefault T>
@@ -48,4 +42,13 @@ inline constexpr T make_default() noexcept {
4842
return T::with_default();
4943
}
5044

45+
template <MakeDefault T>
46+
requires(std::is_move_constructible_v<T>)
47+
inline T* alloc_make_default() noexcept {
48+
if constexpr (std::constructible_from<T>)
49+
return new T();
50+
else
51+
return new T(T::with_default());
52+
}
53+
5154
} // namespace sus::construct

num/float_concepts.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#pragma once
16+
17+
#include <concepts>
18+
19+
namespace sus::num {
20+
21+
struct f32;
22+
struct f64;
23+
24+
template <class T>
25+
concept FloatingPoint =
26+
std::same_as<f32, std::decay_t<T>> || std::same_as<f64, std::decay_t<T>>;
27+
28+
template <class T>
29+
concept PrimitiveFloatingPoint =
30+
std::same_as<float, T> ||
31+
std::same_as<double, T> || std::same_as<long double, T>;
32+
33+
} // namespace sus::num

num/unsigned_integer.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include <stdint.h>
1818

19+
#include "marker/unsafe.h"
1920
#include "num/__private/unsigned_integer_macros.h"
2021

2122
namespace sus::num {
@@ -98,6 +99,19 @@ struct usize final {
9899
check(val <= uint64_t{MAX_PRIMITIVE});
99100
}
100101
}
102+
103+
// Constructs a `usize` from a pointer type.
104+
template <class T>
105+
static inline usize from_ptr(::sus::marker::UnsafeFnMarker, T* t) {
106+
return reinterpret_cast<decltype(std::declval<usize>().primitive_value)>(t);
107+
}
108+
109+
// Converts a `usize` into a pointer type.
110+
template <class T>
111+
requires(std::is_pointer_v<T>)
112+
inline T to_ptr(::sus::marker::UnsafeFnMarker) const {
113+
return reinterpret_cast<T>(primitive_value);
114+
}
101115
};
102116

103117
} // namespace sus::num

option/option.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,10 @@ class Option final {
107107
///
108108
/// The Option's contained type `T` must be #MakeDefault, and will be
109109
/// constructed through that trait.
110-
static inline constexpr Option<T> with_default() noexcept
110+
static inline constexpr Option with_default() noexcept
111111
requires(::sus::construct::MakeDefault<T>)
112112
{
113-
return Option<T>(::sus::construct::make_default<T>());
113+
return Option(::sus::construct::make_default<T>());
114114
}
115115

116116
/// Destructor for the Option.

ptr/__private/in_use.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#pragma once
16+
17+
#include "macros/always_inline.h"
18+
#include "mem/relocate.h"
19+
#include "ptr/ptr_concepts.h"
20+
21+
namespace sus::ptr {
22+
template <class T>
23+
class [[sus_trivial_abi]] Own;
24+
}
25+
26+
namespace sus::ptr::__private {
27+
28+
template <class T>
29+
class InUse {
30+
public:
31+
constexpr sus_always_inline ~InUse() noexcept { own.t_ = &t; }
32+
33+
constexpr sus_always_inline const auto* operator->() const&& noexcept
34+
requires(HasArrow<T>)
35+
{
36+
return t.operator->();
37+
}
38+
constexpr sus_always_inline auto* operator->() && noexcept
39+
requires(HasArrowMut<T>)
40+
{
41+
return t.operator->();
42+
}
43+
44+
constexpr sus_always_inline const auto* operator->() const&& noexcept
45+
requires(!HasArrowMut<T>)
46+
{
47+
return &t;
48+
}
49+
constexpr sus_always_inline auto* operator->() && noexcept
50+
requires(!HasArrowMut<T>)
51+
{
52+
return &t;
53+
}
54+
55+
T& t;
56+
Own<T>& own;
57+
};
58+
59+
} // namespace sus::ptr::__private

0 commit comments

Comments
 (0)