Rheolef  7.2
an efficient C++ finite element environment
iorheobase.h
Go to the documentation of this file.
1 # ifndef _IORHEOBASE_H
2 # define _IORHEOBASE_H
23 
24 #include "rheolef/compiler.h"
25 namespace rheolef {
26 
27 // --------------------------------------------------------------
28 // header macro definitions, for code reduction purpose
29 // --------------------------------------------------------------
30 
31 #define iorheobase_def_boolean_accessor_macro(ioclass,stream,name) \
32  static bool get##name (stream& s) \
33  { \
34  return ioclass::flags(s) [name]; \
35  }
36 #define iorheobase_manip_boolean_accessor_macro(ioclass,stream,name) \
37  inline stream& name (stream& s) \
38  { \
39  ioclass::setf (s, ioclass::name); \
40  return s; \
41  } \
42  inline stream& no##name (stream& s) \
43  { \
44  ioclass::unsetf (s, ioclass::name); \
45  return s; \
46  }
47  // ------------------------------------------------------
48 #define iorheobase_def_member_macro(ioclass,stream,name,groupe) \
49  iorheobase_def_boolean_accessor_macro(ioclass,stream,name)
50 
51 #define iorheobase_manip_member_macro(ioclass,stream,name,groupe) \
52  inline stream& name (stream& s) \
53  { \
54  ioclass::setf(s, ioclass::name, ioclass::groupe##_field); \
55  return s; \
56  }
57  // ------------------------------------------------------
58 #define iorheobase_def_scalar_macro(ioclass,t, a) \
59  protected: \
60  t a ## _; \
61  const t& a () const; \
62  void a (const t& x); \
63  public: \
64  static const t& get##a (std::ios& s); \
65  static std::ios& set##a (std::ios& s, const t& x);
66 
67 #define iorheobase_manip_scalar_macro(ioclass,t, a, io) \
68  inline io##rheomanip1<t> set##a (const t& x) \
69  { \
70  return io##rheomanip1<t>(&(ioclass::set##a), x); \
71  }
72 
73 // --------------------------------------------------------------
74 // body macro definitions, for code reduction purpose
75 // --------------------------------------------------------------
76 # define iorheobase_io_scalar_body_macro(ioclass,t,a) \
77 const t& \
78 ioclass::a () const \
79 { \
80  return a##_; \
81 } \
82 void \
83 ioclass::a (const t& x) \
84 { \
85  a##_ = x; \
86 } \
87 const t& \
88 ioclass::get##a (std::ios& s) \
89 { \
90  return (ioclass::get_pointer(s) -> a()); \
91 } \
92 std::ios& \
93 ioclass::set##a (std::ios& s, const t& x) \
94 { \
95  (ioclass::get_pointer(s) -> a(x)); \
96  return s; \
97 }
98 // --------------------------------------------------------------
99 // io manipulators
100 // --------------------------------------------------------------
101 template <class T>
102 struct irheomanip1 {
103  irheomanip1 (std::ios& (*ff)(std::ios&, const T&), const T& ii)
104  : f(ff), i(ii) {}
105 // data:
106  std::ios& (*f) (std::ios&, const T&);
107  T i;
108 };
109 template <class T>
110 inline
111 std::istream&
112 operator>> (std::istream& is, const irheomanip1<T>& m)
113 {
114  m.f (is, m.i);
115  return is;
116 }
117 template <class T>
118 struct orheomanip1 {
119  orheomanip1 (std::ios& (*ff)(std::ios&, const T&), const T& ii)
120  : f(ff), i(ii) {}
121 // data:
122  std::ios& (*f) (std::ios&, const T&);
123  T i;
124 };
125 template <class T>
126 inline
127 std::ostream&
128 operator<< (std::ostream& os, const orheomanip1<T>& m)
129 {
130  m.f (os, m.i);
131  return os;
132 }
133 template <class T>
134 struct iorheomanip1 {
135  iorheomanip1 (std::ios& (*ff)(std::ios&, const T&), const T& ii)
136  : f(ff), i(ii) {}
137 // data:
138  std::ios& (*f) (std::ios&, const T&);
139  T i;
140 };
141 template <class T>
142 inline
143 std::istream&
144 operator>> (std::istream& is, const iorheomanip1<T>& m)
145 {
146  m.f (is, m.i);
147  return is;
148 }
149 template <class T>
150 inline
151 std::ostream&
152 operator<< (std::ostream& os, const iorheomanip1<T>& m)
153 {
154  m.f (os, m.i);
155  return os;
156 }
157 // ---------------------------------------------------------------------
158 // iofem trivial memory handler
159 // TODO: a global destructor that delete the list
160 // when global cstor/dstor works...
161 // ---------------------------------------------------------------------
162 
163 
164 template <class T>
166 public:
167  static std::list<T*> *pointer_list;
168  static inline T* iorheobase_alloc () {
169  static bool init = false;
170  if (!init) {
171  pointer_list = new_macro(std::list<T*>);
172  init = true;
173  }
174  T *p = new_macro(T);
175  (*pointer_list).push_back(p);
176  return p;
177  }
178  //
179  // io-manips / memory interface
180  //
181  static inline T* get_pointer (std::ios& io) {
182  static int iobaseidx = 0;
183  static bool iobaseidx_initialized = false;
184  if (!iobaseidx_initialized) {
185  iobaseidx = std::ios::xalloc();
186  iobaseidx_initialized = true;
187  }
188  T* p = (T*)io.pword(iobaseidx);
189 
190 #ifdef _RHEOLEF_HAVE_IOS_BITALLOC
191  // io.pword(iobaseidx) n'est pas forcement nul la premiere fois
192  // e.g. CRAY C++ ne l'initialise pas
193  // => peut valoir une valeur arbitraire...
194  // pas moyen de savoir quand il faut l'initialiser
195  // std::ios::bitalloc: un bit va permettre de dire
196  // si on a deja initialise'
197  // bitalloc = pas standard (e.g. pas avec KAI C++)
198  // mais permet de contourner le bug du
199  // compilateur cray c++
200  // le script configure teste si bitalloc() existe
201  static long iobase_bitidx;
202  static bool iobase_bitidx_initialized = false;
203  long bit;
204  if (!iobase_bitidx_initialized) {
205  iobase_bitidx = std::ios::bitalloc();
206  iobase_bitidx_initialized = true;
207  }
208  long bit_init;
209  if (iobase_bitidx == 0) {
210  // no bits are still available in std::ios...
211  bit_init = (p != 0);
212  } else {
213  // get iobase_bitidx flag in io.flags:
214  long io_flags = io.flags();
215  bit_init = io_flags & iobase_bitidx;
216  // set iobase_bitidx flag to true in io.flags:
217  io.flags(io_flags | iobase_bitidx);
218  }
219 #else // ! _RHEOLEF_HAVE_IOS_BITALLOC
220  long bit_init = (p != 0);
221 #endif // _RHEOLEF_HAVE_IOS_BITALLOC
222 
223  if (bit_init == 0) {
224  if (p != 0) {
225  warning_macro ("ios::pwords(int) return value not initialized (fixed)");
226  }
227  io.pword(iobaseidx) = p = iorheobase_alloc();
228  } else {
229  if (p == 0) {
230  fatal_macro ("ios::pwords(int) return null value: memory corrupted");
231  }
232  }
233  return p;
234  }
235 };
236 }// namespace rheolef
237 # endif /* _IORHEOBASE_H */
static T * get_pointer(std::ios &io)
Definition: iorheobase.h:181
static std::list< T * > * pointer_list
Definition: iorheobase.h:167
#define fatal_macro(message)
Definition: dis_macros.h:33
#define warning_macro(message)
Definition: dis_macros.h:53
Expr1::float_type T
Definition: field_expr.h:230
This file is part of Rheolef.
std::istream & operator>>(std::istream &is, const catchmark &m)
Definition: catchmark.h:88
std::ostream & operator<<(std::ostream &os, const catchmark &m)
Definition: catchmark.h:99
Definition: cavity_dg.h:29
Definition: sphere.icc:25
iorheomanip1(std::ios &(*ff)(std::ios &, const T &), const T &ii)
Definition: iorheobase.h:135
irheomanip1(std::ios &(*ff)(std::ios &, const T &), const T &ii)
Definition: iorheobase.h:103
orheomanip1(std::ios &(*ff)(std::ios &, const T &), const T &ii)
Definition: iorheobase.h:119