1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_VOTING_OPTIONAL_VOTING_CHANNEL_H_
#define COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_VOTING_OPTIONAL_VOTING_CHANNEL_H_
#include <map>
#include <optional>
#include <utility>
#include "base/check.h"
#include "components/performance_manager/public/voting/voting.h"
namespace performance_manager::voting {
// A voting channel that accepts std::nullopt votes. Those votes are not
// upstreamed. This voting channel is always valid unless moved from.
template <class VoteImpl>
class OptionalVotingChannel {
public:
using ContextType = typename VoteImpl::ContextType;
using VoterId = voting::VoterId<VoteImpl>;
using VotingChannel = voting::VotingChannel<VoteImpl>;
using VoteType = VoteImpl::VoteType;
OptionalVotingChannel();
explicit OptionalVotingChannel(VotingChannel upstream_voting_channel);
~OptionalVotingChannel();
OptionalVotingChannel(const OptionalVotingChannel&) = delete;
OptionalVotingChannel& operator=(const OptionalVotingChannel&) = delete;
OptionalVotingChannel(OptionalVotingChannel&&);
OptionalVotingChannel& operator=(OptionalVotingChannel&&);
// Submits a vote through this voting channel that is only upstreamed if it is
// not nullopt. Can only be called if this VotingChannel is valid.
void SubmitVote(const ContextType* context,
const std::optional<VoteImpl>& vote);
// Modifies an existing vote. This can either submit, change or invalidate the
// vote to the upstream voting channel depending on the optional value of the
// previous vote and the new vote. Can only be called if this VotingChannel is
// valid.
void ChangeVote(const ContextType* context,
const std::optional<VoteImpl>& vote);
// Invalidates an existing vote. Can only be called if this VotingChannel is
// valid.
void InvalidateVote(const ContextType* context);
// Returns true if this VotingChannel is valid.
bool IsValid() const;
// Resets this voting channel.
void Reset();
VoterId voter_id() const { return upstream_voting_channel_.voter_id(); }
private:
VotingChannel upstream_voting_channel_;
std::map<const ContextType*, std::optional<VoteImpl>> votes_;
};
template <class VoteImpl>
OptionalVotingChannel<VoteImpl>::OptionalVotingChannel() = default;
template <class VoteImpl>
OptionalVotingChannel<VoteImpl>::OptionalVotingChannel(
VotingChannel upstream_voting_channel)
: upstream_voting_channel_(std::move(upstream_voting_channel)) {
CHECK(upstream_voting_channel_.IsValid());
}
template <class VoteImpl>
OptionalVotingChannel<VoteImpl>::~OptionalVotingChannel() {
CHECK(votes_.empty());
}
template <class VoteImpl>
OptionalVotingChannel<VoteImpl>::OptionalVotingChannel(
OptionalVotingChannel&& other) = default;
template <class VoteImpl>
OptionalVotingChannel<VoteImpl>& OptionalVotingChannel<VoteImpl>::operator=(
OptionalVotingChannel&& other) = default;
template <class VoteImpl>
void OptionalVotingChannel<VoteImpl>::SubmitVote(
const ContextType* context,
const std::optional<VoteImpl>& vote) {
const auto [_, inserted] = votes_.try_emplace(context, vote);
CHECK(inserted);
if (!vote.has_value()) {
return;
}
upstream_voting_channel_.SubmitVote(context, *vote);
}
template <class VoteImpl>
void OptionalVotingChannel<VoteImpl>::ChangeVote(
const ContextType* context,
const std::optional<VoteImpl>& vote) {
auto it = votes_.find(context);
CHECK(it != votes_.end());
std::optional<VoteImpl> old_vote = it->second;
// Nothing to do if the vote did not change.
if (old_vote == vote) {
return;
}
it->second = vote;
if (old_vote.has_value() && vote.has_value()) {
upstream_voting_channel_.ChangeVote(context, *vote);
} else if (old_vote.has_value()) {
upstream_voting_channel_.InvalidateVote(context);
} else if (vote.has_value()) {
upstream_voting_channel_.SubmitVote(context, *vote);
}
}
template <class VoteImpl>
void OptionalVotingChannel<VoteImpl>::InvalidateVote(
const ContextType* context) {
auto it = votes_.find(context);
CHECK(it != votes_.end());
const bool had_value = it->second.has_value();
votes_.erase(it);
if (had_value) {
upstream_voting_channel_.InvalidateVote(context);
}
}
template <class VoteImpl>
bool OptionalVotingChannel<VoteImpl>::IsValid() const {
return upstream_voting_channel_.IsValid();
}
template <class VoteImpl>
void OptionalVotingChannel<VoteImpl>::Reset() {
upstream_voting_channel_.Reset();
}
} // namespace performance_manager::voting
#endif // COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_VOTING_OPTIONAL_VOTING_CHANNEL_H_
|