Skip to content

Commit

Permalink
Use absl::monostate where applicable.
Browse files Browse the repository at this point in the history
Bug: none
Change-Id: I0be4990e0a5d5fb4ff00ac828cea995a7562c7ce
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3608889
Reviewed-by: Jamie Walch <jamiewalch@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Martin Robinson <mrobinson@igalia.com>
Commit-Queue: Peter Kasting <pkasting@chromium.org>
Cr-Commit-Position: refs/heads/main@{#996615}
  • Loading branch information
pkasting authored and Chromium LUCI CQ committed Apr 27, 2022
1 parent 4e4b2da commit 41b5a8f
Show file tree
Hide file tree
Showing 14 changed files with 37 additions and 53 deletions.
14 changes: 14 additions & 0 deletions ipc/ipc_message_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -982,6 +982,8 @@ struct ParamTraits<std::unique_ptr<P>> {
}
};

// absl types ParamTraits

template <class P>
struct ParamTraits<absl::optional<P>> {
typedef absl::optional<P> param_type;
Expand Down Expand Up @@ -1013,6 +1015,18 @@ struct ParamTraits<absl::optional<P>> {
}
};

template <>
struct ParamTraits<absl::monostate> {
typedef absl::monostate param_type;
static void Write(base::Pickle* m, const param_type& p) {}
static bool Read(const base::Pickle* m,
base::PickleIterator* iter,
param_type* r) {
return true;
}
static void Log(const param_type& p, std::string* l) { l->append("()"); }
};

// base/util types ParamTraits

template <typename TypeMarker, typename WrappedType, WrappedType kInvalidValue>
Expand Down
17 changes: 8 additions & 9 deletions remoting/base/result.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include "base/check.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "third_party/abseil-cpp/absl/types/variant.h"

// Result<SuccessType, ErrorType> represents the success or failure of an
// operation, along with either the success value or error details.
Expand Down Expand Up @@ -127,18 +128,16 @@ namespace remoting {
// state or error state, respectively.
class SuccessTag {};
class ErrorTag {};
// Monostate can be used for SuccessType or ErrorType to indicate that there is
// no data for that state. Thus, Result<SomeType, Monostate> is somewhat
// analogous to absl::optional<SomeType>, and Result<Monostate, Monostate> is
// effectively a (2-byte) boolean. Result<Monostate, ErrorType> can be useful
// for cases where an operation can fail, but there is no return value in the
// success case.
// TODO(rkjnsn): Replace with std::monostate once C++17 is allowed.
class Monostate {};
// absl::Monostate can be used for SuccessType or ErrorType to indicate that
// there is no data for that state. Thus, Result<SomeType, Monostate> is
// somewhat analogous to absl::optional<SomeType>, and Result<Monostate,
// Monostate> is effectively a (2-byte) boolean. Result<Monostate, ErrorType>
// can be useful for cases where an operation can fail, but there is no return
// value in the success case.

constexpr SuccessTag kSuccessTag = SuccessTag();
constexpr ErrorTag kErrorTag = ErrorTag();
constexpr Monostate kMonostate = Monostate();
constexpr absl::monostate kMonostate = absl::monostate();

namespace internal {

Expand Down
2 changes: 1 addition & 1 deletion remoting/host/chromoting_messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ IPC_MESSAGE_CONTROL(ChromotingDesktopNetworkMsg_KeyboardChanged,
IPC_MESSAGE_CONTROL(
ChromotingDesktopNetworkMsg_FileResult,
uint64_t /* file_id */,
remoting::protocol::FileTransferResult<remoting::Monostate> /* result */)
remoting::protocol::FileTransferResult<absl::monostate> /* result */)

// Carries the result of a read-file operation on the file identified by
// |file_id|. |result| is the filename and size of the selected file. If
Expand Down
18 changes: 0 additions & 18 deletions remoting/host/chromoting_param_traits.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,22 +79,4 @@ void ParamTraits<remoting::protocol::FileTransfer_Error>::Log(
base::StringPrintf("FileTransfer Error: %s", formatted.str().c_str()));
}

// remoting::Monostate

// static
void IPC::ParamTraits<remoting::Monostate>::Write(base::Pickle*,
const param_type&) {}

// static
bool ParamTraits<remoting::Monostate>::Read(const base::Pickle*,
base::PickleIterator*,
param_type*) {
return true;
}

// static
void ParamTraits<remoting::Monostate>::Log(const param_type&, std::string* l) {
l->append("()");
}

} // namespace IPC
10 changes: 0 additions & 10 deletions remoting/host/chromoting_param_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,6 @@ struct ParamTraits<remoting::protocol::FileTransfer_Error> {
static void Log(const param_type& p, std::string* l);
};

template <>
struct ParamTraits<remoting::Monostate> {
typedef remoting::Monostate param_type;
static void Write(base::Pickle* m, const param_type& p);
static bool Read(const base::Pickle* m,
base::PickleIterator* iter,
param_type* p);
static void Log(const param_type& p, std::string* l);
};

