@auto_js/utility 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/CMakeLists.txt +45 -0
  2. package/LICENSE +13 -0
  3. package/_module.cc +14 -0
  4. package/assertions.cc +32 -0
  5. package/container/mutable_priority_queue.cc +35 -0
  6. package/container/sealed_map.cc +121 -0
  7. package/container/segmented_priority_queue.cc +105 -0
  8. package/container/tinker_queue.cc +73 -0
  9. package/functional/bind.cc +151 -0
  10. package/functional/elide.cc +52 -0
  11. package/functional/flat_tuple.cc +90 -0
  12. package/functional/function_constant.cc +58 -0
  13. package/functional/function_ref.cc +68 -0
  14. package/functional/functional.cc +107 -0
  15. package/functional/regular_return.cc +55 -0
  16. package/include/auto_js/chunk_view.h +363 -0
  17. package/include/auto_js/no_unique_address.h +8 -0
  18. package/memory/autorelease_pool.cc +281 -0
  19. package/memory/comparator.cc +53 -0
  20. package/memory/memory.cc +64 -0
  21. package/memory/noinit_allocator.cc +64 -0
  22. package/meta/algorithm.cc +74 -0
  23. package/package.json +11 -0
  24. package/platform/lockable.cc +261 -0
  25. package/platform/shim/darwin.cc +8 -0
  26. package/platform/stop_token.cc +107 -0
  27. package/platform/timer.cc +175 -0
  28. package/platform/timer.h.cc +36 -0
  29. package/type_traits/function_traits.cc +173 -0
  30. package/type_traits/transform.cc +60 -0
  31. package/type_traits/type_of.cc +72 -0
  32. package/type_traits/type_pack.cc +116 -0
  33. package/type_traits/type_traits.cc +5 -0
  34. package/utility/constant_wrapper.cc +57 -0
  35. package/utility/covariant_value.cc +33 -0
  36. package/utility/facade.cc +126 -0
  37. package/utility/hash.cc +37 -0
  38. package/utility/ranges.cc +55 -0
  39. package/utility/string.cc +328 -0
  40. package/utility/utility.cc +150 -0
  41. package/utility/variant.cc +26 -0
