ROOT
master
Reference Guide
Loading...
Searching...
No Matches
vo001_AdoptOrOwnMemory.C
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
// We use this class for didactic purposes: upon copy, a line is printed to the terminal.
14
class
UponCopyPrinter
{
15
public
:
16
UponCopyPrinter
() =
default
;
17
UponCopyPrinter
(
UponCopyPrinter
&&) =
default
;
18
UponCopyPrinter
(
const
UponCopyPrinter
&) { std::cout <<
"Invoking copy c'tor!"
<< std::endl; }
19
};
20
21
void
vo001_AdoptOrOwnMemory
()
22
{
23
24
// One of the essential features of RVec is its ability of adopting and owning memory.
25
26
// Let's create an RVec of UponCopyPrinter instances. We expect no printout:
27
ROOT::RVec<UponCopyPrinter>
v
(3);
28
29
// Let's adopt the memory from v into v2. We expect no printout:
30
ROOT::RVec<UponCopyPrinter>
v2
(
v
.data(),
v
.size());
31
32
// OK, let's check the addresses of the memory associated to the two RVecs It is the same!
33
std::cout <<
v
.data() <<
" and "
<<
v2
.data() << std::endl;
34
35
// Now, upon reallocation, the RVec stops adopting the memory and starts owning it. And yes,
36
// a copy is triggered. Indeed internally the storage of the RVec is an std::vector. Moreover,
37
// the interface of the RVec is very, very similar to the one of std::vector: you have already
38
// noticed it when the `data()` method was invoked, right?
39
40
v2
.push_back(
UponCopyPrinter
());
41
42
// Of course, now the addresses are different.
43
std::cout <<
v
.data() <<
" and "
<<
v2
.data() << std::endl;
44
}
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
ROOT::VecOps::RVec
A "std::vector"-like collection of values implementing handy operation to analyse them.
Definition
RVec.hxx:1529
vo001_AdoptOrOwnMemory
Definition
vo001_AdoptOrOwnMemory.py:1
v2
@ v2
Definition
rootcling_impl.cxx:3702
v
@ v
Definition
rootcling_impl.cxx:3699
tutorials
math
vecops
vo001_AdoptOrOwnMemory.C
ROOT master - Reference Guide Generated on Fri Jan 24 2025 04:37:20 (GVA Time) using Doxygen 1.10.0