Salome HOME
Merge branch 'master' of https://codev-tuleap.cea.fr/plugins/git/salome/smesh
[modules/smesh.git] / src / MEDWrapper / MED_TFile.hxx
diff --git a/src/MEDWrapper/MED_TFile.hxx b/src/MEDWrapper/MED_TFile.hxx
new file mode 100644 (file)
index 0000000..10dc553
--- /dev/null
@@ -0,0 +1,93 @@
+// Copyright (C) 2021  CEA/DEN, EDF R&D
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
+
+#pragma once
+
+#include "MED_Wrapper.hxx"
+
+namespace MED
+{
+  class TFileInternal
+  {
+  public:
+    virtual ~TFileInternal() = default;
+    virtual void Open(EModeAcces theMode, TErr* theErr = nullptr) = 0;
+    virtual void Close() = 0;
+    virtual const TIdt& Id() const = 0;
+  };
+
+  class MEDIDTHoder : public TFileInternal
+  {
+  protected:
+    MEDIDTHoder(bool *isClosedStatus = nullptr):_isClosedStatus(isClosedStatus) { }
+    void UnRefFid()
+    {
+      if (--myCount == 0)
+      {
+        MEDfileClose(myFid);
+        myIsClosed = true;
+        if(_isClosedStatus)
+          *_isClosedStatus = true;
+      }
+    }
+  public:
+    const TIdt& Id() const override;
+    ~MEDIDTHoder() { this->UnRefFid(); }
+    void Close() override { this->UnRefFid(); }
+  protected:
+    TInt myCount = 0;
+    TIdt myFid = 0;
+    bool myIsClosed = false;
+    bool *_isClosedStatus = nullptr;
+  };
+
+  class TFileDecorator : public TFileInternal
+  {
+  public:
+    TFileDecorator(TFileInternal *effective):_effective(effective) { }
+    void Open(EModeAcces theMode, TErr* theErr = nullptr) override { if(_effective) _effective->Open(theMode,theErr); }
+    void Close() override { if(_effective) _effective->Close(); }
+    const TIdt& Id() const override { if(_effective) return _effective->Id(); EXCEPTION(std::runtime_error, "TFileDecorator - GetFid() : no effective TFile !"); }
+    ~TFileDecorator() { delete _effective; }
+  private:
+    TFileInternal *_effective = nullptr;
+  };
+
+  class TMemFile : public MEDIDTHoder
+  {
+  public:
+    TMemFile(bool* isClosedStatus = nullptr):MEDIDTHoder(isClosedStatus) { }
+    void Open(EModeAcces theMode, TErr* theErr = nullptr) override;
+    void *getData() const { return memfile.app_image_ptr; }
+    std::size_t getSize() const { return memfile.app_image_size; }
+  private:
+    med_memfile memfile = MED_MEMFILE_INIT;
+  };
+
+  class TFile : public MEDIDTHoder
+  {
+  public:
+    TFile(const std::string& theFileName, TInt theMajor=-1, TInt theMinor=-1);
+    void Open(EModeAcces theMode, TErr* theErr = nullptr) override;
+  protected:
+    std::string myFileName;
+    TInt myMajor;
+    TInt myMinor;
+  };
+}