OTest2
A C++ testing framework
containertrait.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2020 Ondrej Starek
3  *
4  * This file is part of OTest2
5  *
6  * OTest2 is free software: you can redistribute it and/or modify it under
7  * the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License,
9  * or (at your option) any later version.
10  *
11  * OTest2 is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14  * License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with OTest2. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef OTest2_INCLUDE_OTEST2_CONTAINERTRAIT_H_
21 #define OTest2_INCLUDE_OTEST2_CONTAINERTRAIT_H_
22 
23 #include <map>
24 #include <unordered_map>
25 #include <utility>
26 
27 #include <otest2/typetraits.h>
28 
29 namespace OTest2 {
30 
44 template<typename Container_>
49  static typename Container_::const_iterator begin(
50  const Container_& cont_) {
51  return cont_.begin();
52  }
53 
57  static typename Container_::const_iterator end(
58  const Container_& cont_) {
59  return cont_.end();
60  }
61 };
62 
63 namespace Private {
64 
65 template<typename Type_>
67  private:
68  template<typename T_> struct SfinaeCheck : public std::true_type {};
69 
70  template<typename T_>
71  static auto test_fce(void*) -> SfinaeCheck<decltype(&T_::key_comp)>;
72  template<typename T_>
73  static std::false_type test_fce(...);
74 
75  public:
76  static constexpr bool value = decltype(test_fce<Type_>(nullptr))::value;
77 };
78 
79 template<typename Container_>
84  typedef typename Container_::key_type KeyType;
85 
89  typedef typename Container_::mapped_type ValueType;
90 
91  typedef typename Container_::const_iterator const_iterator;
92 
97  const Container_& cont_) {
98  return cont_.begin();
99  }
100 
105  const Container_& cont_) {
106  return cont_.end();
107  }
108 
112  static std::pair<const_iterator, const_iterator> equalRange(
113  const Container_& cont_,
114  typename TypeTrait<KeyType>::BestArg key_) {
115  return cont_.equal_range(key_);
116  }
117 };
118 
119 template<typename Container_, bool key_comp_>
121 
122 template<typename Container_>
123 struct MapTraitImpl<Container_, true> : public MapTraitImplCommon<Container_>
124 {
128  static bool keyEqual(
129  const Container_& cont_,
130  typename TypeTrait<typename MapTraitImplCommon<Container_>::KeyType>::BestArg a_key_,
131  typename TypeTrait<typename MapTraitImplCommon<Container_>::KeyType>::BestArg b_key_) {
132  return !cont_.key_comp()(a_key_, b_key_) && !cont_.key_comp()(b_key_, a_key_);
133  }
134 };
135 
136 template<typename Container_>
137 struct MapTraitImpl<Container_, false> : public MapTraitImplCommon<Container_>
138 {
142  static bool keyEqual(
143  const Container_& cont_,
144  typename TypeTrait<typename MapTraitImplCommon<Container_>::KeyType>::BestArg a_key_,
145  typename TypeTrait<typename MapTraitImplCommon<Container_>::KeyType>::BestArg b_key_) {
146  return cont_.key_eq()(a_key_, b_key_);
147  }
148 };
149 
150 } /* -- namespace Private */
151 
166 template<typename Container_>
168  public Private::MapTraitImpl<
169  Container_, Private::HasContainerKeyComp<Container_>::value> {};
170 
171 } /* -- namespace OTest2 */
172 
173 #endif /* -- OTest2_INCLUDE_OTEST2_CONTAINERTRAIT_H_ */
OTest2::Private::MapTraitImplCommon::end
static const_iterator end(const Container_ &cont_)
Get ending iterator.
Definition: containertrait.h:104
OTest2::Private::MapTraitImpl< Container_, false >::keyEqual
static bool keyEqual(const Container_ &cont_, typename TypeTrait< typename MapTraitImplCommon< Container_ >::KeyType >::BestArg a_key_, typename TypeTrait< typename MapTraitImplCommon< Container_ >::KeyType >::BestArg b_key_)
Compare equality of two keys.
Definition: containertrait.h:142
OTest2::Private::MapTraitImplCommon::ValueType
Container_::mapped_type ValueType
Type of the mapped value.
Definition: containertrait.h:89
OTest2::TypeTrait::BestArg
Private::SelectArgType< Type_, std::is_scalar< Type_ >::value >::Value BestArg
Definition: typetraits.h:49
OTest2::Private::MapTraitImpl
Definition: containertrait.h:120
OTest2::ListContainerTrait::end
static Container_::const_iterator end(const Container_ &cont_)
Get ending iterator.
Definition: containertrait.h:57
OTest2::TypeTrait
Generic type trait used by the framework.
Definition: typetraits.h:45
typetraits.h
OTest2::Private::MapTraitImplCommon::equalRange
static std::pair< const_iterator, const_iterator > equalRange(const Container_ &cont_, typename TypeTrait< KeyType >::BestArg key_)
Get range of items with the same key.
Definition: containertrait.h:112
OTest2::ListContainerTrait::begin
static Container_::const_iterator begin(const Container_ &cont_)
Get beginning iterator.
Definition: containertrait.h:49
OTest2::Private::MapTraitImplCommon::begin
static const_iterator begin(const Container_ &cont_)
Get the beginning forward iterator.
Definition: containertrait.h:96
OTest2::Private::MapTraitImplCommon
Definition: containertrait.h:80
OTest2
Definition: assertbean.h:25
OTest2::MapContainerTrait
A trait for access to a map container.
Definition: containertrait.h:167
OTest2::ListContainerTrait
A simple trait allowing access to list container's iterators.
Definition: containertrait.h:45
OTest2::Private::HasContainerKeyComp::value
static constexpr bool value
Definition: containertrait.h:76
OTest2::Private::MapTraitImplCommon::KeyType
Container_::key_type KeyType
Type of the key.
Definition: containertrait.h:84
OTest2::Private::HasContainerKeyComp
Definition: containertrait.h:66
OTest2::Private::MapTraitImpl< Container_, true >::keyEqual
static bool keyEqual(const Container_ &cont_, typename TypeTrait< typename MapTraitImplCommon< Container_ >::KeyType >::BestArg a_key_, typename TypeTrait< typename MapTraitImplCommon< Container_ >::KeyType >::BestArg b_key_)
Compare equality of two keys.
Definition: containertrait.h:128
OTest2::Private::MapTraitImplCommon::const_iterator
Container_::const_iterator const_iterator
Definition: containertrait.h:91