Pointwise Plugin SDK
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),
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::PwpAnchoredFileValue
PwpAnchoredFileValue(PwpFile &f, const ValType &initV, PWP_INT fldWidth, const char *suffix=0, const char *prefix=0)
Constructor.
Definition: PwpAnchoredFileValue.h:233
PwpFile::setPos
bool setPos(const sysFILEPOS &pos)
Set the current file position.
Definition: PwpFile.cxx:881
PwpAnchoredFileValue::val
const ValType & val() const
Gets the current data value.
Definition: PwpAnchoredFileValue.h:330
PwpAnchoredFileValue::pos_
sysFILEPOS pos_
Definition: PwpAnchoredFileValue.h:469
sysFILEPOS
fpos_t sysFILEPOS
File position data type.
Definition: pwpPlatform.h:51
PwpAnchoredFileValue::anchored_
bool anchored_
Definition: PwpAnchoredFileValue.h:467
PwpAnchoredFileValue::operator=
PwpAnchoredFileValue< ValType, ValWriter > & operator=(const PwpAnchoredFileValue< ValType, ValWriter > &)
Definition: PwpAnchoredFileValue.h:458
PwpAnchoredFileValue::suffix
const char * suffix() const
Gets the current value suffix.
Definition: PwpAnchoredFileValue.h:392
PwpAnchoredFileValue::f_
PwpFile & f_
Definition: PwpAnchoredFileValue.h:472
PwpAnchoredFileValue::fldWidth_
PWP_INT fldWidth_
Definition: PwpAnchoredFileValue.h:473
PwpAnchoredFileValue::anchor
bool anchor(const char *suffix=0, const char *prefix=0, PWP_INT fldWidth=0)
Anchors the value to the associated file's current file position and writes a placeholder value to th...
Definition: PwpAnchoredFileValue.h:258
PwpFile::isUnformatted
bool isUnformatted() const
Determines if file was opened with pwpUnformatted mode flag.
Definition: PwpFile.cxx:828
PwpAnchoredFileValue::f
PwpFile & f() const
Gets the file associated with the anchored value.
Definition: PwpAnchoredFileValue.h:402
PwpFile::getPos
bool getPos(sysFILEPOS &pos) const
Get the current file position.
Definition: PwpFile.cxx:849
PwpAnchoredFileValueWriter::writeBin
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.
Definition: PwpAnchoredFileValue.h:38
PwpAnchoredFileValue::prefix_
const char * prefix_
Definition: PwpAnchoredFileValue.h:471
PwpAnchoredFileValue::anchor
bool anchor(PWP_INT fldWidth, const char *suffix=0, const char *prefix=0)
Anchors the value to the associated file's current file position and writes a placeholder value to th...
Definition: PwpAnchoredFileValue.h:283
PwpAnchoredFileValue::suffix_
const char * suffix_
Definition: PwpAnchoredFileValue.h:470
PwpAnchoredFileValueWriter
The default ValWriter type used by PwpAnchoredFileValue<ValWriter>.
Definition: PwpAnchoredFileValue.h:26
PwpAnchoredFileValue::rollback
bool rollback()
Moves the associated file to the anchored file position and unanchors the value.
Definition: PwpAnchoredFileValue.h:308
pwpPlatform.h
Cross Platform Functions.
PwpAnchoredFileValue::PwpAnchoredFileValue
PwpAnchoredFileValue(PwpFile &f, const ValType &initV, const char *suffix=0, const char *prefix=0, PWP_INT fldWidth=-1)
Constructor.
Definition: PwpAnchoredFileValue.h:206
PwpAnchoredFileValue::val
ValType & val()
Gets the current data value.
Definition: PwpAnchoredFileValue.h:320
PwpAnchoredFileValue::isAnchored
bool isAnchored() const
Gets the current value anchoring status.
Definition: PwpAnchoredFileValue.h:432
PwpAnchoredFileValue::PwpAnchoredFileValue
PwpAnchoredFileValue(PwpFile &f)
Constructor.
Definition: PwpAnchoredFileValue.h:181
PwpAnchoredFileValueWriter::writeUnf
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.
Definition: PwpAnchoredFileValue.h:78
PwpAnchoredFileValue
Handles the delayed updating of a value in a PwpFile object.
Definition: PwpAnchoredFileValue.h:169
PwpAnchoredFileValue::fldWidth
PWP_INT fldWidth() const
Gets the current value field width.
Definition: PwpAnchoredFileValue.h:422
PwpFile::write
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:1253
PWP_INT
PWP_INT32 PWP_INT
integer same size as void*
Definition: apiPWP.h:282
PwpAnchoredFileValue::operator=
const ValType & operator=(const ValType &rhs) const
const ValType assignment operator.
Definition: PwpAnchoredFileValue.h:360
PwpAnchoredFileValue::write
bool write()
Definition: PwpAnchoredFileValue.h:440
PwpAnchoredFileValue::pos
const sysFILEPOS & pos() const
Gets the anchor position associated with the file.
Definition: PwpAnchoredFileValue.h:412
pwpFileWrite
size_t pwpFileWrite(const void *buf, size_t size, size_t count, FILE *fp)
Write an collection of data items to a file.
Definition: pwpPlatform.cxx:402
PwpAnchoredFileValue::operator=
ValType & operator=(const ValType &rhs)
ValType assignment operator.
Definition: PwpAnchoredFileValue.h:371
PwpFile::isAscii
bool isAscii() const
Determines if file was opened with either the pwpAscii or pwpFormatted mode flag.
Definition: PwpFile.cxx:687
PwuApplyEndianness
const void * PwuApplyEndianness(PWP_ENDIANNESS endianness, const void *buf, size_t size)
Apply specified byte order to buf containing size bytes.
Definition: apiPWPUtils.cxx:230
PwpAnchoredFileValue::v_
ValType v_
Definition: PwpAnchoredFileValue.h:468
PwpFile::isBinary
bool isBinary() const
Determines if file was opened with pwpBinary mode flag.
Definition: PwpFile.cxx:821
PwpFile
Writes solver files.
Definition: PwpFile.h:110
PwpFile::getByteOrder
PWP_ENDIANNESS getByteOrder(bool mapBigLittle=false) const
Get the byte order used for writes.
Definition: PwpFile.cxx:636
PwpAnchoredFileValue::prefix
const char * prefix() const
Gets the current value prefix.
Definition: PwpAnchoredFileValue.h:382
PwpAnchoredFileValue::~PwpAnchoredFileValue
~PwpAnchoredFileValue()
Definition: PwpAnchoredFileValue.h:245
PwpAnchoredFileValueWriter::writeAsc
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.
Definition: PwpAnchoredFileValue.h:58
PwpAnchoredFileValue::commit
bool commit()
Writes the value to the associated file's anchored file position.
Definition: PwpAnchoredFileValue.h:295
PwpFile.h