Commit dc599fc6 authored by Jan Wilken Doerrie's avatar Jan Wilken Doerrie Committed by Commit Bot

[base] Implement Windows::Foundation::IReference

This change provides an implementation of Windows::Foundation's
IReference interface. This class is supposed to be used for optional
POD types, as a null value usually is not possible here.

Bug: 821766
Change-Id: Icc6ff4f5313e2cfa13ca33f8d54e150efa616d76
Reviewed-on: https://chromium-review.googlesource.com/1106385Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarRobert Liao <robliao@chromium.org>
Commit-Queue: Jan Wilken Dörrie <jdoerrie@chromium.org>
Cr-Commit-Position: refs/heads/master@{#568877}
parent e1d23aea
......@@ -1052,6 +1052,7 @@ jumbo_component("base") {
"win/patch_util.h",
"win/process_startup_helper.cc",
"win/process_startup_helper.h",
"win/reference.h",
"win/registry.cc",
"win/registry.h",
"win/resource_util.cc",
......@@ -2443,6 +2444,7 @@ test("base_unittests") {
"win/message_window_unittest.cc",
"win/object_watcher_unittest.cc",
"win/pe_image_unittest.cc",
"win/reference_unittest.cc",
"win/registry_unittest.cc",
"win/scoped_bstr_unittest.cc",
"win/scoped_handle_unittest.cc",
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef BASE_WIN_REFERENCE_H_
#define BASE_WIN_REFERENCE_H_
#include <windows.foundation.collections.h>
#include <wrl/implements.h>
#include <type_traits>
#include <utility>
#include "base/macros.h"
namespace base {
namespace win {
// Implementation of the UWP's IReference interface.
template <typename T>
class Reference
: public Microsoft::WRL::RuntimeClass<
Microsoft::WRL::RuntimeClassFlags<
Microsoft::WRL::WinRt | Microsoft::WRL::InhibitRoOriginateError>,
ABI::Windows::Foundation::IReference<T>> {
public:
using AbiT = typename ABI::Windows::Foundation::Internal::GetAbiType<
typename ABI::Windows::Foundation::IReference<T>::T_complex>::type;
explicit Reference(const AbiT& value) : value_(value) {}
explicit Reference(AbiT&& value) : value_(std::move(value)) {}
// ABI::Windows::Foundation::IReference:
IFACEMETHODIMP get_Value(AbiT* value) override {
*value = value_;
return S_OK;
}
private:
~Reference() = default;
AbiT value_;
DISALLOW_COPY_AND_ASSIGN(Reference);
};
} // namespace win
} // namespace base
#endif // BASE_WIN_REFERENCE_H_
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/win/reference.h"
#include <windows.foundation.h>
#include <wrl/client.h>
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace win {
namespace {
using Microsoft::WRL::Make;
} // namespace
TEST(ReferenceTest, Value) {
auto ref = Make<Reference<int>>(123);
int value = 0;
HRESULT hr = ref->get_Value(&value);
EXPECT_TRUE(SUCCEEDED(hr));
EXPECT_EQ(123, value);
}
TEST(ReferenceTest, ValueAggregate) {
auto ref = Make<Reference<bool>>(true);
boolean value = false;
HRESULT hr = ref->get_Value(&value);
EXPECT_TRUE(SUCCEEDED(hr));
EXPECT_TRUE(value);
}
} // namespace win
} // namespace base
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment