blob: 47a38ec6f251c682551a0ab5545a3ea5e979115d [file] [log] [blame]
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +01001/*
Alex Gildayc357c472018-03-21 13:54:09 +00002 * Copyright (c) 2017-2018 ARM Limited.
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +01003 *
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_SINGLETON
25#define ARM_COMPUTE_TEST_DATASET_SINGLETON
26
27#include "ContainerDataset.h"
28#include "Dataset.h"
29#include "support/ToolchainSupport.h"
30
31#include <string>
32#include <tuple>
33#include <type_traits>
34#include <utility>
35
36namespace arm_compute
37{
38namespace test
39{
40namespace framework
41{
42namespace dataset
43{
44/** Implementation of a dataset holding a single value. */
45template <typename T>
Moritz Pflanzerb7c2a992017-07-18 14:37:35 +010046class SingletonDataset : public NamedDataset
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010047{
48public:
49 /** Construct dataset with given name and value.
50 *
51 * @param[in] name Description of the value.
52 * @param[in] value Value for the dataset.
53 */
54 SingletonDataset(std::string name, T &&value)
55 : NamedDataset{ std::move(name) }, _value{ std::forward<T>(value) }
56 {
57 }
58
Alex Gildayc357c472018-03-21 13:54:09 +000059 /** Allow instances of this class to be move constructed */
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010060 SingletonDataset(SingletonDataset &&) = default;
61
62 /** Type of the dataset. */
63 using type = std::tuple<T>;
64
65 /** Iterator for the dataset. */
66 struct iterator
67 {
Alex Gildayc357c472018-03-21 13:54:09 +000068 /** Construct an iterator.
69 *
70 * @param[in] name Name of the dataset.
71 * @param[in] value The singleton value.
72 */
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010073 iterator(std::string name, const T *value)
74 : _name{ name }, _value{ value }
75 {
76 }
77
Alex Gildayc357c472018-03-21 13:54:09 +000078 /** Default destructor. */
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010079 ~iterator() = default;
80
Alex Gildayc357c472018-03-21 13:54:09 +000081 /** Allow instances of this class to be copy constructed */
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010082 iterator(const iterator &) = default;
Alex Gildayc357c472018-03-21 13:54:09 +000083 /** Allow instances of this class to be copied */
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010084 iterator &operator=(const iterator &) = default;
Alex Gildayc357c472018-03-21 13:54:09 +000085 /** Allow instances of this class to be move constructed */
86 iterator(iterator &&) = default;
87 /** Allow instances of this class to be moved */
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010088 iterator &operator=(iterator &&) = default;
89
Alex Gildayc357c472018-03-21 13:54:09 +000090 /** Get the description of the current value.
91 *
92 * @return description of the current value.
93 */
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +010094 std::string description() const
95 {
96 using support::cpp11::to_string;
97 return _name + "=" + to_string(*_value);
98 }
99
Alex Gildayc357c472018-03-21 13:54:09 +0000100 /** Get the value of the iterator.
101 *
102 * @return the value of the iterator.
103 */
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +0100104 SingletonDataset::type operator*() const
105 {
106 return std::make_tuple(*_value);
107 }
108
Alex Gildayc357c472018-03-21 13:54:09 +0000109 /** Inrement the iterator.
110 *
111 * @return *this;
112 */
Moritz Pflanzer69e44dc2017-07-05 11:02:14 +0100113 iterator &operator++()
114 {
115 return *this;
116 }
117
118 private:
119 std::string _name;
120 const T *_value;
121 };
122
123 /** Iterator pointing at the begin of the dataset.
124 *
125 * @return Iterator for the dataset.
126 */
127 iterator begin() const
128 {
129 return iterator(name(), &_value);
130 }
131
132 /** Size of the dataset.
133 *
134 * @return Number of values in the dataset.
135 */
136 int size() const
137 {
138 return 1;
139 }
140
141private:
142 T _value;
143};
144
145/** Helper function to create a @ref SingletonDataset.
146 *
147 * @param[in] name Name of the dataset.
148 * @param[in] value Value.
149 *
150 * @return A singleton dataset.
151 */
152template <typename T>
153typename std::enable_if < !is_container<T>::value, SingletonDataset<T >>::type make(std::string name, T &&value)
154{
155 return SingletonDataset<T>(std::move(name), std::forward<T>(value));
156}
157} // namespace dataset
158} // namespace framework
159} // namespace test
160} // namespace arm_compute
161#endif /* ARM_COMPUTE_TEST_DATASET_SINGLETON */