matio-cpp  v0.2.5
A C++ wrapper of the matio library, with memory ownership handling, to read and write .mat files.
WeakMatvar.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 
8 #include <matioCpp/WeakMatvar.h>
9 
11 {
12 
13 }
14 
16  : matioCpp::MatvarHandler(other)
17  , m_ownership(other.m_ownership)
18 {
19 
20 }
21 
23  : matioCpp::MatvarHandler(other)
24  , m_ownership(other.m_ownership)
25 {
26 
27 }
28 
30 {
31  matioCpp::WeakMatvar otherWeak = other.weakOwnership();
32  m_ownership = otherWeak.m_ownership;
33  m_ptr = otherWeak.m_ptr;
34 }
35 
36 matioCpp::WeakMatvar::WeakMatvar(matvar_t *inputPtr, const SharedMatvar &owner, DeleteMode mode)
37  : matioCpp::MatvarHandler(inputPtr)
38  , m_ownership(owner.ownership())
39 
40 {
41  auto locked = m_ownership.lock();
42  if (locked)
43  {
44  locked->own(inputPtr, &owner, mode);
45  }
46 }
47 
49 {
50  m_ownership = owner->weakOwnership().m_ownership;
51  m_ptr = std::make_shared<PointerInfo>(inputPtr, mode);
52 
53  auto locked = m_ownership.lock();
54  if (locked)
55  {
56  locked->own(inputPtr, owner, mode);
57  }
58 }
59 
61 {
62 
63 }
64 
65 matvar_t *matioCpp::WeakMatvar::get() const
66 {
67  assert(m_ptr);
68  auto locked = m_ownership.lock();
69 
70  if (locked && locked->isOwning(m_ptr->pointer()))
71  {
72  return m_ptr->pointer();
73  }
74  else
75  {
76  return nullptr;
77  }
78 }
79 
81 {
82  return false;
83 }
84 
86 {
87  std::cerr << "[ERROR][matioCpp::WeakMatvar::duplicateFromMatio] Cannot duplicate from inputPtr. A WeakMatvar cannot modify the matvar pointer." << std::endl;
88 
89  return false;
90 }
91 
93 {
94  std::cerr << "[ERROR][matioCpp::WeakMatvar::importMatvar] Cannot import inputPtr. A WeakMatvar cannot modify the matvar pointer." << std::endl;
95 
96  return false;
97 }
98 
100 {
101  return new WeakMatvar(*this);
102 }
103 
105 {
106  return *this;
107 }
108 
109 void matioCpp::WeakMatvar::dropOwnedPointer(matvar_t *previouslyOwnedPointer)
110 {
111  auto locked = m_ownership.lock();
112  if (locked)
113  {
114  locked->drop(previouslyOwnedPointer);
115  }
116 }
117 
119 {
120  m_ownership = other.m_ownership;
121  m_ptr = other.m_ptr;
122  return *this;
123 }
124 
126 {
127  m_ownership = other.m_ownership;
128  m_ptr = other.m_ptr;
129  return *this;
130 }
131 
133 {
134  return operator=(other.weakOwnership());
135 }
136 
std::shared_ptr< PointerInfo > m_ptr
Shared pointer to a PointerInfo.
virtual WeakMatvar weakOwnership() const =0
Get a WeakMatvar version of the current MatvarHandler.
virtual WeakMatvar weakOwnership() const final
Docs inherited.
virtual matvar_t * get() const final
Docs inherited.
Definition: WeakMatvar.cpp:65
WeakMatvar()
Default constructor.
Definition: WeakMatvar.cpp:10
virtual bool isShared() const final
Docs inherited.
Definition: WeakMatvar.cpp:80
~WeakMatvar()
Destructor.
Definition: WeakMatvar.cpp:60
WeakMatvar & operator=(const WeakMatvar &other)
Copy assignement.
Definition: WeakMatvar.cpp:118
virtual MatvarHandler * pointerToDuplicate() const final
Docs inherited.
Definition: WeakMatvar.cpp:99
virtual bool importMatvar(matvar_t *) final
Docs inherited.
Definition: WeakMatvar.cpp:92
virtual WeakMatvar weakOwnership() const final
Docs inherited.
Definition: WeakMatvar.cpp:104
virtual bool duplicateMatvar(const matvar_t *) final
Docs inherited.
Definition: WeakMatvar.cpp:85
virtual void dropOwnedPointer(matvar_t *previouslyOwnedPointer) final
Docs inherited.
Definition: WeakMatvar.cpp:109
T endl(T... args)
DeleteMode
The delete mode of matvar_t pointers.