IOobject.hpp
Go to the documentation of this file.
1 /*------------------------------- phasicFlow ---------------------------------
2  O C enter of
3  O O E ngineering and
4  O O M ultiscale modeling of
5  OOOOOOO F luid flow
6 ------------------------------------------------------------------------------
7  Copyright (C): www.cemf.ir
8  email: hamid.r.norouzi AT gmail.com
9 ------------------------------------------------------------------------------
10 Licence:
11  This file is part of phasicFlow code. It is a free software for simulating
12  granular and multiphase flows. You can redistribute it and/or modify it under
13  the terms of GNU General Public License v3 or any other later versions.
14 
15  phasicFlow is distributed to help others in their research in the field of
16  granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
17  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 
19 -----------------------------------------------------------------------------*/
20 
21 
22 #ifndef __IOobject_hpp__
23 #define __IOobject_hpp__
24 
25 
26 
27 #include "IOfileHeader.hpp"
28 
29 
30 namespace pFlow
31 {
32 
33 class repository;
34 
35 class IOobject
36 :
37  public IOfileHeader
38 {
39 
40 private:
41 
42  class iObject
43  {
44  public:
45 
46  virtual ~iObject()=default;
47 
48  // - clone
49  virtual uniquePtr<iObject> clone() const = 0;
50 
51  virtual word typeName()const = 0;
52 
53  virtual bool read_object_t(iIstream& is) = 0;
54 
55  virtual bool write_object_t(iOstream& os)const = 0;
56 
57  };
58 
59  template<typename dataType>
60  class object_t
61  :
62  public iObject
63  {
64  public:
65  dataType data_;
66 
67  public:
68 
69  template<typename... Args,
70  typename = std::enable_if_t<!std::is_constructible<object_t, Args&&...>::value>>
71  object_t(Args&&... args)
72  :
73  data_(std::forward<Args>(args)...)
74  {}
75 
76  // cunstruct by copying data
77  object_t(const dataType& data): data_(data){}
78 
79  // construct by moving data
80  //object_t(dataType&& data): data_(std::move(data)){}
81 
82 
83  virtual uniquePtr<iObject> clone() const
84  {
85  return makeUnique<object_t>(*this);
86  }
87 
88  virtual word typeName()const
89  {
90  return data_.typeName();
91  }
92 
93  virtual bool read_object_t(iIstream& is)
94  {
95  return data_.read(is);
96  }
97 
98  virtual bool write_object_t(iOstream& os)const
99  {
100  return data_.write(os);
101  }
102 
103  };
104 
105 protected:
106 
108 
109  // underlaying data object
111 
112 
113 public:
114 
115  // - typeinfo
116  word typeName()const
117  {
118  return object_->typeName();
119  }
120 
122 
123  // - construct from components, transfer the ownership of iObject (object_t) to the
124  // onwner and read the object from file
125  IOobject(const objectFile& objf, const repository* owner, uniquePtr<iObject>&& obj );
126 
127  // - construct from components, transfer the ownership of IOobject to the owner (no read happens)
128  IOobject(const objectFile& objf, const repository* owner, uniquePtr<IOobject>&& obj);
129 
130  // - copy construct
131  IOobject(const IOobject& src)=delete;
132 
133  // - move construct
134  IOobject(IOobject&& src) = default;
135 
136 
137  // - make object from components, considering no owner for this object, and
138  // read from file
139  // Args are the arguments of object constructor
140  template<typename T, typename... Args>
141  static auto make(const objectFile& objf, Args&&... args);
142 
143  // - construct object_t with the Args as the arguments of object constructor
144  template<typename T, typename... Args>
145  static auto make_object_t(Args&&... args);
146 
147 
149 
150  // - is object valid
151  bool isObjectValid()const;
152 
153  // - ref to data object
154  template<typename T>
155  auto& getObject();
156 
157  // - const ref to data object
158  template<typename T>
159  const auto& getObject()const;
160 
161 
162 
164 
165  // - read from file
166  bool read(bool rdHdr = true);
167 
168  // - write to file
169  bool write() const;
170 
171  // - read from istream
172  bool read(iIstream& is, bool rdHdr = true);
173 
174  // - write to istream
175  bool write(iOstream& os) const;
176 
177 
178 };
179 
180 }
181 
182 #include "IOobjectTemplates.cpp"
183 
184 #endif //__IOobject_hpp__
pFlow::IOobject::write
bool write() const
Definition: IOobject.cpp:83
pFlow::IOobject::iObject::read_object_t
virtual bool read_object_t(iIstream &is)=0
pFlow::IOobject::object_t::object_t
object_t(const dataType &data)
Definition: IOobject.hpp:77
pFlow::IOobject::iObject::clone
virtual uniquePtr< iObject > clone() const =0
pFlow::IOobject::object_t
Definition: IOobject.hpp:60
IOobjectTemplates.cpp
pFlow::IOobject::object_t::object_t
object_t(Args &&... args)
Definition: IOobject.hpp:71
pFlow::IOobject::IOobject
IOobject(const objectFile &objf, const repository *owner, uniquePtr< iObject > &&obj)
Definition: IOobject.cpp:26
pFlow::word
std::string word
Definition: builtinTypes.hpp:63
pFlow::IOobject::iObject::typeName
virtual word typeName() const =0
pFlow::IOobject::object_t::clone
virtual uniquePtr< iObject > clone() const
Definition: IOobject.hpp:83
pFlow::IOobject::getObject
auto & getObject()
Definition: IOobjectTemplates.cpp:42
pFlow::IOobject::isObjectValid
bool isObjectValid() const
Definition: IOobject.cpp:58
pFlow
Definition: demComponent.hpp:28
pFlow::IOobject
Definition: IOobject.hpp:35
pFlow::IOobject::object_
uniquePtr< iObject > object_
Definition: IOobject.hpp:110
pFlow::IOobject::make_object_t
static auto make_object_t(Args &&... args)
Definition: IOobjectTemplates.cpp:35
pFlow::IOobject::iObject
Definition: IOobject.hpp:42
pFlow::iIstream
Definition: iIstream.hpp:33
pFlow::IOobject::object_t::read_object_t
virtual bool read_object_t(iIstream &is)
Definition: IOobject.hpp:93
pFlow::IOfileHeader::owner
const repository * owner() const
Definition: IOfileHeader.hpp:78
pFlow::IOobject::object_t::write_object_t
virtual bool write_object_t(iOstream &os) const
Definition: IOobject.hpp:98
pFlow::IOobject::read
bool read(bool rdHdr=true)
Definition: IOobject.cpp:63
pFlow::objectFile
Definition: objectFile.hpp:33
IOfileHeader.hpp
pFlow::IOobject::object_t::typeName
virtual word typeName() const
Definition: IOobject.hpp:88
pFlow::IOobject::object_t::data_
dataType data_
Definition: IOobject.hpp:65
pFlow::IOfileHeader
Definition: IOfileHeader.hpp:35
pFlow::uniquePtr
Definition: uniquePtr.hpp:44
pFlow::IOobject::make
static auto make(const objectFile &objf, Args &&... args)
Definition: IOobjectTemplates.cpp:23
pFlow::repository
Definition: repository.hpp:34
pFlow::IOobject::iObject::~iObject
virtual ~iObject()=default
pFlow::IOobject::iObject::write_object_t
virtual bool write_object_t(iOstream &os) const =0
pFlow::IOobject::typeName
word typeName() const
Definition: IOobject.hpp:116
pFlow::iOstream
Definition: iOstream.hpp:53