Skip to content

Commit

Permalink
Switch rank structs to be consistent with written guidance in go/rank…
Browse files Browse the repository at this point in the history
…ed-overloads

PiperOrigin-RevId: 605125821
Change-Id: I2ee260eaf283acafd80abfd2b7419a0e9f597a78
  • Loading branch information
fowles authored and copybara-github committed Feb 8, 2024
1 parent 0be9f99 commit 99f0b6d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
19 changes: 10 additions & 9 deletions absl/container/internal/common_policy_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ struct common_policy_traits {
// UNINITIALIZED
template <class Alloc>
static void transfer(Alloc* alloc, slot_type* new_slot, slot_type* old_slot) {
transfer_impl(alloc, new_slot, old_slot, Rank0{});
transfer_impl(alloc, new_slot, old_slot, Rank2{});
}

// PRECONDITION: `slot` is INITIALIZED
Expand All @@ -83,7 +83,7 @@ struct common_policy_traits {

static constexpr bool transfer_uses_memcpy() {
return std::is_same<decltype(transfer_impl<std::allocator<char>>(
nullptr, nullptr, nullptr, Rank0{})),
nullptr, nullptr, nullptr, Rank2{})),
std::true_type>::value;
}

Expand All @@ -95,18 +95,19 @@ struct common_policy_traits {
}

private:
// To rank the overloads below for overload resolution. Rank0 is preferred.
struct Rank2 {};
struct Rank1 : Rank2 {};
struct Rank0 : Rank1 {};
// Use go/ranked-overloads for dispatching.
struct Rank0 {};
struct Rank1 : Rank0 {};
struct Rank2 : Rank1 {};

// Use auto -> decltype as an enabler.
// P::transfer returns std::true_type if transfer uses memcpy (e.g. in
// node_slot_policy).
template <class Alloc, class P = Policy>
static auto transfer_impl(Alloc* alloc, slot_type* new_slot,
slot_type* old_slot, Rank0)
-> decltype(P::transfer(alloc, new_slot, old_slot)) {
slot_type* old_slot,
Rank2) -> decltype(P::transfer(alloc, new_slot,
old_slot)) {
return P::transfer(alloc, new_slot, old_slot);
}
#if defined(__cpp_lib_launder) && __cpp_lib_launder >= 201606
Expand All @@ -129,7 +130,7 @@ struct common_policy_traits {

template <class Alloc>
static void transfer_impl(Alloc* alloc, slot_type* new_slot,
slot_type* old_slot, Rank2) {
slot_type* old_slot, Rank0) {
construct(alloc, new_slot, std::move(element(old_slot)));
destroy(alloc, old_slot);
}
Expand Down
2 changes: 1 addition & 1 deletion absl/strings/cord.h
Original file line number Diff line number Diff line change
Expand Up @@ -1120,7 +1120,7 @@ Cord MakeCordFromExternal(absl::string_view data, Releaser&& releaser) {
} else {
using ReleaserType = absl::decay_t<Releaser>;
cord_internal::InvokeReleaser(
cord_internal::Rank0{}, ReleaserType(std::forward<Releaser>(releaser)),
cord_internal::Rank1{}, ReleaserType(std::forward<Releaser>(releaser)),
data);
}
return cord;
Expand Down
11 changes: 6 additions & 5 deletions absl/strings/internal/cord_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -352,18 +352,19 @@ struct CordRepExternal : public CordRep {
static void Delete(CordRep* rep);
};

struct Rank1 {};
struct Rank0 : Rank1 {};
// Use go/ranked-overloads for dispatching.
struct Rank0 {};
struct Rank1 : Rank0 {};

template <typename Releaser, typename = ::absl::base_internal::invoke_result_t<
Releaser, absl::string_view>>
void InvokeReleaser(Rank0, Releaser&& releaser, absl::string_view data) {
void InvokeReleaser(Rank1, Releaser&& releaser, absl::string_view data) {
::absl::base_internal::invoke(std::forward<Releaser>(releaser), data);
}

template <typename Releaser,
typename = ::absl::base_internal::invoke_result_t<Releaser>>
void InvokeReleaser(Rank1, Releaser&& releaser, absl::string_view) {
void InvokeReleaser(Rank0, Releaser&& releaser, absl::string_view) {
::absl::base_internal::invoke(std::forward<Releaser>(releaser));
}

Expand All @@ -381,7 +382,7 @@ struct CordRepExternalImpl
}

~CordRepExternalImpl() {
InvokeReleaser(Rank0{}, std::move(this->template get<0>()),
InvokeReleaser(Rank1{}, std::move(this->template get<0>()),
absl::string_view(base, length));
}

Expand Down

0 comments on commit 99f0b6d

Please sign in to comment.