cisst-saw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cmnPrintf.h
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ex: set filetype=cpp softtabstop=4 shiftwidth=4 tabstop=4 cindent expandtab: */
3 
4 /*
5 
6  Author(s): Ofri Sadowsky
7  Created on: 2006-02-15
8 
9  (C) Copyright 2006-2007 Johns Hopkins University (JHU), All Rights
10  Reserved.
11 
12 --- begin cisst license - do not edit ---
13 
14 This software is provided "as is" under an open source license, with
15 no warranty. The complete license can be found in license.txt and
16 http://www.cisst.org/cisst/license.txt.
17 
18 --- end cisst license ---
19 
20 */
21 
22 
23 
29 #pragma once
30 
31 #ifndef _cmnPrintf_h
32 #define _cmnPrintf_h
33 
35 
36 #include <iostream>
37 
38 // always include last
39 #include <cisstCommon/cmnExport.h>
40 
41 /* Forward declarations */
42 class cmnPrintfParser;
43 
44 
45 #ifndef DOXYGEN
46 CISST_EXPORT bool cmnTypePrintf(cmnPrintfParser & parser, const int number);
47 CISST_EXPORT bool cmnTypePrintf(cmnPrintfParser & parser, const unsigned int number);
48 CISST_EXPORT bool cmnTypePrintf(cmnPrintfParser & parser, const short number);
49 CISST_EXPORT bool cmnTypePrintf(cmnPrintfParser & parser, const unsigned short number);
50 CISST_EXPORT bool cmnTypePrintf(cmnPrintfParser & parser, const long number);
51 CISST_EXPORT bool cmnTypePrintf(cmnPrintfParser & parser, const unsigned long number);
52 CISST_EXPORT bool cmnTypePrintf(cmnPrintfParser & parser, const double number);
53 CISST_EXPORT bool cmnTypePrintf(cmnPrintfParser & parser, const char * text);
54 #endif
55 
56 
162 {
163 public:
167  cmnPrintf(const std::string & formatStr)
168  : FormatString(formatStr)
169  {}
170 
173  {}
174 
178  cmnPrintf(const cmnPrintf & other):
179  FormatString(other.FormatString)
180  {}
181 
182 private:
184  std::string FormatString;
185 
186  friend class cmnPrintfParser;
187 
190  const std::string & GetFormat(void) const
191  {
192  return FormatString;
193  }
194 };
195 
196 
228 {
229 
230 public:
237  cmnPrintfParser(std::ostream & output, const cmnPrintf & outputFormat);
238 
243  void RawOutput(const char * text)
244  {
245  OutputStream << text;
246  }
247 
251  template<class _argType>
252  inline cmnPrintfParser &
253  operator << (const _argType & dataOut)
254  {
255  if (NextFormatTextPosition == 0)
256  return *this;
257  cmnTypePrintf(*this, dataOut);
258  AdvanceToNextFormat();
259  return *this;
260  }
261 
273  inline cmnPrintfParser &
274  operator << (const int dataOut)
275  {
276  if (NextFormatTextPosition == 0)
277  return *this;
278  cmnTypePrintf(*this, dataOut);
279  AdvanceToNextFormat();
280  return *this;
281  }
282 
286  inline cmnPrintfParser &
287  operator << (const std::string & dataOut)
288  {
289  if (NextFormatTextPosition == 0)
290  return *this;
291  cmnTypePrintf(*this, dataOut.c_str());
292  AdvanceToNextFormat();
293  return *this;
294  }
295 
307 #if CISST_OSTREAM_CAN_CAST_TO_VOID_PTR
308  inline operator void *(void)
309  {
310  return (void *)(OutputStream);
311  }
312 #elif CISST_OSTREAM_CAN_CAST_TO_INT
313  inline operator bool (void)
314  {
315  return OutputStream;
316  }
317 #else
318  inline operator bool(void)
319  {
320  return OutputStream.good();
321  }
322 #endif
323 
327  enum {BUFFER_SIZE = 256};
328 
329 
330 private:
333  void DumpUntilPercent(void);
334 
337  void ProcessPercent(void);
338 
340  std::ostream & OutputStream;
341 
343  const cmnPrintf & OutputFormatter;
344 
346  char * NextFormatTextPosition;
347 
351  char NextFormatTextCharacter;
352 
355  char * FormatSequence;
356 
359  char NextTypeIdCharacter;
360 
366  void AdvanceToNextFormat(void)
367  {
368  FormatSequence = 0;
369  DumpUntilPercent();
370  ProcessPercent();
371  }
372 
373 public:
374 
377  static const char * TypeIdCharset;
378 
380  static const char * IntegerTypeIds;
381 
383  static const char * FloatTypeIds;
384 
386  static const char * StringTypeIds;
387 
392  static inline const char * TypeIdString(double CMN_UNUSED(value))
393  {
394  return FloatTypeIds;
395  }
396 
401  static inline const char * TypeIdString(float CMN_UNUSED(value))
402  {
403  return FloatTypeIds;
404  }
405 
410  static inline const char * TypeIdString(int CMN_UNUSED(value))
411  {
412  return IntegerTypeIds;
413  }
414 
419  static inline const char * TypeIdString(unsigned int CMN_UNUSED(value))
420  {
421  return IntegerTypeIds;
422  }
423 
428  static inline const char * TypeIdString(long CMN_UNUSED(value))
429  {
430  return IntegerTypeIds;
431  }
432 
437  static inline const char * TypeIdString(unsigned long CMN_UNUSED(value))
438  {
439  return IntegerTypeIds;
440  }
441 
450  template<typename _outputType>
451  bool MatchOutputWithFormatChar(const _outputType & data)
452  {
453  const char * formatCharsForData = TypeIdString(data);
454  const bool result = this->NextTypeIdCharIsOneOf(formatCharsForData);
455  return result;
456  }
457 
464  bool NextTypeIdCharIsOneOf(const char * typeIdCharset) const;
465 
466  void SuspendOutput(void)
467  {
468  NextFormatTextPosition = 0;
469  }
470 
474  const char * GetNextFormatSequence(void) const
475  {
476  return FormatSequence;
477  }
478 
480  char GetNextTypeIdCharacter(void) const
481  {
482  return NextTypeIdCharacter;
483  }
484 };
485 
486 
495 inline cmnPrintfParser
496 operator << (std::ostream & outputStream, const cmnPrintf & formatStr)
497 {
498  return cmnPrintfParser(outputStream, formatStr);
499 }
500 
501 
502 #endif // _cmnPrintf_h
503 
#define CISST_EXPORT
Definition: cmnExportMacros.h:50
const char * GetNextFormatSequence(void) const
Definition: cmnPrintf.h:474
static const char * TypeIdString(unsigned long CMN_UNUSED(value))
Definition: cmnPrintf.h:437
Portability across compilers and operating systems tools.
cmnPrintfParser operator<<(std::ostream &outputStream, const cmnPrintf &formatStr)
Definition: cmnPrintf.h:496
#define CMN_UNUSED(argument)
Definition: cmnPortability.h:479
bool MatchOutputWithFormatChar(const _outputType &data)
Definition: cmnPrintf.h:451
static const char * StringTypeIds
Definition: cmnPrintf.h:386
static const char * TypeIdString(long CMN_UNUSED(value))
Definition: cmnPrintf.h:428
void SuspendOutput(void)
Definition: cmnPrintf.h:466
cmnPrintf(const std::string &formatStr)
Definition: cmnPrintf.h:167
cmnPrintf(const cmnPrintf &other)
Definition: cmnPrintf.h:178
Macros to export the symbols of cisstCommon (in a Dll).
static const char * TypeIdString(unsigned int CMN_UNUSED(value))
Definition: cmnPrintf.h:419
char GetNextTypeIdCharacter(void) const
Definition: cmnPrintf.h:480
printf-like formatted output for streams
Definition: cmnPrintf.h:161
static const char * FloatTypeIds
Definition: cmnPrintf.h:383
static const char * TypeIdString(double CMN_UNUSED(value))
Definition: cmnPrintf.h:392
CISST_EXPORT bool cmnTypePrintf(cmnPrintfParser &parser, const int number)
static const char * TypeIdCharset
Definition: cmnPrintf.h:377
void RawOutput(const char *text)
Definition: cmnPrintf.h:243
static const char * IntegerTypeIds
Definition: cmnPrintf.h:380
~cmnPrintf()
Definition: cmnPrintf.h:172
static const char * TypeIdString(float CMN_UNUSED(value))
Definition: cmnPrintf.h:401
Parser for cmnPrintf.
Definition: cmnPrintf.h:227
static const char * TypeIdString(int CMN_UNUSED(value))
Definition: cmnPrintf.h:410