Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RFieldProxiedCollection.hxx
Go to the documentation of this file.
1/// \file ROOT/RField/ProxiedCollection.hxx
2/// \author Jakob Blomer <jblomer@cern.ch>
3/// \date 2018-10-09
4
5/*************************************************************************
6 * Copyright (C) 1995-2019, Rene Brun and Fons Rademakers. *
7 * All rights reserved. *
8 * *
9 * For the licensing terms see $ROOTSYS/LICENSE. *
10 * For the list of contributors see $ROOTSYS/README/CREDITS. *
11 *************************************************************************/
12
13#ifndef ROOT_RField_ProxiedCollection
14#define ROOT_RField_ProxiedCollection
15
16#ifndef ROOT_RField
17#error "Please include RField.hxx!"
18#endif
19
20#include <ROOT/RFieldBase.hxx>
21#include <ROOT/RNTupleTypes.hxx>
22
24
25#include <iterator>
26#include <map>
27#include <set>
28#include <string>
29#include <string_view>
30#include <type_traits>
31#include <unordered_map>
32#include <unordered_set>
33#include <vector>
34
35namespace ROOT {
36
37namespace Detail {
38class RFieldVisitor;
39} // namespace Detail
40
41/// The field for a class representing a collection of elements via TVirtualCollectionProxy.
42/// Objects of such type behave as collections that can be accessed through the corresponding member functions in
43/// TVirtualCollectionProxy. For STL collections, these proxies are provided. Custom classes need to implement the
44/// corresponding member functions in TVirtualCollectionProxy. At a bare minimum, the user is required to provide an
45/// implementation for the following functions in TVirtualCollectionProxy: HasPointers(), GetProperties(),
46/// GetValueClass(), GetType(), PushProxy(), PopProxy(), GetFunctionCreateIterators(), GetFunctionNext(),
47/// and GetFunctionDeleteTwoIterators().
48///
49/// The collection proxy for a given class can be set via TClass::CopyCollectionProxy().
51protected:
52 /// Allows for iterating over the elements of a proxied collection. RCollectionIterableOnce avoids an additional
53 /// iterator copy (see TVirtualCollectionProxy::GetFunctionCopyIterator) and thus can only be iterated once.
55 public:
62
63 private:
64 class RIterator {
66 void *fIterator = nullptr;
67 void *fElementPtr = nullptr;
68
69 void Advance()
70 {
71 auto fnNext_Contig = [&]() {
72 // Array-backed collections (e.g. `kSTLvector`) directly use the pointer-to-iterator-data as a
73 // pointer-to-element, thus saving an indirection level (see documentation for TVirtualCollectionProxy)
74 auto &iter = reinterpret_cast<unsigned char *&>(fIterator), p = iter;
75 iter += fOwner.fStride;
76 return p;
77 };
78 fElementPtr = fOwner.fStride ? fnNext_Contig() : fOwner.fIFuncs.fNext(fIterator, fOwner.fEnd);
79 }
80
81 public:
82 using iterator_category = std::forward_iterator_tag;
84 using difference_type = std::ptrdiff_t;
85 using pointer = void *;
86
87 RIterator(const RCollectionIterableOnce &owner) : fOwner(owner) {}
88 RIterator(const RCollectionIterableOnce &owner, void *iter) : fOwner(owner), fIterator(iter) { Advance(); }
90 {
91 Advance();
92 return *this;
93 }
94 pointer operator*() const { return fElementPtr; }
95 bool operator!=(const iterator &rh) const { return fElementPtr != rh.fElementPtr; }
96 bool operator==(const iterator &rh) const { return fElementPtr == rh.fElementPtr; }
97 };
98
100 const std::size_t fStride;
105
106 public:
107 /// Construct a RCollectionIterableOnce that iterates over `collection`. If elements are guaranteed to be
108 /// contiguous in memory (e.g. a vector), `stride` can be provided for faster iteration, i.e. the address of each
109 /// element is known given the base pointer.
111 std::size_t stride = 0U)
113 {
114 fIFuncs.fCreateIterators(collection, &fBegin, &fEnd, proxy);
115 }
116 ~RCollectionIterableOnce() { fIFuncs.fDeleteTwoIterators(fBegin, fEnd); }
117
118 RIterator begin() { return RIterator(*this, fBegin); }
119 RIterator end() { return fStride ? RIterator(*this, fEnd) : RIterator(*this); }
120 }; // class RCollectionIterableOnce
121
123 private:
124 std::shared_ptr<TVirtualCollectionProxy> fProxy;
125 std::unique_ptr<RDeleter> fItemDeleter;
126 std::size_t fItemSize = 0;
128
129 public:
130 explicit RProxiedCollectionDeleter(std::shared_ptr<TVirtualCollectionProxy> proxy);
131 RProxiedCollectionDeleter(std::shared_ptr<TVirtualCollectionProxy> proxy, std::unique_ptr<RDeleter> itemDeleter,
132 size_t itemSize);
133 void operator()(void *objPtr, bool dtorOnly) final;
134 };
135
136 /// The collection proxy is needed by the deleters and thus defined as a shared pointer
137 std::shared_ptr<TVirtualCollectionProxy> fProxy;
140 /// Two sets of functions to operate on iterators, to be used depending on the access type. The direction preserves
141 /// the meaning from TVirtualCollectionProxy, i.e. read from disk / write to disk, respectively
144 std::size_t fItemSize;
146
147 /// Constructor used when the value type of the collection is not known in advance, i.e. in the case of custom
148 /// collections. Note that this constructor requires manual initialization of the item field
149 /// (Attach() and setting fItemSize)
151
152 std::unique_ptr<RFieldBase> CloneImpl(std::string_view newName) const override;
154 void GenerateColumns() final;
156
157 void ConstructValue(void *where) const final;
158 std::unique_ptr<RDeleter> GetDeleter() const final;
159
160 std::size_t AppendImpl(const void *from) final;
162
164
166
167public:
168 RProxiedCollectionField(std::string_view fieldName, std::string_view typeName);
171 ~RProxiedCollectionField() override = default;
172
173 std::vector<RValue> SplitValue(const RValue &value) const final;
174 std::size_t GetValueSize() const final;
175 std::size_t GetAlignment() const final;
177};
178
179////////////////////////////////////////////////////////////////////////////////
180/// Template specializations for classes with collection proxies
181////////////////////////////////////////////////////////////////////////////////
182
183template <typename T, typename = void>
185};
186template <typename T>
188 T, typename std::enable_if<std::is_same<typename T::IsCollectionProxy, std::true_type>::value>::type>
189 : std::true_type {
190};
191
192/* The point here is that we can only tell at run time if a class has an associated collection proxy.
193For compile time, in the first iteration of this PR we had an extra template argument that acted as a "tag" to
194differentiate the RField specialization for classes with an associated collection proxy (inherits
195RProxiedCollectionField) from the RField primary template definition (RClassField-derived), as in:
196```
197auto field = std::make_unique<RField<MyClass>>("klass");
198// vs
199auto otherField = std::make_unique<RField<MyClass, ROOT::TagIsCollectionProxy>>("klass");
200```
201
202That is convenient only for non-nested types, i.e. it doesn't work with, e.g. `RField<std::vector<MyClass>,
203ROOT::TagIsCollectionProxy>`, as the tag is not forwarded to the instantiation of the inner RField
204(that for the value type of the vector). The following two possible solutions were considered:
205- A wrapper type that helps to differentiate both cases.
206There we would have:
207```
208auto field = std::make_unique<RField<RProxiedCollection<MyClass>>>("klass"); // Using collection proxy
209```
210- A helper IsCollectionProxy<T> type, that can be used in a similar way to those in the `<type_traits>` header.
211We found this more convenient and is the implemented thing below. Here, classes can be marked as a
212collection proxy with either of the following two forms (whichever is more convenient for the user):
213```
214template <>
215struct IsCollectionProxy<MyClass> : std::true_type {};
216```
217or by adding a member type to the class as follows:
218```
219class MyClass {
220public:
221 using IsCollectionProxy = std::true_type;
222};
223```
224
225Of course, there is another possible solution which is to have a single RClassField that implements both
226the regular-class and the collection-proxy behaviors, and always chooses appropriately at run time.
227We found that less clean and probably has more overhead, as most probably it involves an additional branch + call
228in each of the member functions. */
229/// Helper type trait for marking classes as a collection proxy.
230/// This type trait must be set for collection proxy-based RNTuple fields created through MakeField<T>.
231template <typename T, typename = void>
234
235/// Classes behaving as a collection of elements that can be queried via the TVirtualCollectionProxy interface
236/// The use of a collection proxy for a particular class can be enabled via:
237/// ```
238/// namespace ROOT {
239/// template <> struct IsCollectionProxy<Classname> : std::true_type {};
240/// }
241/// ```
242/// Alternatively, this can be achieved by adding a member type to the class definition as follows:
243/// ```
244/// class Classname {
245/// public:
246/// using IsCollectionProxy = std::true_type;
247/// };
248/// ```
249template <typename T>
250class RField<T, typename std::enable_if<IsCollectionProxy<T>::value>::type> final : public RProxiedCollectionField {
251public:
252 static std::string TypeName() { return ROOT::Internal::GetRenormalizedTypeName(typeid(T)); }
253 RField(std::string_view name) : RProxiedCollectionField(name, Internal::GetDemangledTypeName(typeid(T)))
254 {
255 static_assert(std::is_class<T>::value, "collection proxy unsupported for fundamental types");
256 }
257 RField(RField &&other) = default;
258 RField &operator=(RField &&other) = default;
259 ~RField() final = default;
260};
261
262////////////////////////////////////////////////////////////////////////////////
263/// Template specializations for C++ std::[unordered_][multi]map
264////////////////////////////////////////////////////////////////////////////////
265
266/// The generic field for a `std::map<KeyType, ValueType>` and `std::unordered_map<KeyType, ValueType>`
268public:
269 enum class EMapType {
270 kMap,
271 kUnorderedMap,
272 kMultiMap,
273 kUnorderedMultiMap
274 };
275
276private:
278
279protected:
280 std::unique_ptr<RFieldBase> CloneImpl(std::string_view newName) const final;
281 void ReconcileOnDiskField(const RNTupleDescriptor &desc) final;
282
283public:
284 RMapField(std::string_view fieldName, EMapType mapType, std::unique_ptr<RFieldBase> itemField);
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, EMapType::kMap, 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
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, EMapType::kUnorderedMap, 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
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, EMapType::kMultiMap, 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
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, EMapType::kUnorderedMultiMap, 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 enum class ESetType {
366 kSet,
367 kUnorderedSet,
368 kMultiSet,
369 kUnorderedMultiSet
370 };
371
372private:
374
375protected:
376 std::unique_ptr<RFieldBase> CloneImpl(std::string_view newName) const final;
377 void ReconcileOnDiskField(const RNTupleDescriptor &desc) final;
378
379public:
380 RSetField(std::string_view fieldName, ESetType setType, std::unique_ptr<RFieldBase> itemField);
383 ~RSetField() override = default;
384};
385
386template <typename ItemT>
387class RField<std::set<ItemT>> final : public RSetField {
388public:
389 static std::string TypeName() { return "std::set<" + RField<ItemT>::TypeName() + ">"; }
390
391 explicit RField(std::string_view name) : RSetField(name, ESetType::kSet, std::make_unique<RField<ItemT>>("_0")) {}
392 RField(RField &&other) = default;
393 RField &operator=(RField &&other) = default;
394 ~RField() final = default;
395};
396
398class RField<std::unordered_set<ItemT>> final : public RSetField {
399public:
400 static std::string TypeName() { return "std::unordered_set<" + RField<ItemT>::TypeName() + ">"; }
401
402 explicit RField(std::string_view name)
403 : RSetField(name, ESetType::kUnorderedSet, 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::multiset<ItemT>> final : public RSetField {
413public:
414 static std::string TypeName() { return "std::multiset<" + RField<ItemT>::TypeName() + ">"; }
415
416 explicit RField(std::string_view name) : RSetField(name, ESetType::kMultiSet, std::make_unique<RField<ItemT>>("_0"))
417 {
418 }
419 RField(RField &&other) = default;
420 RField &operator=(RField &&other) = default;
421 ~RField() final = default;
422};
423
425class RField<std::unordered_multiset<ItemT>> final : public RSetField {
426public:
427 static std::string TypeName() { return "std::unordered_multiset<" + RField<ItemT>::TypeName() + ">"; }
428
429 explicit RField(std::string_view name)
430 : RSetField(name, ESetType::kUnorderedMultiSet, std::make_unique<RField<ItemT>>("_0"))
431 {
432 }
433 RField(RField &&other) = default;
434 RField &operator=(RField &&other) = default;
435 ~RField() final = default;
436};
437
438} // namespace ROOT
439
440#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:148
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:319
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)
void operator()(void *objPtr, bool dtorOnly) final
The field for a class representing a collection of elements via TVirtualCollectionProxy.
RProxiedCollectionField & operator=(RProxiedCollectionField &&other)=default
std::size_t GetValueSize() const final
What sizeof(T) for this type returns.
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 override
Called by Clone(), which additionally copies the on-disk ID.
~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
void ReconcileOnDiskField(const RNTupleDescriptor &desc) override
For non-artificial fields, check compatibility of the in-memory field and the on-disk field.
std::size_t GetAlignment() const final
What alignof(T) for this type returns.
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.