diff options
author | Noel Grandin <noel.grandin@collabora.co.uk> | 2018-09-03 11:32:58 +0200 |
---|---|---|
committer | Noel Grandin <noel.grandin@collabora.co.uk> | 2018-09-06 09:41:48 +0200 |
commit | fad919eb0d30b2303193e1c00ba765514957652c (patch) | |
tree | 10c6d6a8a326d18369148be45a6c4fc66206797d /include/o3tl | |
parent | 62159ea8cc806df327461275563e95dfdff25667 (diff) |
make SwWriteTableRows be a vector of std::unique_ptr
and update o3tl::sorted_vector to handle that
Change-Id: I11a9ec3ec09f835cbd7e49ccda133b9f210d761e
Reviewed-on: https://gerrit.libreoffice.org/59931
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'include/o3tl')
-rw-r--r-- | include/o3tl/sorted_vector.hxx | 48 |
1 files changed, 46 insertions, 2 deletions
diff --git a/include/o3tl/sorted_vector.hxx b/include/o3tl/sorted_vector.hxx index 9141c592ffd8..254d627604df 100644 --- a/include/o3tl/sorted_vector.hxx +++ b/include/o3tl/sorted_vector.hxx @@ -13,6 +13,8 @@ #include <vector> #include <algorithm> #include <functional> +#include <memory> +#include <type_traits> namespace o3tl { @@ -27,8 +29,11 @@ struct find_unique; @tpl Compare comparison method @tpl Find look up index of a Value in the array */ -template<typename Value, typename Compare = std::less<Value>, - template<typename, typename> class Find = find_unique > +template< + typename Value, + typename Compare = std::less<Value>, + template<typename, typename> class Find = find_unique, + bool = std::is_copy_constructible<Value>::value > class sorted_vector { private: @@ -42,6 +47,17 @@ public: // MODIFIERS + std::pair<const_iterator,bool> insert( Value&& x ) + { + std::pair<const_iterator, bool> const ret(Find_t()(m_vector.begin(), m_vector.end(), x)); + if (!ret.second) + { + const_iterator const it = m_vector.insert(m_vector.begin() + (ret.first - m_vector.begin()), std::move(x)); + return std::make_pair(it, true); + } + return std::make_pair(ret.first, false); + } + std::pair<const_iterator,bool> insert( const Value& x ) { std::pair<const_iterator, bool> const ret(Find_t()(m_vector.begin(), m_vector.end(), x)); @@ -197,6 +213,26 @@ private: vector_t m_vector; }; +/* Specialise the template for cases like Value = std::unique_ptr<T>, where + MSVC2017 needs some help +*/ +template< + typename Value, + typename Compare, + template<typename, typename> class Find > +class sorted_vector<Value,Compare,Find,false> : public sorted_vector<Value, Compare, Find, true> +{ +public: + using sorted_vector<Value, Compare, Find, true>::sorted_vector; + + sorted_vector(sorted_vector const&) = delete; + sorted_vector& operator=(sorted_vector const&) = delete; + + sorted_vector() = default; + sorted_vector(sorted_vector&&) = default; + sorted_vector& operator=(sorted_vector&&) = default; +}; + /** Implements an ordering function over a pointer, where the comparison uses the < operator on the pointed-to types. Very useful for the cases where we put pointers to objects inside a sorted_vector. @@ -209,6 +245,14 @@ template <class T> struct less_ptr_to } }; +template <class T> struct less_uniqueptr_to +{ + bool operator() ( std::unique_ptr<T> const& lhs, std::unique_ptr<T> const& rhs ) const + { + return (*lhs) < (*rhs); + } +}; + /** the elements are totally ordered by Compare, for no 2 elements !Compare(a,b) && !Compare(b,a) is true */ |