xref: /aosp_15_r20/external/ComputeLibrary/tests/framework/datasets/SingletonDataset.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2017-2020 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_SINGLETON
25 #define ARM_COMPUTE_TEST_DATASET_SINGLETON
26 
27 #include "ContainerDataset.h"
28 #include "Dataset.h"
29 #include "support/StringSupport.h"
30 
31 #include <string>
32 #include <tuple>
33 #include <type_traits>
34 #include <utility>
35 
36 namespace arm_compute
37 {
38 namespace test
39 {
40 namespace framework
41 {
42 namespace dataset
43 {
44 /** Implementation of a dataset holding a single value. */
45 template <typename T>
46 class SingletonDataset : public NamedDataset
47 {
48 public:
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      */
SingletonDataset(std::string name,T && value)54     SingletonDataset(std::string name, T &&value)
55         : NamedDataset{ std::move(name) }, _value{ std::forward<T>(value) }
56     {
57     }
58 
59     /** Allow instances of this class to be move constructed */
60     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     {
68         /** Construct an iterator.
69          *
70          * @param[in] name  Name of the dataset.
71          * @param[in] value The singleton value.
72          */
iteratoriterator73         iterator(std::string name, const T *value)
74             : _name{ name }, _value{ value }
75         {
76         }
77 
78         /** Default destructor. */
79         ~iterator() = default;
80 
81         /** Allow instances of this class to be copy constructed */
82         iterator(const iterator &) = default;
83         /** Allow instances of this class to be copied */
84         iterator &operator=(const iterator &) = default;
85         /** Allow instances of this class to be move constructed */
86         iterator(iterator &&) = default;
87         /** Allow instances of this class to be moved */
88         iterator &operator=(iterator &&) = default;
89 
90         /** Get the description of the current value.
91          *
92          * @return description of the current value.
93          */
descriptioniterator94         std::string description() const
95         {
96             using support::cpp11::to_string;
97             return _name + "=" + to_string(*_value);
98         }
99 
100         /** Get the value of the iterator.
101          *
102          * @return the value of the iterator.
103          */
104         SingletonDataset::type operator*() const
105         {
106             return std::make_tuple(*_value);
107         }
108 
109         /** Inrement the iterator.
110          *
111          * @return *this;
112          */
113         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      */
begin()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      */
size()136     int size() const
137     {
138         return 1;
139     }
140 
141 private:
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  */
152 template <typename T>
make(std::string name,T && value)153 typename 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 */
162