Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RColumnElementBase.hxx
Go to the documentation of this file.
1/// \file ROOT/RColumnElementBase.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_RColumnElementBase
15#define ROOT_RColumnElementBase
16
17#include "RtypesCore.h"
18#include <ROOT/RError.hxx>
19#include <ROOT/RFloat16.hxx>
20#include <ROOT/RNTupleUtil.hxx>
21
22#include <Byteswap.h>
23#include <TError.h>
24
25#include <cstring> // for memcpy
26#include <cstddef> // for std::byte
27#include <cstdint>
28#include <memory>
29#include <optional>
30#include <string>
31#include <type_traits>
32#include <typeindex>
33#include <typeinfo>
34#include <utility>
35
36namespace ROOT::Internal {
37
38// clang-format off
39/**
40\class ROOT::Internal::RColumnElementBase
41\ingroup NTuple
42\brief A column element encapsulates the translation between basic C++ types and their column representation.
43
44Usually the on-disk element should map bitwise to the in-memory element. Sometimes that's not the case
45though, for instance on big endian platforms or for bools.
46
47There is a template specialization for every valid pair of C++ type and column representation.
48These specialized child classes are responsible for overriding `Pack()` / `Unpack()` for packing / unpacking elements
49as appropriate.
50*/
51// clang-format on
53protected:
54 /// Size of the C++ value that corresponds to the on-disk element
55 std::size_t fSize;
56 std::size_t fBitsOnStorage;
57 /// This is only meaningful for column elements that support it (e.g. Real32Quant)
58 std::optional<std::pair<double, double>> fValueRange = std::nullopt;
59
60 explicit RColumnElementBase(std::size_t size, std::size_t bitsOnStorage = 0)
62 {
63 }
64
65public:
66 /// Every concrete RColumnElement type is identified by its on-disk type (column type) and the
67 /// in-memory C++ type, given by a type index.
68 struct RIdentifier {
69 std::type_index fInMemoryType = std::type_index(typeid(void));
71
72 bool operator==(const RIdentifier &other) const
73 {
74 return this->fInMemoryType == other.fInMemoryType && this->fOnDiskType == other.fOnDiskType;
75 }
76
77 bool operator!=(const RIdentifier &other) const { return !(*this == other); }
78 };
79
84 virtual ~RColumnElementBase() = default;
85
86 /// If CppT == void, use the default C++ type for the given column type
87 template <typename CppT = void>
88 static std::unique_ptr<RColumnElementBase> Generate(ROOT::ENTupleColumnType type);
90 /// Most types have a fixed on-disk bit width. Some low-precision column types
91 /// have a range of possible bit widths. Return the minimum and maximum allowed
92 /// bit size per type.
93 static std::pair<std::uint16_t, std::uint16_t> GetValidBitRange(ROOT::ENTupleColumnType type);
94
95 /// Derived, typed classes tell whether the on-storage layout is bitwise identical to the memory layout
96 virtual bool IsMappable() const
97 {
98 R__ASSERT(false);
99 return false;
100 }
101
102 virtual void SetBitsOnStorage(std::size_t bitsOnStorage)
103 {
105 throw RException(R__FAIL(std::string("internal error: cannot change bit width of this column type")));
106 }
107
108 virtual void SetValueRange(double, double)
109 {
110 throw RException(R__FAIL(std::string("internal error: cannot change value range of this column type")));
111 }
112
113 /// If the on-storage layout and the in-memory layout differ, packing creates an on-disk page from an in-memory page
114 virtual void Pack(void *destination, const void *source, std::size_t count) const
115 {
116 std::memcpy(destination, source, count);
117 }
118
119 /// If the on-storage layout and the in-memory layout differ, unpacking creates a memory page from an on-storage page
120 virtual void Unpack(void *destination, const void *source, std::size_t count) const
121 {
122 std::memcpy(destination, source, count);
123 }
124
125 std::size_t GetSize() const { return fSize; }
126 std::size_t GetBitsOnStorage() const { return fBitsOnStorage; }
127 std::optional<std::pair<double, double>> GetValueRange() const { return fValueRange; }
128 std::size_t GetPackedSize(std::size_t nElements = 1U) const { return (nElements * fBitsOnStorage + 7) / 8; }
129
130 virtual RIdentifier GetIdentifier() const = 0;
131}; // class RColumnElementBase
132
134 std::uint32_t dummy;
135};
136
137std::unique_ptr<RColumnElementBase>
139
140std::unique_ptr<RColumnElementBase> GenerateColumnElement(const RColumnElementBase::RIdentifier &elementId);
141
142template <typename CppT>
144{
145 return GenerateColumnElement(std::type_index(typeid(CppT)), onDiskType);
146}
147
148template <>
149std::unique_ptr<RColumnElementBase> RColumnElementBase::Generate<void>(ROOT::ENTupleColumnType onDiskType);
150
151} // namespace ROOT::Internal
152
153#endif
#define R__FAIL(msg)
Short-hand to return an RResult<T> in an error state; the RError is implicitly converted into RResult...
Definition RError.hxx:299
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
#define R__ASSERT(e)
Checks condition e and reports a fatal error if it's false.
Definition TError.h:125
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
The available trivial, native content types of a column.
A column element encapsulates the translation between basic C++ types and their column representation...
virtual bool IsMappable() const
Derived, typed classes tell whether the on-storage layout is bitwise identical to the memory layout.
static const char * GetColumnTypeName(ROOT::ENTupleColumnType type)
RColumnElementBase & operator=(RColumnElementBase &&other)=default
static std::unique_ptr< RColumnElementBase > Generate(ROOT::ENTupleColumnType type)
If CppT == void, use the default C++ type for the given column type.
virtual void SetBitsOnStorage(std::size_t bitsOnStorage)
static std::pair< std::uint16_t, std::uint16_t > GetValidBitRange(ROOT::ENTupleColumnType type)
Most types have a fixed on-disk bit width.
RColumnElementBase(std::size_t size, std::size_t bitsOnStorage=0)
std::optional< std::pair< double, double > > GetValueRange() const
RColumnElementBase & operator=(const RColumnElementBase &other)=delete
virtual void SetValueRange(double, double)
virtual void Unpack(void *destination, const void *source, std::size_t count) const
If the on-storage layout and the in-memory layout differ, unpacking creates a memory page from an on-...
std::size_t fSize
Size of the C++ value that corresponds to the on-disk element.
RColumnElementBase(const RColumnElementBase &other)=default
std::optional< std::pair< double, double > > fValueRange
This is only meaningful for column elements that support it (e.g. Real32Quant)
virtual ~RColumnElementBase()=default
virtual RIdentifier GetIdentifier() const =0
RColumnElementBase(RColumnElementBase &&other)=default
virtual void Pack(void *destination, const void *source, std::size_t count) const
If the on-storage layout and the in-memory layout differ, packing creates an on-disk page from an in-...
std::size_t GetPackedSize(std::size_t nElements=1U) const
Base class for all ROOT issued exceptions.
Definition RError.hxx:79
std::unique_ptr< RColumnElementBase > GenerateColumnElement(std::type_index inMemoryType, ROOT::ENTupleColumnType onDiskType)
Every concrete RColumnElement type is identified by its on-disk type (column type) and the in-memory ...
bool operator!=(const RIdentifier &other) const
bool operator==(const RIdentifier &other) const