blob: bdca97cbac78d55beb1afc9e1fefe79c4d248773 [file] [log] [blame]
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +01001/*
2 * Copyright (c) 2017 ARM Limited.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#ifndef ARM_COMPUTE_TEST_DATASET_CONTAINER
25#define ARM_COMPUTE_TEST_DATASET_CONTAINER
26
27#include "Dataset.h"
28#include "support/ToolchainSupport.h"
29
30#include <string>
31#include <tuple>
32#include <type_traits>
33#include <utility>
34#include <vector>
35
36namespace arm_compute
37{
38namespace test
39{
40namespace framework
41{
42namespace dataset
43{
44/** Base case. Nothing is a container. */
45template <typename T>
46struct is_container : public std::false_type
47{
48};
49
50/** Vector is considered a container. */
51template <typename V, typename A>
52struct is_container<std::vector<V, A>> : public std::true_type
53{
54};
55
56/** Implementation of a dataset created from a container. */
57template <typename T>
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +010058class ContainerDataset : public NamedDataset
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010059{
60private:
61 using container_value_type = typename T::value_type;
62 using container_const_iterator = typename T::const_iterator;
63
64public:
65 /** Construct dataset with given name and values from the container.
66 *
67 * @param[in] name Description of the values.
68 * @param[in] container Values for the dataset.
69 */
70 ContainerDataset(std::string name, T &&container)
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +010071 : NamedDataset{ std::move(name) }, _container(std::forward<T>(container))
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010072 {
73 }
74
75 ContainerDataset(ContainerDataset &&) = default;
76
77 /** Type of the dataset. */
78 using type = std::tuple<container_value_type>;
79
80 /** Iterator for the dataset. */
81 struct iterator
82 {
83 iterator(std::string name, container_const_iterator iterator)
84 : _name{ name }, _iterator{ iterator }
85 {
86 }
87
88 std::string description() const
89 {
90 using support::cpp11::to_string;
91 return _name + "=" + to_string(*_iterator);
92 }
93
94 ContainerDataset::type operator*() const
95 {
96 return std::make_tuple(*_iterator);
97 }
98
99 iterator &operator++()
100 {
101 ++_iterator;
102 return *this;
103 }
104
105 private:
106 std::string _name;
107 container_const_iterator _iterator;
108 };
109
110 /** Iterator pointing at the begin of the dataset.
111 *
112 * @return Iterator for the dataset.
113 */
114 iterator begin() const
115 {
116 return iterator(name(), _container.cbegin());
117 }
118
119 /** Size of the dataset.
120 *
121 * @return Number of values in the dataset.
122 */
123 int size() const
124 {
125 return _container.size();
126 }
127
128private:
129 T _container;
130};
131
132/** Helper function to create a @ref ContainerDataset.
133 *
134 * @param[in] name Name of the dataset.
135 * @param[in] values Container.
136 *
137 * @return A container dataset.
138 */
139template <typename T>
140typename std::enable_if<is_container<T>::value, ContainerDataset<T>>::type make(std::string name, T &&values)
141{
142 return ContainerDataset<T>(std::move(name), std::forward<T>(values));
143}
144} // namespace dataset
145} // namespace framework
146} // namespace test
147} // namespace arm_compute
148#endif /* ARM_COMPUTE_TEST_DATASET_CONTAINER */