matio-cpp  v0.2.5
A C++ wrapper of the matio library, with memory ownership handling, to read and write .mat files.
SharedMatvar.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2020 Fondazione Istituto Italiano di Tecnologia
3  *
4  * This software may be modified and distributed under the terms of the
5  * BSD-2-Clause license (https://opensource.org/licenses/BSD-2-Clause).
6  */
7 
9 #include <matioCpp/WeakMatvar.h>
10 #include <matioCpp/Span.h>
12 
13 
16  , m_ownership(std::make_shared<MatvarHandler::Ownership>(m_ptr))
17 {
18 
19 }
20 
22  : matioCpp::MatvarHandler(other)
23  , m_ownership(other.m_ownership)
24 {
25 
26 }
27 
29  : matioCpp::MatvarHandler(other)
30  , m_ownership(other.m_ownership)
31 {
32 
33 }
34 
36  : matioCpp::MatvarHandler(inputPtr)
37  , m_ownership(std::make_shared<MatvarHandler::Ownership>(m_ptr))
38 {
39 
40 }
41 
43 {
44 
45 }
46 
47 matvar_t *matioCpp::SharedMatvar::get() const
48 {
49  assert(m_ptr);
50  return m_ptr->pointer();
51 }
52 
54 {
55  return true;
56 }
57 
58 bool matioCpp::SharedMatvar::duplicateMatvar(const matvar_t *inputPtr)
59 {
60  return importMatvar(matioCpp::MatvarHandler::GetMatvarDuplicate(inputPtr));
61 }
62 
63 bool matioCpp::SharedMatvar::importMatvar(matvar_t *inputPtr)
64 {
65  assert(m_ptr);
66 
67  m_ownership->dropAll();
68 
69  m_ptr->changePointer(inputPtr, DeleteMode::Delete);
70 
71  return true;
72 }
73 
75 {
76  return new SharedMatvar(*this);
77 }
78 
80 {
82  weak.m_ownership = m_ownership;
83  weak.m_ptr = m_ptr;
84  return weak;
85 }
86 
87 void matioCpp::SharedMatvar::dropOwnedPointer(matvar_t *previouslyOwnedPointer)
88 {
89  m_ownership->drop(previouslyOwnedPointer);
90 }
91 
93 {
94  m_ownership = other.m_ownership;
95  m_ptr = other.m_ptr;
96  return *this;
97 }
98 
100 {
101  m_ownership = other.m_ownership;
102  m_ptr = other.m_ptr;
103  return *this;
104 }
105 
107 {
108  return m_ownership;
109 }
110 
112 {
113  SharedMatvar output;
114  output.m_ptr = std::make_shared<PointerInfo>(Mat_VarDuplicate(inputPtr, 0), DeleteMode::ShallowDelete);
115  output.m_ownership = std::make_shared<MatvarHandler::Ownership>(output.m_ptr);
116  return output;
117 }
The Ownership class is used to define the ownership of a matvar.
static matvar_t * GetMatvarDuplicate(const matvar_t *inputPtr)
Get a duplicate of the input matvar pointer/.
std::shared_ptr< PointerInfo > m_ptr
Shared pointer to a PointerInfo.
~SharedMatvar()
Destructor.
virtual WeakMatvar weakOwnership() const final
Docs inherited.
virtual bool isShared() const final
Docs inherited.
virtual bool importMatvar(matvar_t *inputPtr) final
Docs inherited.
static SharedMatvar GetMatvarShallowDuplicate(const matvar_t *inputPtr)
Get the shallow duplicate of an input Matvar.
SharedMatvar()
Default constructor.
virtual void dropOwnedPointer(matvar_t *previouslyOwnedPointer) final
Docs inherited.
std::weak_ptr< MatvarHandler::Ownership > ownership() const
Get a weak_ptr to the ownership.
virtual matvar_t * get() const final
Docs inherited.
virtual MatvarHandler * pointerToDuplicate() const final
Docs inherited.
virtual bool duplicateMatvar(const matvar_t *inputPtr) final
Docs inherited.
SharedMatvar & operator=(const SharedMatvar &other)
Copy assignement.
@ ShallowDelete
The handler does not delete the pointer.
@ Delete
The handler deletes the pointer but not the data.
STL namespace.