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/RNTupleTypes.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. Note that this constructor requires manual initialization of the item field
154 /// (Attach() and setting fItemSize)
156
157 std::unique_ptr<RFieldBase> CloneImpl(std::string_view newName) const final;
159 void GenerateColumns() final;
161
162 void ConstructValue(void *where) const final;
163 std::unique_ptr<RDeleter> GetDeleter() const final;
164
165 std::size_t AppendImpl(const void *from) final;
167
169
171
172public:
173 RProxiedCollectionField(std::string_view fieldName, std::string_view typeName);
176 ~RProxiedCollectionField() override = default;
177
178 std::vector<RValue> SplitValue(const RValue &value) const final;
179 size_t GetValueSize() const final { return fProxy->Sizeof(); }
180 size_t GetAlignment() const final { return alignof(std::max_align_t); }
182};
183
184////////////////////////////////////////////////////////////////////////////////
185/// Template specializations for classes with collection proxies
186////////////////////////////////////////////////////////////////////////////////
187
188template <typename T, typename = void>
189struct HasCollectionProxyMemberType : std::false_type {
190};
191template <typename T>
193 T, typename std::enable_if<std::is_same<typename T::IsCollectionProxy, std::true_type>::value>::type>
194 : std::true_type {
195};
196
197/* The point here is that we can only tell at run time if a class has an associated collection proxy.
198For compile time, in the first iteration of this PR we had an extra template argument that acted as a "tag" to
199differentiate the RField specialization for classes with an associated collection proxy (inherits
200RProxiedCollectionField) from the RField primary template definition (RClassField-derived), as in:
201```
202auto field = std::make_unique<RField<MyClass>>("klass");
203// vs
204auto otherField = std::make_unique<RField<MyClass, ROOT::TagIsCollectionProxy>>("klass");
205```
206
207That is convenient only for non-nested types, i.e. it doesn't work with, e.g. `RField<std::vector<MyClass>,
208ROOT::TagIsCollectionProxy>`, as the tag is not forwarded to the instantiation of the inner RField
209(that for the value type of the vector). The following two possible solutions were considered:
210- A wrapper type that helps to differentiate both cases.
211There we would have:
212```
213auto field = std::make_unique<RField<RProxiedCollection<MyClass>>>("klass"); // Using collection proxy
214```
215- A helper IsCollectionProxy<T> type, that can be used in a similar way to those in the `<type_traits>` header.
216We found this more convenient and is the implemented thing below. Here, classes can be marked as a
217collection proxy with either of the following two forms (whichever is more convenient for the user):
218```
219template <>
220struct IsCollectionProxy<MyClass> : std::true_type {};
221```
222or by adding a member type to the class as follows:
223```
224class MyClass {
225public:
226 using IsCollectionProxy = std::true_type;
227};
228```
229
230Of course, there is another possible solution which is to have a single RClassField that implements both
231the regular-class and the collection-proxy behaviors, and always chooses appropriately at run time.
232We found that less clean and probably has more overhead, as most probably it involves an additional branch + call
233in each of the member functions. */
234/// Helper type trait for marking classes as a collection proxy.
235/// This type trait must be set for collection proxy-based RNTuple fields created through MakeField<T>.
236template <typename T, typename = void>
239
240/// Classes behaving as a collection of elements that can be queried via the TVirtualCollectionProxy interface
241/// The use of a collection proxy for a particular class can be enabled via:
242/// ```
243/// namespace ROOT {
244/// template <> struct IsCollectionProxy<Classname> : std::true_type {};
245/// }
246/// ```
247/// Alternatively, this can be achieved by adding a member type to the class definition as follows:
248/// ```
249/// class Classname {
250/// public:
251/// using IsCollectionProxy = std::true_type;
252/// };
253/// ```
254template <typename T>
255class RField<T, typename std::enable_if<IsCollectionProxy<T>::value>::type> final : public RProxiedCollectionField {
256public:
257 static std::string TypeName() { return ROOT::Internal::GetRenormalizedTypeName(typeid(T)); }
258 RField(std::string_view name) : RProxiedCollectionField(name, TypeName())
259 {
260 static_assert(std::is_class<T>::value, "collection proxy unsupported for fundamental types");
261 }
262 RField(RField &&other) = default;
263 RField &operator=(RField &&other) = default;
265};
266
267////////////////////////////////////////////////////////////////////////////////
268/// Template specializations for C++ std::[unordered_][multi]map
269////////////////////////////////////////////////////////////////////////////////
270
271/// The generic field for a `std::map<KeyType, ValueType>` and `std::unordered_map<KeyType, ValueType>`
273public:
274 enum class EMapType {
275 kMap,
276 kUnorderedMap,
277 kMultiMap,
278 kUnorderedMultiMap
279 };
280 RMapField(std::string_view fieldName, EMapType mapType, std::unique_ptr<RFieldBase> itemField);
283 ~RMapField() override = default;
284};
285
286template <typename KeyT, typename ValueT>
287class RField<std::map<KeyT, ValueT>> final : public RMapField {
288public:
289 static std::string TypeName()
290 {
291 return "std::map<" + RField<KeyT>::TypeName() + "," + RField<ValueT>::TypeName() + ">";
292 }
293
294 explicit RField(std::string_view name)
295 : RMapField(name, EMapType::kMap, std::make_unique<RField<std::pair<KeyT, ValueT>>>("_0"))
296 {
297 }
298 RField(RField &&other) = default;
299 RField &operator=(RField &&other) = default;
301};
302
304class RField<std::unordered_map<KeyT, ValueT>> final : public RMapField {
305public:
306 static std::string TypeName()
307 {
308 return "std::unordered_map<" + RField<KeyT>::TypeName() + "," + RField<ValueT>::TypeName() + ">";
309 }
310
311 explicit RField(std::string_view name)
312 : RMapField(name, EMapType::kUnorderedMap, std::make_unique<RField<std::pair<KeyT, ValueT>>>("_0"))
313 {
314 }
315 RField(RField &&other) = default;
316 RField &operator=(RField &&other) = default;
318};
319
321class RField<std::multimap<KeyT, ValueT>> final : public RMapField {
322public:
323 static std::string TypeName()
324 {
325 return "std::multimap<" + RField<KeyT>::TypeName() + "," + RField<ValueT>::TypeName() + ">";
326 }
327
328 explicit RField(std::string_view name)
329 : RMapField(name, EMapType::kMultiMap, std::make_unique<RField<std::pair<KeyT, ValueT>>>("_0"))
330 {
331 }
332 RField(RField &&other) = default;
333 RField &operator=(RField &&other) = default;
335};
336
338class RField<std::unordered_multimap<KeyT, ValueT>> final : public RMapField {
339public:
340 static std::string TypeName()
341 {
342 return "std::unordered_multimap<" + RField<KeyT>::TypeName() + "," + RField<ValueT>::TypeName() + ">";
343 }
344
345 explicit RField(std::string_view name)
346 : RMapField(name, EMapType::kUnorderedMultiMap, std::make_unique<RField<std::pair<KeyT, ValueT>>>("_0"))
347 {
348 }
349 RField(RField &&other) = default;
350 RField &operator=(RField &&other) = default;
352};
353
354////////////////////////////////////////////////////////////////////////////////
355/// Template specializations for C++ std::[unordered_][multi]set
356////////////////////////////////////////////////////////////////////////////////
357
358/// The generic field for a `std::set<Type>` and `std::unordered_set<Type>`
360public:
361 enum class ESetType {
362 kSet,
363 kUnorderedSet,
364 kMultiSet,
365 kUnorderedMultiSet
366 };
367 RSetField(std::string_view fieldName, ESetType setType, std::unique_ptr<RFieldBase> itemField);
370 ~RSetField() override = default;
371};
372
373template <typename ItemT>
374class RField<std::set<ItemT>> final : public RSetField {
375public:
376 static std::string TypeName() { return "std::set<" + RField<ItemT>::TypeName() + ">"; }
377
378 explicit RField(std::string_view name) : RSetField(name, ESetType::kSet, 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::unordered_set<ItemT>> final : public RSetField {
386public:
387 static std::string TypeName() { return "std::unordered_set<" + RField<ItemT>::TypeName() + ">"; }
388
389 explicit RField(std::string_view name)
390 : RSetField(name, ESetType::kUnorderedSet, std::make_unique<RField<ItemT>>("_0"))
391 {
392 }
393 RField(RField &&other) = default;
394 RField &operator=(RField &&other) = default;
395 ~RField() final = default;
396};
397
399class RField<std::multiset<ItemT>> final : public RSetField {
400public:
401 static std::string TypeName() { return "std::multiset<" + RField<ItemT>::TypeName() + ">"; }
402
403 explicit RField(std::string_view name) : RSetField(name, ESetType::kMultiSet, std::make_unique<RField<ItemT>>("_0"))
404 {
405 }
406 RField(RField &&other) = default;
407 RField &operator=(RField &&other) = default;
408 ~RField() final = default;
409};
410
412class RField<std::unordered_multiset<ItemT>> final : public RSetField {
413public:
414 static std::string TypeName() { return "std::unordered_multiset<" + RField<ItemT>::TypeName() + ">"; }
415
416 explicit RField(std::string_view name)
417 : RSetField(name, ESetType::kUnorderedMultiSet, std::make_unique<RField<ItemT>>("_0"))
418 {
419 }
420 RField(RField &&other) = default;
421 RField &operator=(RField &&other) = default;
422 ~RField() final = default;
423};
424
425} // namespace ROOT
426
427#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
Binding & operator=(OUT(*fun)(void))
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:313
~RField() final=default
RField & operator=(RField &&other)=default
static std::string TypeName()
Definition RField.hxx:315
RField(std::string_view name)
Definition RField.hxx:316
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
void ReconcileOnDiskField(const RNTupleDescriptor &desc) final
For non-artificial fields, check compatibility of the in-memory field and the on-disk field.
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 GetRenormalizedTypeName(const std::string &metaNormalizedName)
Given a type name normalized by ROOT meta, renormalize it for RNTuple. E.g., insert std::prefix.
std::uint64_t NTupleSize_t
Integer type long enough to hold the maximum number of entries in a column.
@ kUnorderedMultiSet
Definition TClassEdit.h:105
@ kUnorderedMultiMap
Definition TClassEdit.h:107
Template specializations for classes with collection proxies.
Helper type trait for marking classes as a collection proxy.