Salome HOME
Merge branch 'V9_7_BR'
[modules/smesh.git] / src / MEDWrapper / MED_TFile.hxx
1 // Copyright (C) 2021  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
24 namespace MED
25 {
26   class TFileInternal
27   {
28   public:
29     virtual ~TFileInternal() = default;
30     virtual void Open(EModeAcces theMode, TErr* theErr = nullptr) = 0;
31     virtual void Close() = 0;
32     virtual const TIdt& Id() const = 0;
33   };
34
35   class MEDIDTHoder : public TFileInternal
36   {
37   protected:
38     MEDIDTHoder(bool *isClosedStatus = nullptr):_isClosedStatus(isClosedStatus) { }
39     void UnRefFid()
40     {
41       if (--myCount == 0)
42       {
43         MEDfileClose(myFid);
44         myIsClosed = true;
45         if(_isClosedStatus)
46           *_isClosedStatus = true;
47       }
48     }
49   public:
50     const TIdt& Id() const override;
51     ~MEDIDTHoder() { this->UnRefFid(); }
52     void Close() override { this->UnRefFid(); }
53   protected:
54     TInt myCount = 0;
55     TIdt myFid = 0;
56     bool myIsClosed = false;
57     bool *_isClosedStatus = nullptr;
58   };
59
60   class TFileDecorator : public TFileInternal
61   {
62   public:
63     TFileDecorator(TFileInternal *effective):_effective(effective) { }
64     void Open(EModeAcces theMode, TErr* theErr = nullptr) override { if(_effective) _effective->Open(theMode,theErr); }
65     void Close() override { if(_effective) _effective->Close(); }
66     const TIdt& Id() const override { if(_effective) return _effective->Id(); EXCEPTION(std::runtime_error, "TFileDecorator - GetFid() : no effective TFile !"); }
67     ~TFileDecorator() { delete _effective; }
68   private:
69     TFileInternal *_effective = nullptr;
70   };
71
72   class TMemFile : public MEDIDTHoder
73   {
74   public:
75     TMemFile(bool* isClosedStatus = nullptr):MEDIDTHoder(isClosedStatus) { }
76     void Open(EModeAcces theMode, TErr* theErr = nullptr) override;
77     void *getData() const { return memfile.app_image_ptr; }
78     std::size_t getSize() const { return memfile.app_image_size; }
79   private:
80     med_memfile memfile = MED_MEMFILE_INIT;
81   };
82
83   class TFile : public MEDIDTHoder
84   {
85   public:
86     TFile(const std::string& theFileName, TInt theMajor=-1, TInt theMinor=-1);
87     void Open(EModeAcces theMode, TErr* theErr = nullptr) override;
88   protected:
89     std::string myFileName;
90     TInt myMajor;
91     TInt myMinor;
92   };
93 }