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
3/// \author Jakob Blomer <jblomer@cern.ch>
4/// \date 2018-10-09
5
6/*************************************************************************
7 * Copyright (C) 1995-2019, Rene Brun and Fons Rademakers. *
8 * All rights reserved. *
9 * *
10 * For the licensing terms see $ROOTSYS/LICENSE. *
11 * For the list of contributors see $ROOTSYS/README/CREDITS. *
12 *************************************************************************/
13
14#ifndef ROOT_RField_ProxiedCollection
15#define ROOT_RField_ProxiedCollection
16
17#ifndef ROOT_RField
18#error "Please include RField.hxx!"
19#endif
20
21#include <ROOT/RFieldBase.hxx>
22#include <ROOT/RNTupleUtil.hxx>
23
25
26#include <iterator>
27#include <map>
28#include <set>
29#include <string>
30#include <string_view>
31#include <type_traits>
32#include <unordered_map>
33#include <unordered_set>
34#include <vector>
35
36namespace ROOT {
37
38namespace Detail {
39class RFieldVisitor;
40} // namespace Detail
41
42/// The field for a class representing a collection of elements via TVirtualCollectionProxy.
43/// Objects of such type behave as collections that can be accessed through the corresponding member functions in
44/// TVirtualCollectionProxy. For STL collections, these proxies are provided. Custom classes need to implement the
45/// corresponding member functions in TVirtualCollectionProxy. At a bare minimum, the user is required to provide an
46/// implementation for the following functions in TVirtualCollectionProxy: HasPointers(), GetProperties(),
47/// GetValueClass(), GetType(), PushProxy(), PopProxy(), GetFunctionCreateIterators(), GetFunctionNext(),
48/// and GetFunctionDeleteTwoIterators().
49///
50/// The collection proxy for a given class can be set via TClass::CopyCollectionProxy().
52protected:
53 /// Allows for iterating over the elements of a proxied collection. RCollectionIterableOnce avoids an additional
54 /// iterator copy (see TVirtualCollectionProxy::GetFunctionCopyIterator) and thus can only be iterated once.
56 public:
63
64 private:
65 class RIterator {
67 void *fIterator = nullptr;
68 void *fElementPtr = nullptr;
69
70 void Advance()
71 {
72 auto fnNext_Contig = [&]() {
73 // Array-backed collections (e.g. `kSTLvector`) directly use the pointer-to-iterator-data as a
74 // pointer-to-element, thus saving an indirection level (see documentation for TVirtualCollectionProxy)
75 auto &iter = reinterpret_cast<unsigned char *&>(fIterator), p = iter;
76 iter += fOwner.fStride;
77 return p;
78 };
79 fElementPtr = fOwner.fStride ? fnNext_Contig() : fOwner.fIFuncs.fNext(fIterator, fOwner.fEnd);
80 }
81
82 public:
83 using iterator_category = std::forward_iterator_tag;
85 using difference_type = std::ptrdiff_t;
86 using pointer = void *;
87
88 RIterator(const RCollectionIterableOnce &owner) : fOwner(owner) {}
89 RIterator(const RCollectionIterableOnce &owner, void *iter) : fOwner(owner), fIterator(iter) { Advance(); }
91 {
92 Advance();
93 return *this;
94 }
95 pointer operator*() const { return fElementPtr; }
96 bool operator!=(const iterator &rh) const { return fElementPtr != rh.fElementPtr; }
97 bool operator==(const iterator &rh) const { return fElementPtr == rh.fElementPtr; }
98 };
99
101 const std::size_t fStride;
106
107 public:
108 /// Construct a RCollectionIterableOnce that iterates over `collection`. If elements are guaranteed to be
109 /// contiguous in memory (e.g. a vector), `stride` can be provided for faster iteration, i.e. the address of each
110 /// element is known given the base pointer.
112 std::size_t stride = 0U)
114 {
115 fIFuncs.fCreateIterators(collection, &fBegin, &fEnd, proxy);
116 }
117 ~RCollectionIterableOnce() { fIFuncs.fDeleteTwoIterators(fBegin, fEnd); }
118
119 RIterator begin() { return RIterator(*this, fBegin); }
120 RIterator end() { return fStride ? RIterator(*this, fEnd) : RIterator(*this); }
121 }; // class RCollectionIterableOnce
122
124 private:
125 std::shared_ptr<TVirtualCollectionProxy> fProxy;
126 std::unique_ptr<RDeleter> fItemDeleter;
127 std::size_t fItemSize = 0;
129
130 public:
131 explicit RProxiedCollectionDeleter(std::shared_ptr<TVirtualCollectionProxy> proxy) : fProxy(proxy) {}
132 RProxiedCollectionDeleter(std::shared_ptr<TVirtualCollectionProxy> proxy, std::unique_ptr<RDeleter> itemDeleter,
133 size_t itemSize)
135 {
136 fIFuncsWrite = RCollectionIterableOnce::GetIteratorFuncs(fProxy.get(), false /* readFromDisk */);
137 }
138 void operator()(void *objPtr, bool dtorOnly) final;
139 };
140
141 /// The collection proxy is needed by the deleters and thus defined as a shared pointer
142 std::shared_ptr<TVirtualCollectionProxy> fProxy;
145 /// Two sets of functions to operate on iterators, to be used depending on the access type. The direction preserves
146 /// the meaning from TVirtualCollectionProxy, i.e. read from disk / write to disk, respectively
149 std::size_t fItemSize;
151
152 /// Constructor used when the value type of the collection is not known in advance, i.e. in the case of custom
153 /// collections.
155 /// Constructor used when the value type of the collection is known in advance, e.g. in RSetField.
156 RProxiedCollectionField(std::string_view fieldName, std::string_view typeName,
157 std::unique_ptr<RFieldBase> itemField);
158
159protected:
160 std::unique_ptr<RFieldBase> CloneImpl(std::string_view newName) const final;
162 void GenerateColumns() final;
164
165 void ConstructValue(void *where) const final;
166 std::unique_ptr<RDeleter> GetDeleter() const final;
167
168 std::size_t AppendImpl(const void *from) final;
170
172
173public:
174 RProxiedCollectionField(std::string_view fieldName, std::string_view typeName);
177 ~RProxiedCollectionField() override = default;
178
179 std::vector<RValue> SplitValue(const RValue &value) const final;
180 size_t GetValueSize() const final { return fProxy->Sizeof(); }
181 size_t GetAlignment() const final { return alignof(std::max_align_t); }
183};
184
185////////////////////////////////////////////////////////////////////////////////
186/// Template specializations for classes with collection proxies
187////////////////////////////////////////////////////////////////////////////////
188
189template <typename T, typename = void>
190struct HasCollectionProxyMemberType : std::false_type {
191};
192template <typename T>
194 T, typename std::enable_if<std::is_same<typename T::IsCollectionProxy, std::true_type>::value>::type>
195 : std::true_type {
196};
197
198/* The point here is that we can only tell at run time if a class has an associated collection proxy.
199For compile time, in the first iteration of this PR we had an extra template argument that acted as a "tag" to
200differentiate the RField specialization for classes with an associated collection proxy (inherits
201RProxiedCollectionField) from the RField primary template definition (RClassField-derived), as in:
202```
203auto field = std::make_unique<RField<MyClass>>("klass");
204// vs
205auto otherField = std::make_unique<RField<MyClass, ROOT::Experimental::TagIsCollectionProxy>>("klass");
206```
207
208That is convenient only for non-nested types, i.e. it doesn't work with, e.g. `RField<std::vector<MyClass>,
209ROOT::Experimental::TagIsCollectionProxy>`, as the tag is not forwarded to the instantiation of the inner RField
210(that for the value type of the vector). The following two possible solutions were considered:
211- A wrapper type that helps to differentiate both cases.
212There we would have:
213```
214auto field = std::make_unique<RField<RProxiedCollection<MyClass>>>("klass"); // Using collection proxy
215```
216- A helper IsCollectionProxy<T> type, that can be used in a similar way to those in the `<type_traits>` header.
217We found this more convenient and is the implemented thing below. Here, classes can be marked as a
218collection proxy with either of the following two forms (whichever is more convenient for the user):
219```
220template <>
221struct IsCollectionProxy<MyClass> : std::true_type {};
222```
223or by adding a member type to the class as follows:
224```
225class MyClass {
226public:
227 using IsCollectionProxy = std::true_type;
228};
229```
230
231Of course, there is another possible solution which is to have a single RClassField that implements both
232the regular-class and the collection-proxy behaviors, and always chooses appropriately at run time.
233We found that less clean and probably has more overhead, as most probably it involves an additional branch + call
234in each of the member functions. */
235/// Helper type trait for marking classes as a collection proxy.
236/// This type trait must be set for collection proxy-based RNTuple fields created through MakeField<T>.
237template <typename T, typename = void>
240
241/// Classes behaving as a collection of elements that can be queried via the TVirtualCollectionProxy interface
242/// The use of a collection proxy for a particular class can be enabled via:
243/// ```
244/// namespace ROOT::Experimental {
245/// template <> struct IsCollectionProxy<Classname> : std::true_type {};
246/// }
247/// ```
248/// Alternatively, this can be achieved by adding a member type to the class definition as follows:
249/// ```
250/// class Classname {
251/// public:
252/// using IsCollectionProxy = std::true_type;
253/// };
254/// ```
255template <typename T>
256class RField<T, typename std::enable_if<IsCollectionProxy<T>::value>::type> final : public RProxiedCollectionField {
257public:
258 static std::string TypeName() { return ROOT::Internal::GetRenormalizedDemangledTypeName(typeid(T)); }
259 RField(std::string_view name) : RProxiedCollectionField(name, TypeName())
260 {
261 static_assert(std::is_class<T>::value, "collection proxy unsupported for fundamental types");
262 }
263 RField(RField &&other) = default;
264 RField &operator=(RField &&other) = default;
266};
267
268////////////////////////////////////////////////////////////////////////////////
269/// Template specializations for C++ std::[unordered_][multi]map
270////////////////////////////////////////////////////////////////////////////////
271
272/// The generic field for a `std::map<KeyType, ValueType>` and `std::unordered_map<KeyType, ValueType>`
274public:
275 RMapField(std::string_view fieldName, std::string_view typeName, std::unique_ptr<RFieldBase> itemField);
278 ~RMapField() override = default;
279};
280
281template <typename KeyT, typename ValueT>
282class RField<std::map<KeyT, ValueT>> final : public RMapField {
283public:
284 static std::string TypeName()
285 {
286 return "std::map<" + RField<KeyT>::TypeName() + "," + RField<ValueT>::TypeName() + ">";
287 }
288
289 explicit RField(std::string_view name)
290 : RMapField(name, TypeName(), std::make_unique<RField<std::pair<KeyT, ValueT>>>("_0"))
291 {
292 }
293 RField(RField &&other) = default;
294 RField &operator=(RField &&other) = default;
296};
297
299class RField<std::unordered_map<KeyT, ValueT>> final : public RMapField {
300public:
301 static std::string TypeName()
302 {
303 return "std::unordered_map<" + RField<KeyT>::TypeName() + "," + RField<ValueT>::TypeName() + ">";
304 }
305
306 explicit RField(std::string_view name)
307 : RMapField(name, TypeName(), std::make_unique<RField<std::pair<KeyT, ValueT>>>("_0"))
308 {
309 }
310 RField(RField &&other) = default;
311 RField &operator=(RField &&other) = default;
313};
314
316class RField<std::multimap<KeyT, ValueT>> final : public RMapField {
317public:
318 static std::string TypeName()
319 {
320 return "std::multimap<" + RField<KeyT>::TypeName() + "," + RField<ValueT>::TypeName() + ">";
321 }
322
323 explicit RField(std::string_view name)
324 : RMapField(name, TypeName(), std::make_unique<RField<std::pair<KeyT, ValueT>>>("_0"))
325 {
326 }
327 RField(RField &&other) = default;
328 RField &operator=(RField &&other) = default;
330};
331
333class RField<std::unordered_multimap<KeyT, ValueT>> final : public RMapField {
334public:
335 static std::string TypeName()
336 {
337 return "std::unordered_multimap<" + RField<KeyT>::TypeName() + "," + RField<ValueT>::TypeName() + ">";
338 }
339
340 explicit RField(std::string_view name)
341 : RMapField(name, TypeName(), std::make_unique<RField<std::pair<KeyT, ValueT>>>("_0"))
342 {
343 }
344 RField(RField &&other) = default;
345 RField &operator=(RField &&other) = default;
347};
348
349////////////////////////////////////////////////////////////////////////////////
350/// Template specializations for C++ std::[unordered_][multi]set
351////////////////////////////////////////////////////////////////////////////////
352
353/// The generic field for a `std::set<Type>` and `std::unordered_set<Type>`
355public:
356 RSetField(std::string_view fieldName, std::string_view typeName, std::unique_ptr<RFieldBase> itemField);
359 ~RSetField() override = default;
360};
361
362template <typename ItemT>
363class RField<std::set<ItemT>> final : public RSetField {
364public:
365 static std::string TypeName() { return "std::set<" + RField<ItemT>::TypeName() + ">"; }
366
367 explicit RField(std::string_view name) : RSetField(name, TypeName(), std::make_unique<RField<ItemT>>("_0")) {}
368 RField(RField &&other) = default;
369 RField &operator=(RField &&other) = default;
370 ~RField() final = default;
371};
372
374class RField<std::unordered_set<ItemT>> final : public RSetField {
375public:
376 static std::string TypeName() { return "std::unordered_set<" + RField<ItemT>::TypeName() + ">"; }
377
378 explicit RField(std::string_view name) : RSetField(name, TypeName(), std::make_unique<RField<ItemT>>("_0")) {}
379 RField(RField &&other) = default;
380 RField &operator=(RField &&other) = default;
381 ~RField() final = default;
382};
383
385class RField<std::multiset<ItemT>> final : public RSetField {
386public:
387 static std::string TypeName() { return "std::multiset<" + RField<ItemT>::TypeName() + ">"; }
388
389 explicit RField(std::string_view name) : RSetField(name, TypeName(), std::make_unique<RField<ItemT>>("_0")) {}
390 RField(RField &&other) = default;
391 RField &operator=(RField &&other) = default;
392 ~RField() final = default;
393};
394
396class RField<std::unordered_multiset<ItemT>> final : public RSetField {
397public:
398 static std::string TypeName() { return "std::unordered_multiset<" + RField<ItemT>::TypeName() + ">"; }
399
400 explicit RField(std::string_view name) : RSetField(name, TypeName(), std::make_unique<RField<ItemT>>("_0")) {}
401 RField(RField &&other) = default;
402 RField &operator=(RField &&other) = default;
403 ~RField() final = default;
404};
405
406} // namespace ROOT
407
408#endif
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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
void operator=(const TProof &)
Abstract base class for classes implementing the visitor design pattern.
The in-memory representation of a 32bit or 64bit on-disk index column.
The list of column representations a field can have.
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.
Classes with dictionaries that can be inspected by TClass.
Definition RField.hxx:285
~RField() final=default
RField & operator=(RField &&other)=default
static std::string TypeName()
Definition RField.hxx:287
RField(std::string_view name)
Definition RField.hxx:288
Template specializations for C++ std::[unordered_][multi]map.
RMapField(RMapField &&other)=default
~RMapField() override=default
RMapField & operator=(RMapField &&other)=default
The on-storage metadata of an RNTuple.
Allows for iterating over the elements of a proxied collection.
unsigned char fEndSmallBuf[TVirtualCollectionProxy::fgIteratorArenaSize]
RCollectionIterableOnce(void *collection, const RIteratorFuncs &ifuncs, TVirtualCollectionProxy *proxy, std::size_t stride=0U)
Construct a RCollectionIterableOnce that iterates over collection.
static RIteratorFuncs GetIteratorFuncs(TVirtualCollectionProxy *proxy, bool readFromDisk)
unsigned char fBeginSmallBuf[TVirtualCollectionProxy::fgIteratorArenaSize]
RProxiedCollectionDeleter(std::shared_ptr< TVirtualCollectionProxy > proxy, std::unique_ptr< RDeleter > itemDeleter, size_t itemSize)
RProxiedCollectionDeleter(std::shared_ptr< TVirtualCollectionProxy > proxy)
void operator()(void *objPtr, bool dtorOnly) final
The field for a class representing a collection of elements via TVirtualCollectionProxy.
RProxiedCollectionField & operator=(RProxiedCollectionField &&other)=default
void GenerateColumns() final
Implementations in derived classes should create the backing columns corresponding to the field type ...
std::unique_ptr< RFieldBase > CloneImpl(std::string_view newName) const final
Called by Clone(), which additionally copies the on-disk ID.
size_t GetValueSize() const final
The number of bytes taken by a value of the appropriate type.
~RProxiedCollectionField() override=default
RProxiedCollectionField(RProxiedCollectionField &&other)=default
const RColumnRepresentations & GetColumnRepresentations() const final
Implementations in derived classes should return a static RColumnRepresentations object.
void ConstructValue(void *where) const final
Constructs value in a given location of size at least GetValueSize(). Called by the base class' Creat...
RProxiedCollectionField(std::string_view fieldName, TClass *classp)
Constructor used when the value type of the collection is not known in advance, i....
RCollectionIterableOnce::RIteratorFuncs fIFuncsWrite
ROOT::Internal::RColumnIndex fNWritten
void AcceptVisitor(ROOT::Detail::RFieldVisitor &visitor) const final
RCollectionIterableOnce::RIteratorFuncs fIFuncsRead
Two sets of functions to operate on iterators, to be used depending on the access type.
std::shared_ptr< TVirtualCollectionProxy > fProxy
The collection proxy is needed by the deleters and thus defined as a shared pointer.
void ReadGlobalImpl(ROOT::NTupleSize_t globalIndex, void *to) final
std::size_t AppendImpl(const void *from) final
Operations on values of complex types, e.g.
std::unique_ptr< RDeleter > GetDeleter() const final
size_t GetAlignment() const final
As a rule of thumb, the alignment is equal to the size of the type.
std::vector< RValue > SplitValue(const RValue &value) const final
Creates the list of direct child values given an existing value for this field.
Template specializations for C++ std::[unordered_][multi]set.
RSetField & operator=(RSetField &&other)=default
~RSetField() override=default
RSetField(RSetField &&other)=default
TClass instances represent classes, structs and namespaces in the ROOT type system.
Definition TClass.h:84
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::string GetRenormalizedDemangledTypeName(const std::type_info &ti)
Given a type info ask ROOT meta to demangle it, then renormalize the resulting type name for RNTuple.
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...
std::uint64_t NTupleSize_t
Integer type long enough to hold the maximum number of entries in a column.
Template specializations for classes with collection proxies.
Helper type trait for marking classes as a collection proxy.