-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathRosPrintfS.cpp
More file actions
142 lines (118 loc) · 3.68 KB
/
RosPrintfS.cpp
File metadata and controls
142 lines (118 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// $Id: RosPrintfS.cpp 1186 2019-07-12 17:49:59Z mueller $
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright 2000-2018 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
//
// Revision History:
// Date Rev Version Comment
// 2018-12-18 1089 1.1.1 use c++ style casts
// 2018-12-17 1088 1.1 add bool specialization (use c++11 std::boolalpha)
// 2011-02-25 364 1.0.1 allow NULL ptr for const char*, output <NULL>
// 2011-01-30 357 1.0 Adopted from CTBprintfS
// 2000-10-29 - - Last change on CTBprintfS
// ---------------------------------------------------------------------------
/*!
\brief Implemenation of RosPrintfS .
*/
#include <iomanip>
#include "RiosState.hpp"
#include "RosPrintfS.hpp"
using namespace std;
/*!
\class RosPrintfS
\brief Print object for scalar values . **
*/
// all method definitions in namespace Retro
namespace Retro {
//------------------------------------------+-----------------------------------
/*!
\brief Constructor.
\param value value to be printed
\param form format descriptor string
\param width field width
\param prec precision
*/
template <class T>
RosPrintfS<T>::RosPrintfS(T value, const char* form, int width, int prec)
: RosPrintfBase(form, width, prec),
fValue(value)
{}
//------------------------------------------+-----------------------------------
template <class T>
void RosPrintfS<T>::ToStream(std::ostream& os) const
{
RiosState iostate(os, fForm, fPrec);
os << setw(fWidth) << fValue;
}
//------------------------------------------+-----------------------------------
template <>
void RosPrintfS<bool>::ToStream(std::ostream& os) const
{
RiosState iostate(os, fForm, fPrec);
os << std::boolalpha << fValue;
}
//------------------------------------------+-----------------------------------
template <>
void RosPrintfS<char>::ToStream(std::ostream& os) const
{
RiosState iostate(os, fForm, fPrec);
char ctype = iostate.Ctype();
os.width(fWidth);
if (ctype == 0 || ctype == 'c') {
os << fValue;
} else {
os << int(fValue);
}
}
//------------------------------------------+-----------------------------------
template <>
void RosPrintfS<int>::ToStream(std::ostream& os) const
{
RiosState iostate(os, fForm, fPrec);
char ctype = iostate.Ctype();
os.width(fWidth);
if (ctype == 'c') {
os << char(fValue);
} else {
os << fValue;
}
}
//------------------------------------------+-----------------------------------
template <>
void RosPrintfS<const char *>::ToStream(std::ostream& os) const
{
RiosState iostate(os, fForm, fPrec);
char ctype = iostate.Ctype();
os.width(fWidth);
if (ctype == 'p') {
os << reinterpret_cast<const void*>(fValue);
} else {
os << (fValue?fValue:"<NULL>");
}
}
//------------------------------------------+-----------------------------------
template <>
void RosPrintfS<const void *>::ToStream(std::ostream& os) const
{
RiosState iostate(os, fForm, fPrec);
char ctype = iostate.Ctype();
os.width(fWidth);
if (ctype == 0 || ctype == 'p') {
os << fValue;
} else {
os << reinterpret_cast<unsigned long>(fValue);
}
}
//!! Note:
//!! 1. This specialization is printing signed and unsigned char types and
//!! implements the `c' conversion format,
// finally do an explicit instantiation of the required RosPrintfS
template class RosPrintfS<bool>;
template class RosPrintfS<char>;
template class RosPrintfS<int>;
template class RosPrintfS<unsigned int>;
template class RosPrintfS<long>;
template class RosPrintfS<unsigned long>;
template class RosPrintfS<double>;
template class RosPrintfS<const char *>;
template class RosPrintfS<const void *>;
} // end namespace Retro