Version: 9.12.0
MED_TFile.hxx
Go to the documentation of this file.
1 // Copyright (C) 2021-2023 CEA, EDF
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 
20 #pragma once
21 
22 #include "MED_Wrapper.hxx"
23 #include "MED_WrapperDef.hxx"
24 
25 namespace MED
26 {
28  {
29  public:
30  virtual ~TFileInternal() = default;
31  virtual void Open(EModeAcces theMode, TErr* theErr = nullptr) = 0;
32  virtual void Close() = 0;
33  virtual const TIdt& Id() const = 0;
34  };
35 
37  {
38  protected:
39  MEDIDTHoder(bool *isClosedStatus = nullptr):_isClosedStatus(isClosedStatus) { }
40  void UnRefFid()
41  {
42  if (--myCount == 0)
43  {
44  MEDfileClose(myFid);
45  myIsClosed = true;
46  if(_isClosedStatus)
47  *_isClosedStatus = true;
48  }
49  }
50  public:
51  const TIdt& Id() const override;
52  ~MEDIDTHoder() { this->UnRefFid(); }
53  void Close() override { this->UnRefFid(); }
54  protected:
55  TInt myCount = 0;
56  TIdt myFid = 0;
57  bool myIsClosed = false;
58  bool *_isClosedStatus = nullptr;
59  };
60 
62  {
63  public:
64  TFileDecorator(TFileInternal *effective):_effective(effective) { }
65  void Open(EModeAcces theMode, TErr* theErr = nullptr) override { if(_effective) _effective->Open(theMode,theErr); }
66  void Close() override { if(_effective) _effective->Close(); }
67  const TIdt& Id() const override { if(_effective) return _effective->Id(); EXCEPTION(std::runtime_error, "TFileDecorator - GetFid() : no effective TFile !"); }
68  ~TFileDecorator() { delete _effective; }
69  private:
70  TFileInternal *_effective = nullptr;
71  };
72 
74  {
75  public:
76  TMemFile(void **data, std::size_t *sz, bool* isClosedStatus):MEDIDTHoder(isClosedStatus),_data(data),_sz(sz) { memfile.app_image_ptr=*data; memfile.app_image_size=*sz; }
77  ~TMemFile() { UnRefFid(); if(myIsClosed) { *_data = memfile.app_image_ptr; *_sz = memfile.app_image_size; } }
78  void Open(EModeAcces theMode, TErr* theErr = nullptr) override;
79  void *getData() const { return memfile.app_image_ptr; }
80  std::size_t getSize() const { return memfile.app_image_size; }
81  private:
82  void **_data = nullptr;
83  std::size_t * _sz = nullptr;
84  med_memfile memfile = MED_MEMFILE_INIT;
85  };
86 
88  {
89  public:
90  TFile(const std::string& theFileName, TInt theMajor=-1, TInt theMinor=-1);
91  void Open(EModeAcces theMode, TErr* theErr = nullptr) override;
92  protected:
93  std::string myFileName;
96  };
97 }
#define MEDWRAPPER_EXPORT
Definition: MED_WrapperDef.hxx:33
#define EXCEPTION(TYPE, MSG)
Definition: UNV_Utilities.hxx:160
Definition: MED_TFile.hxx:37
void Close() override
Definition: MED_TFile.hxx:53
void UnRefFid()
Definition: MED_TFile.hxx:40
~MEDIDTHoder()
Definition: MED_TFile.hxx:52
MEDIDTHoder(bool *isClosedStatus=nullptr)
Definition: MED_TFile.hxx:39
Definition: MED_TFile.hxx:62
~TFileDecorator()
Definition: MED_TFile.hxx:68
TFileDecorator(TFileInternal *effective)
Definition: MED_TFile.hxx:64
void Close() override
Definition: MED_TFile.hxx:66
const TIdt & Id() const override
Definition: MED_TFile.hxx:67
void Open(EModeAcces theMode, TErr *theErr=nullptr) override
Definition: MED_TFile.hxx:65
Definition: MED_TFile.hxx:28
virtual void Close()=0
virtual void Open(EModeAcces theMode, TErr *theErr=nullptr)=0
virtual ~TFileInternal()=default
virtual const TIdt & Id() const =0
Definition: MED_TFile.hxx:88
TInt myMinor
Definition: MED_TFile.hxx:95
std::string myFileName
Definition: MED_TFile.hxx:93
TInt myMajor
Definition: MED_TFile.hxx:94
Definition: MED_TFile.hxx:74
TMemFile(void **data, std::size_t *sz, bool *isClosedStatus)
Definition: MED_TFile.hxx:76
std::size_t getSize() const
Definition: MED_TFile.hxx:80
void * getData() const
Definition: MED_TFile.hxx:79
~TMemFile()
Definition: MED_TFile.hxx:77
Definition: MED_Algorithm.cxx:28
herr_t TErr
Definition: MED_Common.hxx:52
med_int TInt
Definition: MED_Common.hxx:50
hid_t TIdt
Definition: MED_Common.hxx:51
EModeAcces
Definition: MED_Wrapper.hxx:38