Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RFieldProxiedCollection.hxx
Go to the documentation of this file.
1/// \file ROOT/RField/ProxiedCollection.hxx
2/// \ingroup NTuple ROOT7
3/// \author Jakob Blomer <jblomer@cern.ch>
4/// \date 2018-10-09
5/// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback
6/// is welcome!
7
8/*************************************************************************
9 * Copyright (C) 1995-2019, Rene Brun and Fons Rademakers. *
10 * All rights reserved. *
11 * *
12 * For the licensing terms see $ROOTSYS/LICENSE. *
13 * For the list of contributors see $ROOTSYS/README/CREDITS. *
14 *************************************************************************/
15
16#ifndef ROOT7_RField_ProxiedCollection
17#define ROOT7_RField_ProxiedCollection
18
19#ifndef ROOT7_RField
20#error "Please include RField.hxx!"
21#endif
22
23#include <ROOT/RFieldBase.hxx>
24#include <ROOT/RNTupleUtil.hxx>
25
27
28#include <iterator>
29#include <map>
30#include <set>
31#include <string>
32#include <string_view>
33#include <type_traits>
34#include <unordered_map>
35#include <unordered_set>
36#include <vector>
37
38namespace ROOT {
39namespace Experimental {
40
41namespace Detail {
42class RFieldVisitor;
43} // namespace Detail
44
45/// The field for a class representing a collection of elements via `TVirtualCollectionProxy`.
46/// Objects of such type behave as collections that can be accessed through the corresponding member functions in
47/// `TVirtualCollectionProxy`. For STL collections, these proxies are provided. Custom classes need to implement the
48/// corresponding member functions in `TVirtualCollectionProxy`. At a bare minimum, the user is required to provide an
49/// implementation for the following functions in `TVirtualCollectionProxy`: `HasPointers()`, `GetProperties()`,
50/// `GetValueClass()`, `GetType()`, `PushProxy()`, `PopProxy()`, `GetFunctionCreateIterators()`, `GetFunctionNext()`,
51/// and `GetFunctionDeleteTwoIterators()`.
52///
53/// The collection proxy for a given class can be set via `TClass::CopyCollectionProxy()`.
55protected:
56 /// Allows for iterating over the elements of a proxied collection. RCollectionIterableOnce avoids an additional
57 /// iterator copy (see `TVirtualCollectionProxy::GetFunctionCopyIterator`) and thus can only be iterated once.
59 public:
64 };
65 static RIteratorFuncs GetIteratorFuncs(TVirtualCollectionProxy *proxy, bool readFromDisk);
66
67 private:
68 class RIterator {
70 void *fIterator = nullptr;
71 void *fElementPtr = nullptr;
72
73 void Advance()
74 {
75 auto fnNext_Contig = [&]() {
76 // Array-backed collections (e.g. kSTLvector) directly use the pointer-to-iterator-data as a
77 // pointer-to-element, thus saving an indirection level (see documentation for TVirtualCollectionProxy)
78 auto &iter = reinterpret_cast<unsigned char *&>(fIterator), p = iter;
79 iter += fOwner.fStride;
80 return p;
81 };
83 }
84
85 public:
86 using iterator_category = std::forward_iterator_tag;
88 using difference_type = std::ptrdiff_t;
89 using pointer = void *;
90
91 RIterator(const RCollectionIterableOnce &owner) : fOwner(owner) {}
92 RIterator(const RCollectionIterableOnce &owner, void *iter) : fOwner(owner), fIterator(iter) { Advance(); }
94 {
95 Advance();
96 return *this;
97 }
98 pointer operator*() const { return fElementPtr; }
99 bool operator!=(const iterator &rh) const { return fElementPtr != rh.fElementPtr; }
100 bool operator==(const iterator &rh) const { return fElementPtr == rh.fElementPtr; }
101 };
102
104 const std::size_t fStride;
109
110 public:
111 /// Construct a `RCollectionIterableOnce` that iterates over `collection`. If elements are guaranteed to be
112 /// contiguous in memory (e.g. a vector), `stride` can be provided for faster iteration, i.e. the address of each
113 /// element is known given the base pointer.
114 RCollectionIterableOnce(void *collection, const RIteratorFuncs &ifuncs, TVirtualCollectionProxy *proxy,
115 std::size_t stride = 0U)
116 : fIFuncs(ifuncs), fStride(stride)
117 {
118 fIFuncs.fCreateIterators(collection, &fBegin, &fEnd, proxy);
119 }
121
122 RIterator begin() { return RIterator(*this, fBegin); }
123 RIterator end() { return fStride ? RIterator(*this, fEnd) : RIterator(*this); }
124 }; // class RCollectionIterableOnce
125
127 private:
128 std::shared_ptr<TVirtualCollectionProxy> fProxy;
129 std::unique_ptr<RDeleter> fItemDeleter;
130 std::size_t fItemSize = 0;
132
133 public:
134 explicit RProxiedCollectionDeleter(std::shared_ptr<TVirtualCollectionProxy> proxy) : fProxy(proxy) {}
135 RProxiedCollectionDeleter(std::shared_ptr<TVirtualCollectionProxy> proxy, std::unique_ptr<RDeleter> itemDeleter,
136 size_t itemSize)
137 : fProxy(proxy), fItemDeleter(std::move(itemDeleter)), fItemSize(itemSize)
138 {
139 fIFuncsWrite = RCollectionIterableOnce::GetIteratorFuncs(fProxy.get(), false /* readFromDisk */);
140 }
141 void operator()(void *objPtr, bool dtorOnly) final;
142 };
143
144 /// The collection proxy is needed by the deleters and thus defined as a shared pointer
145 std::shared_ptr<TVirtualCollectionProxy> fProxy;
148 /// Two sets of functions to operate on iterators, to be used depending on the access type. The direction preserves
149 /// the meaning from TVirtualCollectionProxy, i.e. read from disk / write to disk, respectively
152 std::size_t fItemSize;
154
155 /// Constructor used when the value type of the collection is not known in advance, i.e. in the case of custom
156 /// collections.
157 RProxiedCollectionField(std::string_view fieldName, std::string_view typeName, TClass *classp);
158 /// Constructor used when the value type of the collection is known in advance, e.g. in `RSetField`.
159 RProxiedCollectionField(std::string_view fieldName, std::string_view typeName,
160 std::unique_ptr<RFieldBase> itemField);
161
162protected:
163 std::unique_ptr<RFieldBase> CloneImpl(std::string_view newName) const final;
165 void GenerateColumns() final;
166 void GenerateColumns(const RNTupleDescriptor &desc) final;
167
168 void ConstructValue(void *where) const final;
169 std::unique_ptr<RDeleter> GetDeleter() const final;
170
171 std::size_t AppendImpl(const void *from) final;
172 void ReadGlobalImpl(NTupleSize_t globalIndex, void *to) final;
173
174 void CommitClusterImpl() final { fNWritten = 0; }
175
176public:
177 RProxiedCollectionField(std::string_view fieldName, std::string_view typeName);
180 ~RProxiedCollectionField() override = default;
181
182 std::vector<RValue> SplitValue(const RValue &value) const final;
183 size_t GetValueSize() const final { return fProxy->Sizeof(); }
184 size_t GetAlignment() const final { return alignof(std::max_align_t); }
185 void AcceptVisitor(Detail::RFieldVisitor &visitor) const final;
186 void GetCollectionInfo(NTupleSize_t globalIndex, RClusterIndex *collectionStart, ClusterSize_t *size) const
187 {
188 fPrincipalColumn->GetCollectionInfo(globalIndex, collectionStart, size);
189 }
190 void GetCollectionInfo(RClusterIndex clusterIndex, RClusterIndex *collectionStart, ClusterSize_t *size) const
191 {
192 fPrincipalColumn->GetCollectionInfo(clusterIndex, collectionStart, size);
193 }
194};
195
196////////////////////////////////////////////////////////////////////////////////
197/// Template specializations for classes with collection proxies
198////////////////////////////////////////////////////////////////////////////////
199
200template <typename T, typename = void>
201struct HasCollectionProxyMemberType : std::false_type {
202};
203template <typename T>
205 T, typename std::enable_if<std::is_same<typename T::IsCollectionProxy, std::true_type>::value>::type>
206 : std::true_type {
207};
208
209/// The point here is that we can only tell at run time if a class has an associated collection proxy.
210/// For compile time, in the first iteration of this PR we had an extra template argument that acted as a "tag" to
211/// differentiate the RField specialization for classes with an associated collection proxy (inherits
212/// `RProxiedCollectionField`) from the RField primary template definition (`RClassField`-derived), as in:
213/// ```
214/// auto field = std::make_unique<RField<MyClass>>("klass");
215/// // vs
216/// auto otherField = std::make_unique<RField<MyClass, ROOT::Experimental::TagIsCollectionProxy>>("klass");
217/// ```
218///
219/// That is convenient only for non-nested types, i.e. it doesn't work with, e.g. `RField<std::vector<MyClass>,
220/// ROOT::Experimental::TagIsCollectionProxy>`, as the tag is not forwarded to the instantiation of the inner RField
221/// (that for the value type of the vector). The following two possible solutions were considered:
222/// - A wrapper type (much like `ntuple/v7/inc/ROOT/RNTupleUtil.hxx:49`), that helps to differentiate both cases.
223/// There we would have:
224/// ```
225/// auto field = std::make_unique<RField<RProxiedCollection<MyClass>>>("klass"); // Using collection proxy
226/// ```
227/// - A helper `IsCollectionProxy<T>` type, that can be used in a similar way to those in the `<type_traits>` header.
228/// We found this more convenient and is the implemented thing below. Here, classes can be marked as a
229/// collection proxy with either of the following two forms (whichever is more convenient for the user):
230/// ```
231/// template <>
232/// struct IsCollectionProxy<MyClass> : std::true_type {};
233/// ```
234/// or by adding a member type to the class as follows:
235/// ```
236/// class MyClass {
237/// public:
238/// using IsCollectionProxy = std::true_type;
239/// };
240/// ```
241///
242/// Of course, there is another possible solution which is to have a single `RClassField` that implements both
243/// the regular-class and the collection-proxy behaviors, and always chooses appropriately at run time.
244/// We found that less clean and probably has more overhead, as most probably it involves an additional branch + call
245/// in each of the member functions.
246template <typename T, typename = void>
248};
249
250/// Classes behaving as a collection of elements that can be queried via the `TVirtualCollectionProxy` interface
251/// The use of a collection proxy for a particular class can be enabled via:
252/// ```
253/// namespace ROOT::Experimental {
254/// template <> struct IsCollectionProxy<Classname> : std::true_type {};
255/// }
256/// ```
257/// Alternatively, this can be achieved by adding a member type to the class definition as follows:
258/// ```
259/// class Classname {
260/// public:
261/// using IsCollectionProxy = std::true_type;
262/// };
263/// ```
264template <typename T>
265class RField<T, typename std::enable_if<IsCollectionProxy<T>::value>::type> final : public RProxiedCollectionField {
266public:
267 static std::string TypeName() { return ROOT::Internal::GetDemangledTypeName(typeid(T)); }
268 RField(std::string_view name) : RProxiedCollectionField(name, TypeName())
269 {
270 static_assert(std::is_class<T>::value, "collection proxy unsupported for fundamental types");
271 }
272 RField(RField &&other) = default;
273 RField &operator=(RField &&other) = default;
274 ~RField() final = default;
275};
276
277////////////////////////////////////////////////////////////////////////////////
278/// Template specializations for C++ std::[unordered_][multi]map
279////////////////////////////////////////////////////////////////////////////////
280
281/// The generic field for a std::map<KeyType, ValueType> and std::unordered_map<KeyType, ValueType>
283public:
284 RMapField(std::string_view fieldName, std::string_view typeName, std::unique_ptr<RFieldBase> itemField);
285 RMapField(RMapField &&other) = default;
286 RMapField &operator=(RMapField &&other) = default;
287 ~RMapField() override = default;
288};
289
290template <typename KeyT, typename ValueT>
291class RField<std::map<KeyT, ValueT>> final : public RMapField {
292public:
293 static std::string TypeName()
294 {
295 return "std::map<" + RField<KeyT>::TypeName() + "," + RField<ValueT>::TypeName() + ">";
296 }
297
298 explicit RField(std::string_view name)
299 : RMapField(name, TypeName(), std::make_unique<RField<std::pair<KeyT, ValueT>>>("_0"))
300 {
301 }
302 RField(RField &&other) = default;
303 RField &operator=(RField &&other) = default;
304 ~RField() final = default;
305};
306
307template <typename KeyT, typename ValueT>
308class RField<std::unordered_map<KeyT, ValueT>> final : public RMapField {
309public:
310 static std::string TypeName()
311 {
312 return "std::unordered_map<" + RField<KeyT>::TypeName() + "," + RField<ValueT>::TypeName() + ">";
313 }
314
315 explicit RField(std::string_view name)
316 : RMapField(name, TypeName(), std::make_unique<RField<std::pair<KeyT, ValueT>>>("_0"))
317 {
318 }
319 RField(RField &&other) = default;
320 RField &operator=(RField &&other) = default;
321 ~RField() final = default;
322};
323
324template <typename KeyT, typename ValueT>
325class RField<std::multimap<KeyT, ValueT>> final : public RMapField {
326public:
327 static std::string TypeName()
328 {
329 return "std::multimap<" + RField<KeyT>::TypeName() + "," + RField<ValueT>::TypeName() + ">";
330 }
331
332 explicit RField(std::string_view name)
333 : RMapField(name, TypeName(), std::make_unique<RField<std::pair<KeyT, ValueT>>>("_0"))
334 {
335 }
336 RField(RField &&other) = default;
337 RField &operator=(RField &&other) = default;
338 ~RField() final = default;
339};
340
341template <typename KeyT, typename ValueT>
342class RField<std::unordered_multimap<KeyT, ValueT>> final : public RMapField {
343public:
344 static std::string TypeName()
345 {
346 return "std::unordered_multimap<" + RField<KeyT>::TypeName() + "," + RField<ValueT>::TypeName() + ">";
347 }
348
349 explicit RField(std::string_view name)
350 : RMapField(name, TypeName(), std::make_unique<RField<std::pair<KeyT, ValueT>>>("_0"))
351 {
352 }
353 RField(RField &&other) = default;
354 RField &operator=(RField &&other) = default;
355 ~RField() final = default;
356};
357
358////////////////////////////////////////////////////////////////////////////////
359/// Template specializations for C++ std::[unordered_][multi]set
360////////////////////////////////////////////////////////////////////////////////
361
362/// The generic field for a std::set<Type> and std::unordered_set<Type>
364public:
365 RSetField(std::string_view fieldName, std::string_view typeName, std::unique_ptr<RFieldBase> itemField);
366 RSetField(RSetField &&other) = default;
367 RSetField &operator=(RSetField &&other) = default;
368 ~RSetField() override = default;
369};
370
371template <typename ItemT>
372class RField<std::set<ItemT>> final : public RSetField {
373public:
374 static std::string TypeName() { return "std::set<" + RField<ItemT>::TypeName() + ">"; }
375
376 explicit RField(std::string_view name) : RSetField(name, TypeName(), std::make_unique<RField<ItemT>>("_0")) {}
377 RField(RField &&other) = default;
378 RField &operator=(RField &&other) = default;
379 ~RField() final = default;
380};
381
382template <typename ItemT>
383class RField<std::unordered_set<ItemT>> final : public RSetField {
384public:
385 static std::string TypeName() { return "std::unordered_set<" + RField<ItemT>::TypeName() + ">"; }
386
387 explicit RField(std::string_view name) : RSetField(name, TypeName(), std::make_unique<RField<ItemT>>("_0")) {}
388 RField(RField &&other) = default;
389 RField &operator=(RField &&other) = default;
390 ~RField() final = default;
391};
392
393template <typename ItemT>
394class RField<std::multiset<ItemT>> final : public RSetField {
395public:
396 static std::string TypeName() { return "std::multiset<" + RField<ItemT>::TypeName() + ">"; }
397
398 explicit RField(std::string_view name) : RSetField(name, TypeName(), std::make_unique<RField<ItemT>>("_0")) {}
399 RField(RField &&other) = default;
400 RField &operator=(RField &&other) = default;
401 ~RField() final = default;
402};
403
404template <typename ItemT>
405class RField<std::unordered_multiset<ItemT>> final : public RSetField {
406public:
407 static std::string TypeName() { return "std::unordered_multiset<" + RField<ItemT>::TypeName() + ">"; }
408
409 explicit RField(std::string_view name) : RSetField(name, TypeName(), std::make_unique<RField<ItemT>>("_0")) {}
410 RField(RField &&other) = default;
411 RField &operator=(RField &&other) = default;
412 ~RField() final = default;
413};
414
415} // namespace Experimental
416} // namespace ROOT
417
418#endif
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
char name[80]
Definition TGX11.cxx:110
Abstract base class for classes implementing the visitor design pattern.
void GetCollectionInfo(const NTupleSize_t globalIndex, RClusterIndex *collectionStart, ClusterSize_t *collectionSize)
For offset columns only, look at the two adjacent values that define a collection's coordinates.
Definition RColumn.hxx:275
Addresses a column element or field item relative to a particular cluster, instead of a global NTuple...
Some fields have multiple possible column representations, e.g.
A functor to release the memory acquired by CreateValue (memory and constructor).
Points to an object with RNTuple I/O support and keeps a pointer to the corresponding field.
A field translates read and write calls from/to underlying columns to/from tree values.
Internal::RColumn * fPrincipalColumn
All fields that have columns have a distinct main column.
Classes with dictionaries that can be inspected by TClass.
Definition RField.hxx:241
RField & operator=(RField &&other)=default
static std::string TypeName()
Definition RField.hxx:243
Template specializations for C++ std::[unordered_][multi]map.
RMapField(RMapField &&other)=default
~RMapField() override=default
RMapField & operator=(RMapField &&other)=default
The on-storage meta-data of an ntuple.
Allows for iterating over the elements of a proxied collection.
unsigned char fEndSmallBuf[TVirtualCollectionProxy::fgIteratorArenaSize]
unsigned char fBeginSmallBuf[TVirtualCollectionProxy::fgIteratorArenaSize]
static RIteratorFuncs GetIteratorFuncs(TVirtualCollectionProxy *proxy, bool readFromDisk)
Definition RField.cxx:2301
RCollectionIterableOnce(void *collection, const RIteratorFuncs &ifuncs, TVirtualCollectionProxy *proxy, std::size_t stride=0U)
Construct a RCollectionIterableOnce that iterates over collection.
RProxiedCollectionDeleter(std::shared_ptr< TVirtualCollectionProxy > proxy, std::unique_ptr< RDeleter > itemDeleter, size_t itemSize)
RProxiedCollectionDeleter(std::shared_ptr< TVirtualCollectionProxy > proxy)
The field for a class representing a collection of elements via TVirtualCollectionProxy.
size_t GetValueSize() const final
The number of bytes taken by a value of the appropriate type.
size_t GetAlignment() const final
As a rule of thumb, the alignment is equal to the size of the type.
std::size_t AppendImpl(const void *from) final
Operations on values of complex types, e.g.
Definition RField.cxx:2389
void GetCollectionInfo(RClusterIndex clusterIndex, RClusterIndex *collectionStart, ClusterSize_t *size) const
std::shared_ptr< TVirtualCollectionProxy > fProxy
The collection proxy is needed by the deleters and thus defined as a shared pointer.
RCollectionIterableOnce::RIteratorFuncs fIFuncsRead
Two sets of functions to operate on iterators, to be used depending on the access type.
void AcceptVisitor(Detail::RFieldVisitor &visitor) const final
Definition RField.cxx:2483
void ReadGlobalImpl(NTupleSize_t globalIndex, void *to) final
Definition RField.cxx:2405
std::unique_ptr< RDeleter > GetDeleter() const final
Definition RField.cxx:2449
const RColumnRepresentations & GetColumnRepresentations() const final
Implementations in derived classes should return a static RColumnRepresentations object.
Definition RField.cxx:2425
std::unique_ptr< RFieldBase > CloneImpl(std::string_view newName) const final
Called by Clone(), which additionally copies the on-disk ID.
Definition RField.cxx:2382
RProxiedCollectionField(RProxiedCollectionField &&other)=default
void GetCollectionInfo(NTupleSize_t globalIndex, RClusterIndex *collectionStart, ClusterSize_t *size) const
void GenerateColumns() final
Implementations in derived classes should create the backing columns corresponsing to the field type ...
Definition RField.cxx:2433
RCollectionIterableOnce::RIteratorFuncs fIFuncsWrite
void ConstructValue(void *where) const final
Constructs value in a given location of size at least GetValueSize(). Called by the base class' Creat...
Definition RField.cxx:2443
std::vector< RValue > SplitValue(const RValue &value) const final
Creates the list of direct child values given a value for this field.
Definition RField.cxx:2471
RProxiedCollectionField & operator=(RProxiedCollectionField &&other)=default
Template specializations for C++ std::[unordered_][multi]set.
RSetField(RSetField &&other)=default
RSetField & operator=(RSetField &&other)=default
~RSetField() override=default
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:81
Defines a common interface to inspect/change the contents of an object that represents a collection.
void(* CreateIterators_t)(void *collection, void **begin_arena, void **end_arena, TVirtualCollectionProxy *proxy)
*begin_arena and *end_arena should contain the location of a memory arena of size fgIteratorArenaSize...
void *(* Next_t)(void *iter, const void *end)
iter and end should be pointers to an iterator to be incremented and an iterator that points to the e...
void(* DeleteTwoIterators_t)(void *begin, void *end)
static const Int_t fgIteratorArenaSize
The size of a small buffer that can be allocated on the stack to store iterator-specific information.
std::uint64_t NTupleSize_t
Integer type long enough to hold the maximum number of entries in a column.
std::string GetDemangledTypeName(const std::type_info &t)
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
Template specializations for classes with collection proxies.
The point here is that we can only tell at run time if a class has an associated collection proxy.
Wrap the integer in a struct in order to avoid template specialization clash with std::uint64_t.