template <typename SuccessType, typename ErrorType>
struct ParamTraits<remoting::Result<SuccessType, ErrorType>> {
typedef remoting::Result<SuccessType, ErrorType> param_type;
Expand Down
2 changes: 1 addition & 1 deletion remoting/host/file_transfer/ensure_user.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace remoting {
// user is on the log-in screen, an error of type NOT_LOGGED_IN will be
// returned. If something else goes wrong, the error type will be
// UNEXPECTED_ERROR.
protocol::FileTransferResult<Monostate> EnsureUserContext();
protocol::FileTransferResult<absl::monostate> EnsureUserContext();

} // namespace remoting

Expand Down
2 changes: 1 addition & 1 deletion remoting/host/file_transfer/ensure_user_mac.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace remoting {

protocol::FileTransferResult<Monostate> EnsureUserContext() {
protocol::FileTransferResult<absl::monostate> EnsureUserContext() {
// Make sure we're not on the log-in screen.
if (getuid() == 0) {
LOG(ERROR) << "Cannot transfer files on log-in screen.";
Expand Down
2 changes: 1 addition & 1 deletion remoting/host/file_transfer/ensure_user_no_op.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace remoting {

protocol::FileTransferResult<Monostate> EnsureUserContext() {
protocol::FileTransferResult<absl::monostate> EnsureUserContext() {
return kSuccessTag;
}

Expand Down
2 changes: 1 addition & 1 deletion remoting/host/file_transfer/ensure_user_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace remoting {

protocol::FileTransferResult<Monostate> EnsureUserContext() {
protocol::FileTransferResult<absl::monostate> EnsureUserContext() {
// Impersonate the currently logged-in user, or fail if there is none.
HANDLE user_token = nullptr;
if (!WTSQueryUserToken(WTS_CURRENT_SESSION, &user_token)) {
Expand Down
6 changes: 3 additions & 3 deletions remoting/host/file_transfer/file_chooser_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class FileChooserWindows : public FileChooser,
void OnObjectSignaled(HANDLE object) override;

private:
FileTransferResult<Monostate> LaunchChooserProcess();
FileTransferResult<absl::monostate> LaunchChooserProcess();

ResultCallback callback_;
base::Process process_;
Expand All @@ -126,7 +126,7 @@ FileChooserWindows::FileChooserWindows(
: callback_(std::move(callback)) {}

void FileChooserWindows::Show() {
FileTransferResult<Monostate> result = LaunchChooserProcess();
FileTransferResult<absl::monostate> result = LaunchChooserProcess();

if (!result) {
base::SequencedTaskRunnerHandle::Get()->PostTask(
Expand Down Expand Up @@ -180,7 +180,7 @@ void FileChooserWindows::OnObjectSignaled(HANDLE object) {
std::move(callback_).Run(std::move(result));
}

FileTransferResult<Monostate> FileChooserWindows::LaunchChooserProcess() {
FileTransferResult<absl::monostate> FileChooserWindows::LaunchChooserProcess() {
base::LaunchOptions launch_options;

FileTransferResult<ScopedHandle> current_user =
Expand Down
4 changes: 2 additions & 2 deletions remoting/host/file_transfer/file_operations.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class FileOperations {

class Reader {
public:
using OpenResult = protocol::FileTransferResult<Monostate>;
using OpenResult = protocol::FileTransferResult<absl::monostate>;
using OpenCallback = base::OnceCallback<void(OpenResult result)>;

// On success, |result| will contain the read data, or an empty vector on
Expand All @@ -71,7 +71,7 @@ class FileOperations {

class Writer {
public:
using Result = protocol::FileTransferResult<Monostate>;
using Result = protocol::FileTransferResult<absl::monostate>;
using Callback = base::OnceCallback<void(Result result)>;

// Destructing before the file is completely written and closed will
Expand Down
2 changes: 1 addition & 1 deletion remoting/host/file_transfer/ipc_file_operations.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class IpcFileOperations : public FileOperations {
// Handles responses to file operations requests.
class ResultHandler {
public:
using Result = protocol::FileTransferResult<Monostate>;
using Result = protocol::FileTransferResult<absl::monostate>;
using InfoResult =
protocol::FileTransferResult<std::tuple<base::FilePath, uint64_t>>;
using DataResult =
Expand Down
6 changes: 3 additions & 3 deletions remoting/host/file_transfer/local_file_operations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class LocalFileReader : public FileOperations::Reader {

private:
void OnEnsureUserResult(OpenCallback callback,
protocol::FileTransferResult<Monostate> result);
protocol::FileTransferResult<absl::monostate> result);
void OnFileChooserResult(OpenCallback callback, FileChooser::Result result);
void OnOpenResult(OpenCallback callback, base::File::Error error);
void OnGetInfoResult(OpenCallback callback,
Expand Down Expand Up @@ -196,7 +196,7 @@ FileOperations::State LocalFileReader::state() const {

void LocalFileReader::OnEnsureUserResult(
FileOperations::Reader::OpenCallback callback,
protocol::FileTransferResult<Monostate> result) {
protocol::FileTransferResult<absl::monostate> result) {
if (!result) {
SetState(FileOperations::kFailed);
std::move(callback).Run(std::move(result.error()));
Expand Down Expand Up @@ -313,7 +313,7 @@ void LocalFileWriter::Open(const base::FilePath& filename, Callback callback) {
base::PostTaskAndReplyWithResult(
file_task_runner_.get(), FROM_HERE, base::BindOnce([] {
return EnsureUserContext().AndThen(
[](Monostate) { return GetDesktopDirectory(); });
[](absl::monostate) { return GetDesktopDirectory(); });
}),
base::BindOnce(&LocalFileWriter::OnGetTargetDirectoryResult,
weak_ptr_factory_.GetWeakPtr(), filename,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#define UI_ACCESSIBILITY_PLATFORM_INSPECT_AX_CALL_STATEMENT_INVOKER_AURALINUX_H_

#include <atspi/atspi.h>
#include <variant>

#include "base/memory/raw_ptr.h"
#include "third_party/abseil-cpp/absl/types/variant.h"
Expand All @@ -19,7 +18,7 @@ namespace ui {
class AXPropertyNode;

using Target =
absl::variant<std::monostate, std::string, int, const AtspiAccessible*>;
absl::variant<absl::monostate, std::string, int, const AtspiAccessible*>;

// Optional tri-state object.
using AXOptionalObject = ui::AXOptional<Target>;
Expand Down

0 comments on commit 41b5a8f

Please sign in to comment.