@@ -0,0 +1,150 @@
1
+ module;
2
+ #include <concepts>
3
+ #include <stdexcept>
4
+ #include <string_view>
5
+ #include <utility>
6
+ export module util:utility;
7
+ export import :utility.constant_wrapper;
8
+ export import :utility.covariant_value;
9
+ export import :utility.facade;
10
+ export import :utility.hash;
11
+ export import :utility.ranges;
12
+ export import :utility.string;
13
+ // export import :utility.variant;
14
+ import :type_traits;
15
+
16
+ namespace util {
17
+
18
+ // `boost::noncopyable` actually prevents moving too
19
+ // NOLINTNEXTLINE(cppcoreguidelines-special-member-functions)
20
+ export class non_copyable {
21
+ protected:
22
+ non_copyable() = default;
23
+
24
+ public:
25
+ non_copyable(non_copyable&&) = default;
26
+ auto operator=(non_copyable&&) -> non_copyable& = default;
27
+ };
28
+
29
+ // NOLINTNEXTLINE(cppcoreguidelines-special-member-functions)
30
+ export class non_moveable {
31
+ protected:
32
+ non_moveable() = default;
33
+
34
+ public:
35
+ non_moveable(const non_moveable&) = delete;
36
+ auto operator=(const non_moveable&) = delete;
37
+ };
38
+
39
+ // Return a sequence of index constants
40
+ export template <std::size_t Size>
41
+ constexpr auto sequence = []() consteval -> auto {
42
+ // With C++26 P2686 we can do constexpr decomposition. So instead of the `tuple` of
43
+ // `integral_constants` it can be an array of `size_t`.
44
+ // https://clang.llvm.org/cxx_status.html
45
+
46
+ // std::array<std::size_t, Size> result{};
47
+ // std::ranges::copy(std::ranges::iota_view{std::size_t{0}, Size}, result.begin());
48
+ // return result;
49
+
50
+ return []<std::size_t... Index>(std::index_sequence<Index...> /*sequence*/) consteval {
51
+ return std::tuple{std::integral_constant<std::size_t, Index>{}...};
52
+ }(std::make_index_sequence<Size>());
53
+ }();
54
+
55
+ // Checked container / range access for types which don't have `.at()`
56
+ export auto at(auto&& range, std::size_t index) -> decltype(auto) {
57
+ if (range.size() <= index) {
58
+ throw std::out_of_range{"Index out of range"};
59
+ }
60
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-avoid-unchecked-container-access,cppcoreguidelines-pro-bounds-constant-array-index)
61
+ return std::forward<decltype(range)>(range)[ index ];
62
+ }
63
+
64
+ // Allocates storage for a copy of a constant expression value and initializes it with the value.
65
+ export template <const auto& Value, class Type = std::remove_cvref_t<decltype(Value)>>
66
+ struct copy_of : Type {
67
+ constexpr copy_of() :
68
+ Type(Value) {}
69
+ };
70
+
71
+ // Remove an element from a (probably) `std::vector` by swap and popping
72
+ export auto swap_and_pop(auto& container, auto iterator) -> void {
73
+ if (std::next(iterator) != container.end()) {
74
+ std::swap(*iterator, container.back());
75
+ }
76
+ container.pop_back();
77
+ }
78
+
79
+ // Deref'able holder which always contains a value
80
+ export template <class Type>
81
+ class just : public pointer_facade {
82
+ public:
83
+ explicit constexpr just(const Type& value) : value_{value} {}
84
+ explicit constexpr just(Type&& value) : value_{std::move(value)} {}
85
+ explicit constexpr operator bool() const { return true; }
86
+ constexpr auto operator*(this auto&& self) { return std::forward<decltype(self)>(self).value_; }
87
+
88
+ private:
89
+ Type value_;
90
+ };
91
+
92
+ export template <class Type>
93
+ class just<Type&> : public pointer_facade {
94
+ public:
95
+ explicit constexpr just(Type& value) : value_{&value} {}
96
+ explicit constexpr operator bool() const { return true; }
97
+ constexpr auto operator*() const -> Type& { return *value_; }
98
+
99
+ private:
100
+ Type* value_;
101
+ };
102
+
103
+ // Like `just<T>` but never contains a value
104
+ export template <class Type>
105
+ class nothing : public pointer_facade {
106
+ public:
107
+ explicit constexpr operator bool() const { return false; }
108
+ constexpr auto operator*() const -> Type { std::unreachable(); }
109
+ };
110
+
111
+ // https://en.cppreference.com/w/cpp/experimental/scope_exit
112
+ export template <class Invoke>
113
+ class scope_exit : non_copyable {
114
+ public:
115
+ explicit scope_exit(Invoke invoke) :
116
+ invoke_{std::move(invoke)} {}
117
+ scope_exit(const scope_exit&) = delete;
118
+ ~scope_exit() { invoke_(); }
119
+ auto operator=(const scope_exit&) -> scope_exit& = delete;
120
+
121
+ private:
122
+ Invoke invoke_;
123
+ };
124
+
125
+ // Explicitly annotate desired struct slicing: cppcoreguidelines-slicing
126
+ template <class Type>
127
+ struct slice_t;
128
+
129
+ template <class Type>
130
+ struct slice_t<const Type> {
131
+ public:
132
+ explicit slice_t(const Type& value) : value_{value} {}
133
+
134
+ template <class As>
135
+ // NOLINTNEXTLINE(google-explicit-constructor)
136
+ operator const As&() &&
137
+ requires(std::convertible_to<Type&, As&> && type<Type> != type<As>) {
138
+ return static_cast<const As&>(value_.get());
139
+ }
140
+
141
+ private:
142
+ std::reference_wrapper<const Type> value_;
143
+ };
144
+
145
+ export template <class Type>
146
+ constexpr auto slice(Type& value) {
147
+ return slice_t<const Type>{value};
148
+ }
149
+
150
+ } // namespace util
@@ -0,0 +1,26 @@
1
+ module;
2
+ #include <utility>
3
+ #include <variant>
4
+ export module util:utility.variant;
5
+ import :meta.algorithm;
6
+ import :type_traits;
7
+
8
+ namespace util {
9
+
10
+ // Apply a visitor to a variant and map its result to a new variant type. Repeated types are collapsed.
11
+ export auto map_variant(auto&& variant, auto visit) {
12
+ constexpr auto result_from = [](auto alternative) {
13
+ return type<decltype(visit)>(type<util::apply_cvref_t<decltype(variant), type_t<alternative>>>);
14
+ };
15
+ const auto [... alternative_types ] = util::make_type_pack(util::remove_cvref(type<decltype(variant)>));
16
+ const auto [... result_types ] = util::pack_unique(result_from(alternative_types)...);
17
+ using result_type = std::variant<type_t<result_types>...>;
18
+ return std::visit(
19
+ [ & ](auto&& value) -> result_type {
20
+ return result_type{visit(std::forward<decltype(value)>(value))};
21
+ },
22
+ std::forward<decltype(variant)>(variant)
23
+ );
24
+ }
25
+
26
+ } // namespace util