cisst-saw
Loading...
Searching...
No Matches
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 Author(s): Ofri Sadowsky
6 Created on: 2006-02-15
7
8 (C) Copyright 2006-2018 Johns Hopkins University (JHU), All Rights Reserved.
9
10--- begin cisst license - do not edit ---
11
12This software is provided "as is" under an open source license, with
13no warranty. The complete license can be found in license.txt and
14http://www.cisst.org/cisst/license.txt.
15
16--- end cisst license ---
17
18*/
19
20
26#pragma once
27
28#ifndef _cmnPrintf_h
29#define _cmnPrintf_h
30
32
33#include <iostream>
34
35// always include last
37
38/* Forward declarations */
39class cmnPrintfParser;
40
41
42#ifndef DOXYGEN
43CISST_EXPORT bool cmnTypePrintf(cmnPrintfParser & parser, const int number);
44CISST_EXPORT bool cmnTypePrintf(cmnPrintfParser & parser, const unsigned int number);
45CISST_EXPORT bool cmnTypePrintf(cmnPrintfParser & parser, const short number);
46CISST_EXPORT bool cmnTypePrintf(cmnPrintfParser & parser, const unsigned short number);
47CISST_EXPORT bool cmnTypePrintf(cmnPrintfParser & parser, const long number);
48CISST_EXPORT bool cmnTypePrintf(cmnPrintfParser & parser, const unsigned long number);
49CISST_EXPORT bool cmnTypePrintf(cmnPrintfParser & parser, const double number);
50CISST_EXPORT bool cmnTypePrintf(cmnPrintfParser & parser, const char * text);
51#endif
52
53
159{
160public:
164 cmnPrintf(const std::string & formatStr)
165 : FormatString(formatStr)
166 {}
167
170 {}
171
175 cmnPrintf(const cmnPrintf & other):
176 FormatString(other.FormatString)
177 {}
178
179private:
181 std::string FormatString;
182
183 friend class cmnPrintfParser;
184
187 const std::string & GetFormat(void) const
188 {
189 return FormatString;
190 }
191};
192
193
224class CISST_EXPORT cmnPrintfParser
225{
226
227public:
234 cmnPrintfParser(std::ostream & output, const cmnPrintf & outputFormat);
235
240 void RawOutput(const char * text)
241 {
242 OutputStream << text;
243 }
244
248 template<class _argType>
249 inline cmnPrintfParser &
250 operator << (const _argType & dataOut)
251 {
252 if (NextFormatTextPosition == 0)
253 return *this;
254 cmnTypePrintf(*this, dataOut);
255 AdvanceToNextFormat();
256 return *this;
257 }
258
270 inline cmnPrintfParser &
271 operator << (const int dataOut)
272 {
273 if (NextFormatTextPosition == 0)
274 return *this;
275 cmnTypePrintf(*this, dataOut);
276 AdvanceToNextFormat();
277 return *this;
278 }
279
283 inline cmnPrintfParser &
284 operator << (const std::string & dataOut)
285 {
286 if (NextFormatTextPosition == 0)
287 return *this;
288 cmnTypePrintf(*this, dataOut.c_str());
289 AdvanceToNextFormat();
290 return *this;
291 }
292
296 enum {BUFFER_SIZE = 256};
297
298
299private:
302 void DumpUntilPercent(void);
303
306 void ProcessPercent(void);
307
309 std::ostream & OutputStream;
310
312 const cmnPrintf & OutputFormatter;
313
315 char * NextFormatTextPosition;
316
320 char NextFormatTextCharacter;
321
324 char * FormatSequence;
325
328 char NextTypeIdCharacter;
329
335 inline void AdvanceToNextFormat(void)
336 {
337 FormatSequence = 0;
338 DumpUntilPercent();
339 ProcessPercent();
340 }
341
342public:
343
346 static const char * TypeIdCharset;
347
349 static const char * IntegerTypeIds;
350
352 static const char * FloatTypeIds;
353
355 static const char * StringTypeIds;
356
361 static inline const char * TypeIdString(double CMN_UNUSED(value))
362 {
363 return FloatTypeIds;
364 }
365
370 static inline const char * TypeIdString(float CMN_UNUSED(value))
371 {
372 return FloatTypeIds;
373 }
374
379 static inline const char * TypeIdString(int CMN_UNUSED(value))
380 {
381 return IntegerTypeIds;
382 }
383
388 static inline const char * TypeIdString(unsigned int CMN_UNUSED(value))
389 {
390 return IntegerTypeIds;
391 }
392
397 static inline const char * TypeIdString(long CMN_UNUSED(value))
398 {
399 return IntegerTypeIds;
400 }
401
406 static inline const char * TypeIdString(unsigned long CMN_UNUSED(value))
407 {
408 return IntegerTypeIds;
409 }
410
419 template<typename _outputType>
420 bool MatchOutputWithFormatChar(const _outputType & data)
421 {
422 const char * formatCharsForData = TypeIdString(data);
423 const bool result = this->NextTypeIdCharIsOneOf(formatCharsForData);
424 return result;
425 }
426
433 bool NextTypeIdCharIsOneOf(const char * typeIdCharset) const;
434
435 void SuspendOutput(void)
436 {
437 NextFormatTextPosition = 0;
438 }
439
443 const char * GetNextFormatSequence(void) const
444 {
445 return FormatSequence;
446 }
447
449 char GetNextTypeIdCharacter(void) const
450 {
451 return NextTypeIdCharacter;
452 }
453};
454
455
464inline cmnPrintfParser
465operator << (std::ostream & outputStream, const cmnPrintf & formatStr)
466{
467 return cmnPrintfParser(outputStream, formatStr);
468}
469
470
471#endif // _cmnPrintf_h
472
std::ostream & operator<<(std::ostream &output, const cmnClassRegister &classRegister)
Definition cmnClassRegister.h:349
Macros to export the symbols of cisstCommon (in a Dll).
#define CISST_EXPORT
Definition cmnExportMacros.h:50
Portability across compilers and operating systems tools.
#define CMN_UNUSED(argument)
Definition cmnPortability.h:497
friend class cmnPrintfParser
Definition cmnPrintf.h:183
CISST_EXPORT bool cmnTypePrintf(cmnPrintfParser &parser, const int number)
cmnPrintf(const std::string &formatStr)
printf-like formatted output for streams
Definition cmnPrintf.h:164