Commit 2c8a4081 authored by sammc's avatar sammc Committed by Commit bot

Remove mojo::WTFMap.

BUG=674766

Review-Url: https://codereview.chromium.org/2608503002
Cr-Commit-Position: refs/heads/master@{#440900}
parent c4518770
......@@ -180,11 +180,9 @@ if (!is_ios) {
"lib/wtf_clone_equals_util.h",
"lib/wtf_hash_util.h",
"lib/wtf_serialization.h",
"map_traits_wtf.h",
"map_traits_wtf_hash_map.h",
"string_traits_wtf.h",
"wtf_array.h",
"wtf_map.h",
]
public_deps = [
......
......@@ -7,7 +7,6 @@
#include "mojo/public/cpp/bindings/array_traits_wtf.h"
#include "mojo/public/cpp/bindings/array_traits_wtf_vector.h"
#include "mojo/public/cpp/bindings/map_traits_wtf.h"
#include "mojo/public/cpp/bindings/map_traits_wtf_hash_map.h"
#include "mojo/public/cpp/bindings/string_traits_wtf.h"
......
// Copyright 2016 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 MOJO_PUBLIC_CPP_BINDINGS_MAP_TRAITS_WTF_H_
#define MOJO_PUBLIC_CPP_BINDINGS_MAP_TRAITS_WTF_H_
#include "base/logging.h"
#include "mojo/public/cpp/bindings/map_traits.h"
#include "mojo/public/cpp/bindings/wtf_map.h"
namespace mojo {
template <typename K, typename V>
struct MapTraits<WTFMap<K, V>> {
using Key = K;
using Value = V;
using Iterator = typename WTFMap<K, V>::Iterator;
using ConstIterator = typename WTFMap<K, V>::ConstIterator;
static bool IsNull(const WTFMap<K, V>& input) { return input.is_null(); }
static void SetToNull(WTFMap<K, V>* output) { *output = nullptr; }
static size_t GetSize(const WTFMap<K, V>& input) { return input.size(); }
static ConstIterator GetBegin(const WTFMap<K, V>& input) {
return input.begin();
}
static Iterator GetBegin(WTFMap<K, V>& input) { return input.begin(); }
static void AdvanceIterator(ConstIterator& iterator) { ++iterator; }
static void AdvanceIterator(Iterator& iterator) { ++iterator; }
static const K& GetKey(Iterator& iterator) { return iterator->key; }
static const K& GetKey(ConstIterator& iterator) { return iterator->key; }
static V& GetValue(Iterator& iterator) { return iterator->value; }
static const V& GetValue(ConstIterator& iterator) { return iterator->value; }
static bool Insert(WTFMap<K, V>& input, const K& key, V&& value) {
if (!WTFMap<K, V>::IsValidKey(key)) {
LOG(ERROR) << "The key value is disallowed by WTF::HashMap: " << key;
return false;
}
input.insert(key, std::forward<V>(value));
return true;
}
static bool Insert(WTFMap<K, V>& input, const K& key, const V& value) {
if (!WTFMap<K, V>::IsValidKey(key)) {
LOG(ERROR) << "The key value is disallowed by WTF::HashMap: " << key;
return false;
}
input.insert(key, value);
return true;
}
static void SetToEmpty(WTFMap<K, V>* output) { output->SetToEmpty(); }
};
} // namespace mojo
#endif // MOJO_PUBLIC_CPP_BINDINGS_MAP_TRAITS_WTF_H_
......@@ -39,7 +39,7 @@ namespace mojo {
// - map:
// Value or reference of any type that has a MapTraits defined.
// Supported by default: std::map, std::unordered_map, mojo::Map,
// WTF::HashMap (in blink), mojo::WTFMap (in blink).
// WTF::HashMap (in blink).
//
// - struct:
// Value or reference of any type that has a StructTraits defined.
......
......@@ -83,11 +83,9 @@ if (!is_ios) {
"array_common_test.h",
"container_test_util.cc",
"container_test_util.h",
"map_common_test.h",
"variant_test_util.h",
"wtf_array_unittest.cc",
"wtf_hash_unittest.cc",
"wtf_map_unittest.cc",
"wtf_types_unittest.cc",
]
......
// Copyright 2016 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 "mojo/public/cpp/bindings/wtf_map.h"
#include "mojo/public/cpp/bindings/lib/wtf_serialization.h"
#include "mojo/public/cpp/bindings/tests/container_test_util.h"
#include "mojo/public/cpp/bindings/tests/map_common_test.h"
#include "mojo/public/cpp/bindings/tests/rect_blink.h"
#include "mojo/public/cpp/bindings/wtf_array.h"
#include "mojo/public/interfaces/bindings/tests/rect.mojom-blink.h"
#include "mojo/public/interfaces/bindings/tests/test_structs.mojom-blink.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/WebKit/Source/wtf/text/WTFString.h"
namespace mojo {
namespace test {
namespace {
using WTFMapTest = testing::Test;
MAP_COMMON_TEST(WTFMap, NullAndEmpty)
MAP_COMMON_TEST(WTFMap, InsertWorks)
MAP_COMMON_TEST(WTFMap, TestIndexOperator)
MAP_COMMON_TEST(WTFMap, TestIndexOperatorAsRValue)
MAP_COMMON_TEST(WTFMap, TestIndexOperatorMoveOnly)
MAP_COMMON_TEST(WTFMap, MapArrayClone)
MAP_COMMON_TEST(WTFMap, ArrayOfMap)
TEST_F(WTFMapTest, MoveFromAndToWTFHashMap_Copyable) {
WTF::HashMap<int32_t, CopyableType> map1;
map1.add(123, CopyableType());
map1.find(123)->value.ResetCopied();
ASSERT_FALSE(map1.find(123)->value.copied());
WTFMap<int32_t, CopyableType> mojo_map(std::move(map1));
ASSERT_EQ(1u, mojo_map.size());
ASSERT_NE(mojo_map.end(), mojo_map.find(123));
ASSERT_FALSE(mojo_map[123].copied());
WTF::HashMap<int32_t, CopyableType> map2(mojo_map.PassStorage());
ASSERT_EQ(1u, map2.size());
ASSERT_NE(map2.end(), map2.find(123));
ASSERT_FALSE(map2.find(123)->value.copied());
ASSERT_EQ(0u, mojo_map.size());
ASSERT_TRUE(mojo_map.is_null());
}
TEST_F(WTFMapTest, MoveFromAndToWTFHashMap_MoveOnly) {
WTF::HashMap<int32_t, MoveOnlyType> map1;
map1.add(123, MoveOnlyType());
WTFMap<int32_t, MoveOnlyType> mojo_map(std::move(map1));
ASSERT_EQ(1u, mojo_map.size());
ASSERT_NE(mojo_map.end(), mojo_map.find(123));
WTF::HashMap<int32_t, MoveOnlyType> map2(mojo_map.PassStorage());
ASSERT_EQ(1u, map2.size());
ASSERT_NE(map2.end(), map2.find(123));
ASSERT_EQ(0u, mojo_map.size());
ASSERT_TRUE(mojo_map.is_null());
}
static blink::RectPtr MakeRect(int32_t x,
int32_t y,
int32_t width,
int32_t height) {
blink::RectPtr rect_ptr = blink::Rect::New();
rect_ptr->x = x;
rect_ptr->y = y;
rect_ptr->width = width;
rect_ptr->height = height;
return rect_ptr;
}
TEST_F(WTFMapTest, StructKey) {
WTF::HashMap<blink::RectPtr, int32_t> map;
map.add(MakeRect(1, 2, 3, 4), 123);
blink::RectPtr key = MakeRect(1, 2, 3, 4);
ASSERT_NE(map.end(), map.find(key));
ASSERT_EQ(123, map.find(key)->value);
map.remove(key);
ASSERT_EQ(0u, map.size());
}
static blink::ContainsHashablePtr MakeContainsHashablePtr(RectBlink rect) {
blink::ContainsHashablePtr ptr = blink::ContainsHashable::New();
ptr->rect = rect;
return ptr;
}
TEST_F(WTFMapTest, TypemappedStructKey) {
WTF::HashMap<blink::ContainsHashablePtr, int32_t> map;
map.add(MakeContainsHashablePtr(RectBlink(1, 2, 3, 4)), 123);
blink::ContainsHashablePtr key =
MakeContainsHashablePtr(RectBlink(1, 2, 3, 4));
ASSERT_NE(map.end(), map.find(key));
ASSERT_EQ(123, map.find(key)->value);
map.remove(key);
ASSERT_EQ(0u, map.size());
}
} // namespace
} // namespace test
} // namespace mojo
......@@ -177,66 +177,6 @@ TEST_F(WTFTypesTest, Serialization_WTFArrayToMojoArray) {
EXPECT_TRUE(kUTF8HelloWorld == strs2[3]);
}
TEST_F(WTFTypesTest, Serialization_WTFMapToWTFMap) {
using WTFType = WTFMap<WTF::String, WTF::String>;
using MojomType = MapDataView<StringDataView, StringDataView>;
WTFType str_map = ConstructStringMap();
WTFType cloned_str_map = str_map.Clone();
mojo::internal::SerializationContext context;
size_t size =
mojo::internal::PrepareToSerialize<MojomType>(cloned_str_map, &context);
mojo::internal::FixedBufferForTesting buf(size);
typename mojo::internal::MojomTypeTraits<MojomType>::Data* data;
mojo::internal::ContainerValidateParams validate_params(
new mojo::internal::ContainerValidateParams(
0, false,
new mojo::internal::ContainerValidateParams(0, false, nullptr)),
new mojo::internal::ContainerValidateParams(
0, true,
new mojo::internal::ContainerValidateParams(0, false, nullptr)));
mojo::internal::Serialize<MojomType>(cloned_str_map, &buf, &data,
&validate_params, &context);
WTFType str_map2;
mojo::internal::Deserialize<MojomType>(data, &str_map2, &context);
EXPECT_TRUE(str_map.Equals(str_map2));
}
TEST_F(WTFTypesTest, Serialization_WTFMapToMojoMap) {
using WTFType = WTFMap<WTF::String, WTF::String>;
using MojomType = MapDataView<StringDataView, StringDataView>;
WTFType str_map = ConstructStringMap();
mojo::internal::SerializationContext context;
size_t size =
mojo::internal::PrepareToSerialize<MojomType>(str_map, &context);
mojo::internal::FixedBufferForTesting buf(size);
typename mojo::internal::MojomTypeTraits<MojomType>::Data* data;
mojo::internal::ContainerValidateParams validate_params(
new mojo::internal::ContainerValidateParams(
0, false,
new mojo::internal::ContainerValidateParams(0, false, nullptr)),
new mojo::internal::ContainerValidateParams(
0, true,
new mojo::internal::ContainerValidateParams(0, false, nullptr)));
mojo::internal::Serialize<MojomType>(str_map, &buf, &data, &validate_params,
&context);
Map<mojo::String, mojo::String> str_map2;
mojo::internal::Deserialize<MojomType>(data, &str_map2, &context);
ASSERT_EQ(3u, str_map2.size());
EXPECT_TRUE(str_map2["0"].is_null());
EXPECT_TRUE(kHelloWorld == str_map2["1"]);
EXPECT_TRUE(kUTF8HelloWorld == str_map2["2"]);
}
TEST_F(WTFTypesTest, Serialization_PublicAPI) {
blink::TestWTFStructPtr input(blink::TestWTFStruct::New());
input->str = kHelloWorld;
......
// Copyright 2016 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 MOJO_PUBLIC_CPP_BINDINGS_WTF_MAP_H_
#define MOJO_PUBLIC_CPP_BINDINGS_WTF_MAP_H_
#include <stddef.h>
#include <utility>
#include "base/macros.h"
#include "mojo/public/cpp/bindings/lib/template_util.h"
#include "mojo/public/cpp/bindings/lib/wtf_clone_equals_util.h"
#include "mojo/public/cpp/bindings/type_converter.h"
#include "third_party/WebKit/Source/wtf/HashMap.h"
#include "third_party/WebKit/Source/wtf/text/StringHash.h"
namespace mojo {
// Represents a map backed by WTF::HashMap. Comparing with WTF::HashMap,
// mojo::WTFMap is move-only and can be null.
//
// It is easy to convert between WTF::HashMap<K, V> and mojo::WTFMap<K, V>:
// - constructor WTFMap(WTF::HashMap<K, V>&&) takes the contents of a
// WTF::HashMap<K, V>;
// - method PassStorage() passes the underlying WTF::HashMap.
//
// NOTE: WTF::HashMap disallows certain key values. For integer types, those are
// 0 and -1 (max value instead of -1 for unsigned). For string, that is null.
template <typename Key, typename Value>
class WTFMap {
public:
using Iterator = typename WTF::HashMap<Key, Value>::iterator;
using ConstIterator = typename WTF::HashMap<Key, Value>::const_iterator;
// Constructs an empty map.
WTFMap() : is_null_(false) {}
// Constructs a null map.
WTFMap(std::nullptr_t null_pointer) : is_null_(true) {}
~WTFMap() {}
WTFMap(WTF::HashMap<Key, Value>&& other)
: map_(std::move(other)), is_null_(false) {}
WTFMap(WTFMap&& other) : is_null_(true) { Take(&other); }
WTFMap& operator=(WTF::HashMap<Key, Value>&& other) {
is_null_ = false;
map_ = std::move(other);
return *this;
}
WTFMap& operator=(WTFMap&& other) {
Take(&other);
return *this;
}
WTFMap& operator=(std::nullptr_t null_pointer) {
is_null_ = true;
map_.clear();
return *this;
}
static bool IsValidKey(const Key& key) {
return WTF::HashMap<Key, Value>::isValidKey(key);
}
// Copies the contents of some other type of map into a new WTFMap using a
// TypeConverter.
template <typename U>
static WTFMap From(const U& other) {
return TypeConverter<WTFMap, U>::Convert(other);
}
// Copies the contents of the WTFMap into some other type of map.
template <typename U>
U To() const {
return TypeConverter<U, WTFMap>::Convert(*this);
}
// Indicates whether the map is null (which is distinct from empty).
bool is_null() const { return is_null_; }
// Indicates whether the map is empty (which is distinct from null).
bool empty() const { return map_.isEmpty() && !is_null_; }
// Indicates the number of keys in the map, which will be zero if the map is
// null.
size_t size() const { return map_.size(); }
// Inserts a key-value pair into the map. Like WTF::HashMap::add(), this does
// not insert |value| if |key| is already a member of the map.
void insert(const Key& key, const Value& value) {
is_null_ = false;
map_.add(key, value);
}
void insert(const Key& key, Value&& value) {
is_null_ = false;
map_.add(key, std::move(value));
}
// Returns a reference to the value associated with the specified key,
// crashing the process if the key is not present in the map.
Value& at(const Key& key) { return map_.find(key)->value; }
const Value& at(const Key& key) const { return map_.find(key)->value; }
// Returns a reference to the value associated with the specified key,
// creating a new entry if the key is not already present in the map. A
// newly-created value will be value-initialized (meaning that it will be
// initialized by the default constructor of the value type, if any, or else
// will be zero-initialized).
Value& operator[](const Key& key) {
is_null_ = false;
if (!map_.contains(key))
map_.add(key, Value());
return at(key);
}
// Sets the map to empty (even if previously it was null).
void SetToEmpty() {
is_null_ = false;
map_.clear();
}
// Returns a const reference to the WTF::HashMap managed by this class. If
// this object is null, the return value will be an empty map.
const WTF::HashMap<Key, Value>& storage() const { return map_; }
// Passes the underlying storage and resets this map to null.
WTF::HashMap<Key, Value> PassStorage() {
is_null_ = true;
return std::move(map_);
}
// Swaps the contents of this WTFMap with another WTFMap of the same type
// (including nullness).
void Swap(WTFMap<Key, Value>* other) {
std::swap(is_null_, other->is_null_);
map_.swap(other->map_);
}
// Swaps the contents of this WTFMap with an WTF::HashMap containing keys and
// values of the same type. Since WTF::HashMap cannot represent the null
// state, the WTF::HashMap will be empty if WTFMap is null. The WTFMap will
// always be left in a non-null state.
void Swap(WTF::HashMap<Key, Value>* other) {
is_null_ = false;
map_.swap(*other);
}
// Returns a new WTFMap that contains a copy of the contents of this map. If
// the key/value type defines a Clone() method, it will be used; otherwise
// copy constructor/assignment will be used.
//
// Please note that calling this method will fail compilation if the key/value
// type cannot be cloned (which usually means that it is a Mojo handle type or
// a type containing Mojo handles).
WTFMap Clone() const {
WTFMap result;
result.is_null_ = is_null_;
result.map_ = internal::Clone(map_);
return result;
}
// Indicates whether the contents of this map are equal to those of another
// WTFMap (including nullness). If the key/value type defines an Equals()
// method, it will be used; otherwise == operator will be used.
bool Equals(const WTFMap& other) const {
if (is_null() != other.is_null())
return false;
return internal::Equals(map_, other.map_);
}
ConstIterator begin() const { return map_.begin(); }
Iterator begin() { return map_.begin(); }
ConstIterator end() const { return map_.end(); }
Iterator end() { return map_.end(); }
// Returns the iterator pointing to the entry for |key|, if present, or else
// returns end().
ConstIterator find(const Key& key) const { return map_.find(key); }
Iterator find(const Key& key) { return map_.find(key); }
explicit operator bool() const { return !is_null_; }
private:
void Take(WTFMap* other) {
operator=(nullptr);
Swap(other);
}
WTF::HashMap<Key, Value> map_;
bool is_null_;
DISALLOW_COPY_AND_ASSIGN(WTFMap);
};
} // namespace mojo
#endif // MOJO_PUBLIC_CPP_BINDINGS_WTF_MAP_H_
......@@ -73,7 +73,6 @@ namespace {{variant}} {
every use of {Inlined}StructPtr. #}
#include "mojo/public/cpp/bindings/lib/wtf_hash_util.h"
#include "mojo/public/cpp/bindings/wtf_array.h"
#include "mojo/public/cpp/bindings/wtf_map.h"
#include "third_party/WebKit/Source/wtf/HashFunctions.h"
#include "third_party/WebKit/Source/wtf/Optional.h"
#include "third_party/WebKit/Source/wtf/text/WTFString.h"
......
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