matio-cpp  v0.2.5
A C++ wrapper of the matio library, with memory ownership handling, to read and write .mat files.
MatvarHandler.h
Go to the documentation of this file.
1 #ifndef MATIOCPP_MATVARHANDLER_H
2 #define MATIOCPP_MATVARHANDLER_H
3 
4 /*
5  * Copyright (C) 2020 Fondazione Istituto Italiano di Tecnologia
6  *
7  * This software may be modified and distributed under the terms of the
8  * BSD-2-Clause license (https://opensource.org/licenses/BSD-2-Clause).
9  */
10 
12 
14 {
15 protected:
16 
18  {
19  matvar_t* m_ptr;
23  ValueType m_valueType{ValueType::UNSUPPORTED};
25  DeleteMode m_mode;
27  public:
28 
32  PointerInfo();
33 
39  PointerInfo(matvar_t* ptr, DeleteMode deleteMode);
40 
44  ~PointerInfo();
45 
51  void changePointer(matvar_t* ptr, DeleteMode deleteMode);
52 
56  void deletePointer();
57 
62  matvar_t* pointer();
63 
68  VariableType variableType() const;
69 
74  ValueType valueType() const;
75 
80  DeleteMode deleteMode() const;
81 
87  bool operator!=(const PointerInfo& other) const;
88 
94  static void DeletePointer(matvar_t *ptr, DeleteMode deleteMode);
95  };
96 
97  struct Dependency
98  {
103  matvar_t* parent{nullptr};
104  };
105 
109  class Ownership
110  {
119  void dropDependencies(matvar_t* previouslyOwned);
120 
121  public:
122 
127  Ownership(std::weak_ptr<PointerInfo> pointerToDeallocate);
128 
132  ~Ownership();
133 
139  bool isOwning(matvar_t* test);
140 
148  void own(matvar_t* owned, const MatvarHandler* owner, matioCpp::DeleteMode mode);
149 
154  void drop(matvar_t* previouslyOwned);
155 
159  void dropAll();
160  };
161 
168 
169 public:
170 
176  MatvarHandler();
177 
183  MatvarHandler(matvar_t *inputPtr, matioCpp::DeleteMode deleteMode = matioCpp::DeleteMode::Delete);
184 
189  MatvarHandler(const MatvarHandler& other);
190 
195  MatvarHandler(MatvarHandler&& other);
196 
200  virtual ~MatvarHandler(){}
201 
206  virtual matvar_t* get() const = 0;
207 
212  virtual bool isShared() const = 0;
213 
219  virtual bool duplicateMatvar(const matvar_t* inputPtr) = 0;
220 
226  virtual bool importMatvar(matvar_t* inputPtr) = 0;
227 
232  virtual MatvarHandler* pointerToDuplicate() const = 0;
233 
238  virtual WeakMatvar weakOwnership() const = 0;
239 
244  virtual void dropOwnedPointer(matvar_t* previouslyOwnedPointer) = 0;
245 
250  ValueType valueType() const;
251 
256  VariableType variableType() const;
257 
263  static matvar_t* GetMatvarDuplicate(const matvar_t* inputPtr);
264 
270  static void DeleteMatvar(matvar_t* pointerToDelete, DeleteMode mode = DeleteMode::Delete);
271 
272 };
273 
274 
275 #endif // MATIOCPP_MATVARHANDLER_H
The Ownership class is used to define the ownership of a matvar.
void drop(matvar_t *previouslyOwned)
Drop a previously owned pointer and deleted if necessary.
bool isOwning(matvar_t *test)
Check if an input pointer is owned by this ownership object.
void own(matvar_t *owned, const MatvarHandler *owner, matioCpp::DeleteMode mode)
Add a pointer to the list of owned pointers.
Ownership(std::weak_ptr< PointerInfo > pointerToDeallocate)
Constructor.
void dropAll()
Drops all the previously owned pointers and free those that need to be deallocated,...
matvar_t * pointer()
Get the matvar pointer.
void deletePointer()
Delete the matvar pointer.
ValueType valueType() const
Get the value type.
VariableType variableType() const
Get the variable type.
bool operator!=(const PointerInfo &other) const
Comparison operator.
void changePointer(matvar_t *ptr, DeleteMode deleteMode)
Change the input pointer.
DeleteMode deleteMode() const
Get the deletion mode.
static void DeletePointer(matvar_t *ptr, DeleteMode deleteMode)
Delete the given pointer given the mode.
virtual bool duplicateMatvar(const matvar_t *inputPtr)=0
Perform a deep copy of the input pointer.
static matvar_t * GetMatvarDuplicate(const matvar_t *inputPtr)
Get a duplicate of the input matvar pointer/.
virtual bool importMatvar(matvar_t *inputPtr)=0
Import the input pointer.
virtual bool isShared() const =0
Check if the onwership is shared with the current object.
virtual MatvarHandler * pointerToDuplicate() const =0
Get a pointer to a duplicate of the MatvarHandler.
virtual void dropOwnedPointer(matvar_t *previouslyOwnedPointer)=0
Drop a pointer from the list of owned pointers.
static void DeleteMatvar(matvar_t *pointerToDelete, DeleteMode mode=DeleteMode::Delete)
Delete the specified Matvar.
ValueType valueType() const
Get the value type of the pointer.
virtual ~MatvarHandler()
Default destructor.
MatvarHandler()
Default constructor.
std::shared_ptr< PointerInfo > m_ptr
Shared pointer to a PointerInfo.
virtual matvar_t * get() const =0
Get the shared matvar_t pointer.
VariableType variableType() const
Get the variable type of the pointer.
virtual WeakMatvar weakOwnership() const =0
Get a WeakMatvar version of the current MatvarHandler.
DeleteMode
The delete mode of matvar_t pointers.
@ Delete
The handler deletes the pointer but not the data.
VariableType
Define the type of variable.
ValueType
The list of types for an element of a certain variable type.
matioCpp::DeleteMode mode
The set of dependencies.
std::unordered_set< matvar_t * > dependencies
Definition: MatvarHandler.h:99
matvar_t * parent
Deletion mode for the dependency.