Pointwise Plugin SDK
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
PwpAnchoredFileValue.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * template class PwpAnchoredFileValue
4  * template struct PwpAnchoredFileValueWriter
5  *
6  * (C) 2021 Cadence Design Systems, Inc. All rights reserved worldwide.
7  *
8  ***************************************************************************/
9 
10 #ifndef _PWPANCHOREDFILEVALUE_H_
11 #define _PWPANCHOREDFILEVALUE_H_
12 
13 #include "PwpFile.h"
14 #include "pwpPlatform.h"
15 
16 
25 template< typename ValType >
28 
38  static bool writeBin(bool placeholder, PwpFile &f, const ValType &v,
39  PWP_INT fldWidth, const char * suffix, const char * prefix)
40  {
41  (void)placeholder;
42  (void)fldWidth;
43  return f.write(v, suffix, prefix);
44  }
45 
46 
48 
58  static bool writeAsc(bool placeholder, PwpFile &f, const ValType &v,
59  PWP_INT fldWidth, const char * suffix, const char * prefix)
60  {
61  (void)placeholder;
62  return 0 != fprintf(f, "%s%*u%s", (prefix ? prefix : ""), (int)fldWidth,
63  (unsigned int)v, (suffix ? suffix : ""));
64  }
65 
66 
68 
78  static bool writeUnf(bool placeholder, PwpFile &f, const ValType &v,
79  PWP_INT fldWidth, const char * suffix, const char * prefix)
80  {
81  (void)fldWidth;
82  bool ret = false;
83  if (placeholder) {
84  // Writing the placeholder data value. Write normally so that the
85  // unf record byte count will properly increment.
86  ret = f.write(v, suffix, prefix);
87  }
88  else {
89  // Update anchored value. Bypass unf data handling and write value
90  // directly.
91  const void *p = PwuApplyEndianness(f.getByteOrder(), &v, sizeof(v));
92  ret = (1 == pwpFileWrite(p, sizeof(v), 1, f));
93  }
94  return ret;
95  }
96 };
97 
98 
167 template< typename ValType,
168  typename ValWriter = PwpAnchoredFileValueWriter<ValType> >
170 public:
171 
173 
182  anchored_(false),
183  v_(),
184  suffix_(0),
185  prefix_(0),
186  f_(f),
187  fldWidth_(-1) // -1 is left-aligned, packed
188  {
189  }
190 
191 
193 
206  PwpAnchoredFileValue(PwpFile &f, const ValType &initV,
207  const char * suffix = 0, const char * prefix = 0,
208  PWP_INT fldWidth = -1) :
209  anchored_(false),
210  v_(initV),
211  suffix_(suffix),
212  prefix_(prefix),
213  f_(f),
214  fldWidth_(fldWidth) // -1 is left-aligned, packed
215  {
216  }
217 
218 
220 
233  PwpAnchoredFileValue(PwpFile &f, const ValType &initV, PWP_INT fldWidth,
234  const char * suffix = 0, const char * prefix = 0) :
235  anchored_(false),
236  v_(initV),
237  suffix_(suffix),
238  prefix_(prefix),
239  f_(f),
240  fldWidth_(fldWidth)
241  {
242  }
243 
244 
246  {
247  }
248 
249 
252 
258  bool anchor(const char * suffix = 0, const char * prefix = 0,
259  PWP_INT fldWidth = 0)
260  {
261  if (0 != suffix) {
262  suffix_ = suffix;
263  }
264  if (0 != prefix) {
265  prefix_ = prefix;
266  }
267  if (0 != fldWidth) {
269  }
270  anchored_ = f_.getPos(pos_) && write();
271  return anchored_;
272  }
273 
274 
277 
283  bool anchor(PWP_INT fldWidth, const char * suffix = 0,
284  const char * prefix = 0)
285  {
286  return anchor(suffix, prefix, fldWidth);
287  }
288 
289 
292 
295  bool commit()
296  {
297  sysFILEPOS posSave;
298  return anchored_ && f_.getPos(posSave) && f_.setPos(pos_) &&
299  write() && f_.setPos(posSave);
300  }
301 
302 
305 
308  bool rollback()
309  {
310  bool ret = anchored_ && f_.setPos(pos_);
311  anchored_ = false;
312  return ret;
313  }
314 
315 
317 
320  ValType& val()
321  {
322  return v_;
323  }
324 
325 
327 
330  const ValType& val() const
331  {
332  return v_;
333  }
334 
335 
337 
340  operator const ValType&() const
341  {
342  return v_;
343  }
344 
345 
347 
350  operator ValType&()
351  {
352  return v_;
353  }
354 
355 
357 
360  const ValType& operator=(const ValType& rhs) const
361  {
362  v_ = rhs;
363  return v_;
364  }
365 
366 
368 
371  ValType& operator=(const ValType& rhs)
372  {
373  v_ = rhs;
374  return v_;
375  }
376 
377 
379 
382  const char* prefix() const
383  {
384  return prefix_ ? prefix_ : "";
385  }
386 
387 
389 
392  const char* suffix() const
393  {
394  return suffix_ ? suffix_ : "";
395  }
396 
397 
399 
402  PwpFile& f() const
403  {
404  return f_;
405  }
406 
407 
409 
412  const sysFILEPOS& pos() const
413  {
414  return pos_;
415  }
416 
417 
419 
423  {
424  return fldWidth_;
425  }
426 
427 
429 
432  bool isAnchored() const
433  {
434  return anchored_;
435  }
436 
437 
438 private:
439 
440  bool write()
441  {
442  bool ret = false;
443  if (f_.isAscii()) {
444  ret = ValWriter::writeAsc(!anchored_, f_, v_, fldWidth_, suffix_,
445  prefix_);
446  }
447  else if (f_.isBinary()) {
448  ret = ValWriter::writeBin(!anchored_, f_, v_, fldWidth_, suffix_,
449  prefix_);
450  }
451  else if (f_.isUnformatted()) {
452  ret = ValWriter::writeUnf(!anchored_, f_, v_, fldWidth_, suffix_,
453  prefix_);
454  }
455  return ret;
456  }
457 
460  {
461  return *this;
462  }
463 
464 
465 private:
466 
467  bool anchored_;
468  ValType v_;
470  const char * suffix_;
471  const char * prefix_;
474 };
475 
476 #endif // _PWPANCHOREDFILEVALUE_H_
PwpAnchoredFileValue(PwpFile &f)
Constructor.
PwpFile & f() const
Gets the file associated with the anchored value.
const char * prefix() const
Gets the current value prefix.
static bool writeAsc(bool placeholder, PwpFile &f, const ValType &v, PWP_INT fldWidth, const char *suffix, const char *prefix)
Handles the writing an anchored value to an ASCII file.
bool commit()
Writes the value to the associated file&#39;s anchored file position.
bool isUnformatted() const
Determines if file was opened with pwpUnformatted mode flag.
Definition: PwpFile.cxx:828
bool isAscii() const
Determines if file was opened with either the pwpAscii or pwpFormatted mode flag. ...
Definition: PwpFile.cxx:687
bool setPos(const sysFILEPOS &pos)
Set the current file position.
Definition: PwpFile.cxx:881
PWP_ENDIANNESS getByteOrder(bool mapBigLittle=false) const
Get the byte order used for writes.
Definition: PwpFile.cxx:636
Handles the delayed updating of a value in a PwpFile object.
PWP_INT fldWidth() const
Gets the current value field width.
static bool writeUnf(bool placeholder, PwpFile &f, const ValType &v, PWP_INT fldWidth, const char *suffix, const char *prefix)
Handles the writing an anchored value to an fortran unformatted file.
bool anchor(PWP_INT fldWidth, const char *suffix=0, const char *prefix=0)
Anchors the value to the associated file&#39;s current file position and writes a placeholder value to th...
bool anchor(const char *suffix=0, const char *prefix=0, PWP_INT fldWidth=0)
Anchors the value to the associated file&#39;s current file position and writes a placeholder value to th...
const char * suffix() const
Gets the current value suffix.
PwpAnchoredFileValue(PwpFile &f, const ValType &initV, const char *suffix=0, const char *prefix=0, PWP_INT fldWidth=-1)
Constructor.
fpos_t sysFILEPOS
File position data type.
Definition: pwpPlatform.h:51
bool getPos(sysFILEPOS &pos) const
Get the current file position.
Definition: PwpFile.cxx:849
Cross Platform Functions.
bool isAnchored() const
Gets the current value anchoring status.
const void * PwuApplyEndianness(PWP_ENDIANNESS endianness, const void *buf, size_t size)
Apply specified byte order to buf containing size bytes.
const ValType & val() const
Gets the current data value.
virtual bool write(PWP_INT64 val, const char *suffix=0, const char *prefix=0)
Writes a integer value with proper encoding and byte order.
Definition: PwpFile.cxx:1233
Writes solver files.
Definition: PwpFile.h:110
bool isBinary() const
Determines if file was opened with pwpBinary mode flag.
Definition: PwpFile.cxx:821
The default ValWriter type used by PwpAnchoredFileValue&lt;ValWriter&gt;.
PwpAnchoredFileValue< ValType, ValWriter > & operator=(const PwpAnchoredFileValue< ValType, ValWriter > &)
static bool writeBin(bool placeholder, PwpFile &f, const ValType &v, PWP_INT fldWidth, const char *suffix, const char *prefix)
Handles the writing an anchored value to a binary file.
size_t pwpFileWrite(const void *buf, size_t size, size_t count, FILE *fp)
Write an collection of data items to a file.
PwpAnchoredFileValue(PwpFile &f, const ValType &initV, PWP_INT fldWidth, const char *suffix=0, const char *prefix=0)
Constructor.
ValType & val()
Gets the current data value.
bool rollback()
Moves the associated file to the anchored file position and unanchors the value.
PWP_INT32 PWP_INT
integer same size as void*
Definition: apiPWP.h:282
const sysFILEPOS & pos() const
Gets the anchor position associated with the file.
ValType & operator=(const ValType &rhs)
ValType assignment operator.
const ValType & operator=(const ValType &rhs) const
const ValType assignment operator.