LibraryLink Utilities  3.0.1
Modern C++ wrapper over LibraryLink and WSTP
DataList.h
Go to the documentation of this file.
1 
8 #ifndef LLU_CONTAINERS_DATALIST_H
9 #define LLU_CONTAINERS_DATALIST_H
10 
11 #include <initializer_list>
12 #include <string>
13 #include <string_view>
14 #include <utility>
15 #include <vector>
16 
20 #include "LLU/LibraryData.h"
21 #include "LLU/MArgument.h"
22 #include "LLU/TypedMArgument.h"
23 
24 namespace LLU {
25 
26  namespace NodeType = Argument::Typed;
27 
35  template<typename T>
36  class DataList : public MContainer<MArgumentType::DataStore> {
37  public:
40 
43 
46 
49 
51  using value_type = T;
52 
53  public:
54  using GenericDataList::GenericDataList; // NOLINT(modernize-use-equals-default): false positive
55 
60  explicit DataList(GenericDataList gds);
61 
67  DataList(std::initializer_list<value_type> initList);
68 
74  DataList(std::initializer_list<std::pair<std::string, value_type>> initList);
75 
81  [[nodiscard]] DataList clone() const;
82 
86  iterator begin() const {
87  return iterator {front()};
88  }
89 
94  return const_iterator {begin()};
95  }
96 
100  iterator end() const {
101  return iterator {nullptr};
102  }
103 
108  return const_iterator {end()};
109  }
110 
115  return value_iterator {front()};
116  }
117 
122  return value_iterator {nullptr};
123  }
124 
129  return name_iterator {front()};
130  }
131 
136  return name_iterator {nullptr};
137  }
138 
143  void push_back(value_type nodeData);
144 
150  void push_back(std::string_view name, value_type nodeData);
151 
156  std::vector<T> values() const {
157  return {valueBegin(), valueEnd()};
158  }
159 
164  std::vector<std::string> names() const {
165  return {nameBegin(), nameEnd()};
166  }
167 
172  std::vector<DataNode<T>> toVector() const {
173  return {cbegin(), cend()};
174  }
175  };
176 
177  /* Definitions od DataList methods */
178 
179  template<typename T>
181  if constexpr (!std::is_same_v<T, LLU::NodeType::Any>) {
182  std::for_each(GenericDataList::cbegin(), GenericDataList::cend(), [](auto node) {
183  if (node.type() != Argument::WrapperIndex<T>) {
184  ErrorManager::throwException(ErrorName::DLInvalidNodeType);
185  }
186  });
187  }
188  }
189 
190  template<typename T>
191  DataList<T>::DataList(std::initializer_list<value_type> initList) : DataList() {
192  for (auto&& elem : initList) {
193  push_back(std::move(elem));
194  }
195  }
196 
197  template<typename T>
198  DataList<T>::DataList(std::initializer_list<std::pair<std::string, value_type>> initList) : DataList() {
199  for (auto&& elem : initList) {
200  push_back(elem.first, std::move(elem.second));
201  }
202  }
203 
204  template<typename T>
206  return DataList {cloneContainer(), Ownership::Library};
207  }
208 
209  template<typename T>
211  GenericDataList::push_back(std::move(nodeData));
212  }
213 
214  template<typename T>
215  void DataList<T>::push_back(std::string_view name, value_type nodeData) {
216  GenericDataList::push_back(name, std::move(nodeData));
217  }
218 
219 
220  namespace Detail {
221  template<typename T, typename IteratorType>
222  struct IteratorAdaptor {
223  using iterator = IteratorType;
224 
225  explicit IteratorAdaptor(DataList<T>& d) : dl {d} {};
226 
227  iterator begin() const {
228  return iterator {dl.begin()};
229  }
230 
231  iterator cbegin() {
232  return iterator {dl.begin()};
233  }
234 
235  iterator end() const {
236  return iterator {dl.end()};
237  }
238 
239  iterator end() {
240  return iterator {dl.end()};
241  }
242 
243  private:
244  DataList<T>& dl;
245  };
246  } // namespace Detail
247 
253  template<typename T>
254  struct ValueAdaptor : Detail::IteratorAdaptor<T, NodeValueIterator<T>> {
259  explicit ValueAdaptor(DataList<T>& d) : Detail::IteratorAdaptor<T, NodeValueIterator<T>> {d} {};
260  };
261 
267  template<typename T>
268  struct NameAdaptor : Detail::IteratorAdaptor<T, NodeNameIterator> {
273  explicit NameAdaptor(DataList<T>& d) : Detail::IteratorAdaptor<T, NodeNameIterator> {d} {};
274  };
275 
276 } // namespace LLU
277 
278 #endif // LLU_CONTAINERS_DATALIST_H
LLU::MContainer< MArgumentType::DataStore >::cend
const_iterator cend() const
Proxy iterator past the last element of the DataStore.
Definition: Generic/DataStore.hpp:104
LLU::DataList::names
std::vector< std::string > names() const
Return a vector of DataList node names.
Definition: DataList.h:164
LLU::DataList::push_back
void push_back(value_type nodeData)
Add new node to the DataList.
Definition: DataList.h:210
LLU::MContainer< MArgumentType::DataStore >
MContainer specialization for DataStore, provides basic list interface for the underlying raw DataSto...
Definition: Generic/DataStore.hpp:23
LLU::DataList::cbegin
const_iterator cbegin() const
Get constant iterator at the beginning of underlying data.
Definition: DataList.h:93
LLU
Main namespace of LibraryLink Utilities.
Definition: Queue.h:13
LLU::MContainer
MContainer is an abstract class template for generic containers. Only specializations shall be used.
Definition: Base.hpp:245
TypedMArgument.h
LLU::GenericDataList
MContainer< MArgumentType::DataStore > GenericDataList
MContainer specialization for DataStore is called GenericDataList.
Definition: Generic/DataStore.hpp:17
LLU::MContainer< MArgumentType::DataStore >::push_back
void push_back(T nodeValue)
Add new nameless node at the end of the underlying DataStore.
Definition: Generic/DataStore.hpp:209
LibraryData.h
LLU::NameAdaptor::NameAdaptor
NameAdaptor(DataList< T > &d)
Create NameAdaptor to an existing DataList.
Definition: DataList.h:273
LLU::NameAdaptor
Iterator adaptor for DataList that makes begin() and end() return proxy iterators for node names.
Definition: DataList.h:268
DataStore.hpp
Definition and implementation of generic DataStore wrapper.
LLU::ValueAdaptor::ValueAdaptor
ValueAdaptor(DataList< T > &d)
Create ValueAdaptor to an existing DataList.
Definition: DataList.h:259
LLU::NodeNameIterator
Simple proxy input iterator that goes over a DataStore and returns node names when dereferenced.
Definition: DataList.hpp:87
LLU::DataList::nameBegin
name_iterator nameBegin() const
Get proxy iterator over node names (keys) pointing to the first node.
Definition: DataList.h:128
LLU::DataList::begin
iterator begin() const
Get iterator at the beginning of underlying data.
Definition: DataList.h:86
LLU::Ownership::Library
@ Library
The library (LLU) is responsible for managing the container's memory. Used for Manual passing and con...
LLU::DataList::valueEnd
value_iterator valueEnd() const
Get proxy iterator over node values pointing past the last node.
Definition: DataList.h:121
LLU::DataList::DataList
DataList(GenericDataList gds)
Create DataList wrapping around an existing GenericDataList.
Definition: DataList.h:180
LLU::DataList::end
iterator end() const
Get iterator after the end of underlying data.
Definition: DataList.h:100
ErrorManager.h
Definition of the ErrorManager class responsible for error registration and throwing.
LLU::DataList::value_type
T value_type
Value of a node is of type T.
Definition: DataList.h:51
LLU::DataList
Top-level wrapper over LibraryLink's DataStore.
Definition: DataList.h:36
LLU::DataList::iterator
NodeIterator< T > iterator
Default DataList iterator is NodeIterator<T>
Definition: DataList.h:39
DataList.hpp
Special iterators for DataLists. Iteration over keys, values, reversed iteration.
LLU::DataList::toVector
std::vector< DataNode< T > > toVector() const
Return a vector of DataList nodes.
Definition: DataList.h:172
LLU::DataList::values
std::vector< T > values() const
Return a vector of DataList node values.
Definition: DataList.h:156
LLU::DataList::nameEnd
name_iterator nameEnd() const
Get proxy iterator over node names (keys) pointing past the last node.
Definition: DataList.h:135
LLU::DataList::cend
const_iterator cend() const
Get constant reverse iterator after the end of underlying data.
Definition: DataList.h:107
LLU::DataList::clone
DataList clone() const
Clone this DataList, performing a deep copy of the underlying DataStore.
Definition: DataList.h:205
LLU::MContainer< MArgumentType::DataStore >::cbegin
const_iterator cbegin() const
Proxy iterator to the first element of the DataStore.
Definition: Generic/DataStore.hpp:99
LLU::MContainer< MArgumentType::DataStore >::front
DataStoreNode front() const
Get the first node of the DataStore.
Definition: Generic/DataStore.hpp:75
MArgument.h
Template class and utilities to work with MArgument in type-safe manner.
LLU::NodeIterator
Simple proxy input iterator that goes over a DataStore and returns proxy DataNodes when dereferenced.
Definition: DataList.hpp:46
LLU::NodeValueIterator
Simple proxy input iterator that goes over a DataStore and returns node values of requested type when...
Definition: DataList.hpp:138
LLU::DataList::valueBegin
value_iterator valueBegin() const
Get proxy iterator over node values pointing to the first node.
Definition: DataList.h:114
LLU::ValueAdaptor
Iterator adaptor for DataList that makes begin() and end() return proxy iterators for node values.
Definition: DataList.h:254