// 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_ARRAY_TRAITS_STL_H_ #define MOJO_PUBLIC_CPP_BINDINGS_ARRAY_TRAITS_STL_H_ #include #include #include #include "mojo/public/cpp/bindings/array_traits.h" namespace mojo { template struct ArrayTraits> { using Element = T; static bool IsNull(const std::vector& input) { // std::vector<> is always converted to non-null mojom array. return false; } static void SetToNull(std::vector* output) { // std::vector<> doesn't support null state. Set it to empty instead. output->clear(); } static size_t GetSize(const std::vector& input) { return input.size(); } static T* GetData(std::vector& input) { return input.data(); } static const T* GetData(const std::vector& input) { return input.data(); } static typename std::vector::reference GetAt(std::vector& input, size_t index) { return input[index]; } static typename std::vector::const_reference GetAt( const std::vector& input, size_t index) { return input[index]; } static inline bool Resize(std::vector& input, size_t size) { // Instead of calling std::vector::resize() directly, this is a hack to // make compilers happy. Some compilers (e.g., Mac, Android, Linux MSan) // currently don't allow resizing types like // std::vector>. // Because the deserialization code doesn't care about the original contents // of |input|, we discard them directly. // // The "inline" keyword of this method matters. Without it, we have observed // significant perf regression with some tests on Mac. crbug.com/631415 if (input.size() != size) { std::vector temp(size); input.swap(temp); } return true; } }; // This ArrayTraits specialization is used only for serialization. template struct ArrayTraits> { using Element = T; using ConstIterator = typename std::set::const_iterator; static bool IsNull(const std::set& input) { // std::set<> is always converted to non-null mojom array. return false; } static size_t GetSize(const std::set& input) { return input.size(); } static ConstIterator GetBegin(const std::set& input) { return input.begin(); } static void AdvanceIterator(ConstIterator& iterator) { ++iterator; } static const T& GetValue(ConstIterator& iterator) { return *iterator; } }; template struct MapValuesArrayView { explicit MapValuesArrayView(const std::map& map) : map(map) {} const std::map& map; }; // Convenience function to create a MapValuesArrayView<> that infers the // template arguments from its argument type. template MapValuesArrayView MapValuesToArray(const std::map& map) { return MapValuesArrayView(map); } // This ArrayTraits specialization is used only for serialization and converts // a map into an array, discarding the keys. template struct ArrayTraits> { using Element = V; using ConstIterator = typename std::map::const_iterator; static bool IsNull(const MapValuesArrayView& input) { // std::map<> is always converted to non-null mojom array. return false; } static size_t GetSize(const MapValuesArrayView& input) { return input.map.size(); } static ConstIterator GetBegin(const MapValuesArrayView& input) { return input.map.begin(); } static void AdvanceIterator(ConstIterator& iterator) { ++iterator; } static const V& GetValue(ConstIterator& iterator) { return iterator->second; } }; } // namespace mojo #endif // MOJO_PUBLIC_CPP_BINDINGS_ARRAY_TRAITS_STL_H_