Salome HOME
Merge branch 'V9_9_BR'
[modules/smesh.git] / src / MEDWrapper / MED_TFile.hxx
1 // Copyright (C) 2021-2022  CEA/DEN, EDF R&D
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 {
27   class TFileInternal
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
36   class MEDWRAPPER_EXPORT MEDIDTHoder : public TFileInternal
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
61   class MEDWRAPPER_EXPORT TFileDecorator : public TFileInternal
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
73   class MEDWRAPPER_EXPORT TMemFile : public MEDIDTHoder
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
87   class MEDWRAPPER_EXPORT TFile : public MEDIDTHoder
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;
94     TInt myMajor;
95     TInt myMinor;
96   };
97 }