ROOT
Version master
v6.34
v6.32
v6.30
v6.28
v6.26
v6.24
v6.22
v6.20
v6.18
v6.16
v6.14
v6.12
v6.10
v6.08
v6.06
Reference Guide
►
ROOT
•
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Modules
Pages
Loading...
Searching...
No Matches
vo001_AdoptOrOwnMemory.py
Go to the documentation of this file.
1
## \file
2
## \ingroup tutorial_vecops
3
## \notebook -nodraw
4
## In this tutorial we learn how the RVec class can be used to
5
## adopt existing memory or allocate some.
6
##
7
## \macro_code
8
## \macro_output
9
##
10
## \date May 2018
11
## \author Danilo Piparo
12
13
import
ROOT
14
15
# We use this class for didactic purposes: upon copy, a line is printed to the terminal.
16
17
ROOT.gInterpreter.Declare
(
'''
18
class UponCopyPrinter {
19
public:
20
UponCopyPrinter() = default;
21
UponCopyPrinter(UponCopyPrinter &&) = default;
22
UponCopyPrinter(const UponCopyPrinter &) { std::cout << "Invoking copy c'tor!" << std::endl; }
23
};
24
'''
)
25
26
RVec_UponCopyPrinter =
ROOT.ROOT.VecOps.RVec
(
ROOT.UponCopyPrinter
)
27
28
# One of the essential features of RVec is its ability of adopting and owning memory.
29
30
# Let's create an RVec of UponCopyPrinter instances. We expect no printout:
31
v =
RVec_UponCopyPrinter
(3)
32
33
# Let's adopt the memory from v into v2. We expect no printout:
34
v2 =
RVec_UponCopyPrinter
(
v.data
(),
v.size
())
35
36
# OK, let's check the addresses of the memory associated to the two RVecs It is the same!
37
print(
"%s and %s"
%(
v.data
(),
v2.data
()))
38
39
# Now, upon reallocation, the RVec stops adopting the memory and starts owning it. And yes,
40
# a copy is triggered. Indeed internally the storage of the RVec is an std::vector. Moreover,
41
# the interface of the RVec is very, very similar to the one of std::vector: you have already
42
# noticed it when the `data()` method was invoked, right?
43
v2.resize
(4)
44
45
# Of course, now the addresses are different.
46
print(
"%s and %s"
%(
v.data
(),
v2.data
()))
TRangeDynCast
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Definition
TCollection.h:358
ROOT::Detail::TRangeCast
Definition
TCollection.h:311
tutorials
math
vecops
vo001_AdoptOrOwnMemory.py
ROOT master - Reference Guide Generated on Tue Apr 8 2025 06:21:58 (GVA Time) using Doxygen 1.10.0