Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
U
uniset2
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
1
Issues
1
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
UniSet project repositories
uniset2
Commits
dbed50d4
Commit
dbed50d4
authored
Jun 28, 2017
by
Pavel Vainerman
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Переименовал IosFlagSaver --> ios_fmt_restorer
и перенёс его в UniSetTypes.h из DebugStream.h А также задействовал его везде, где был старый способ.
parent
b4a1c093
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
67 additions
and
56 deletions
+67
-56
admin.cc
Utilities/Admin/admin.cc
+5
-14
iocalibr.cc
extensions/IOControl/iocalibr.cc
+1
-2
MTR.cc
extensions/lib/MTR.cc
+3
-6
DebugStream.h
include/DebugStream.h
+0
-25
UniSetTypes.h
include/UniSetTypes.h
+21
-0
ModbusTypes.cc
src/Communications/Modbus/ModbusTypes.cc
+2
-2
DebugStream.cc
src/Log/DebugStream.cc
+3
-7
test_utypes.cc
tests/test_utypes.cc
+32
-0
No files found.
Utilities/Admin/admin.cc
View file @
dbed50d4
...
...
@@ -80,12 +80,9 @@ int apiRequest( const string& args, UInterface& ui, const string& query );
// --------------------------------------------------------------------------
static
void
print_help
(
int
width
,
const
string
&
cmd
,
const
string
&
help
,
const
string
&
tab
=
" "
)
{
// чтобы не менять параметры основного потока
// создаём свой stream...
ostringstream
info
;
info
.
setf
(
ios
::
left
,
ios
::
adjustfield
);
info
<<
tab
<<
setw
(
width
)
<<
cmd
<<
" - "
<<
help
;
cout
<<
info
.
str
();
uniset
::
ios_fmt_restorer
ifs
(
cout
);
cout
.
setf
(
ios
::
left
,
ios
::
adjustfield
);
cout
<<
tab
<<
setw
(
width
)
<<
cmd
<<
" - "
<<
help
;
}
// --------------------------------------------------------------------------
static
void
short_usage
()
...
...
@@ -430,7 +427,7 @@ static bool commandToAll(const string& section, std::shared_ptr<ObjectRepository
if
(
verb
)
cout
<<
"
\n
||=======******** "
<<
section
<<
" ********=========||
\n
"
<<
endl
;
std
::
ios_base
::
fmtflags
old_flags
=
cout
.
flags
(
);
uniset
::
ios_fmt_restorer
ifs
(
cout
);
try
{
...
...
@@ -549,7 +546,6 @@ static bool commandToAll(const string& section, std::shared_ptr<ObjectRepository
if
(
!
quiet
)
cout
<<
"неизвестная команда -"
<<
cmd
<<
endl
;
cout
.
setf
(
old_flags
);
return
false
;
}
}
...
...
@@ -576,11 +572,9 @@ static bool commandToAll(const string& section, std::shared_ptr<ObjectRepository
if
(
!
quiet
)
cerr
<<
"..ORepFailed.."
<<
endl
;
cout
.
setf
(
old_flags
);
return
false
;
}
cout
.
setf
(
old_flags
);
return
true
;
}
...
...
@@ -602,7 +596,7 @@ static void createSections( const std::shared_ptr<uniset::Configuration>& rconf
// ==============================================================================================
int
omap
()
{
std
::
ios_base
::
fmtflags
old_flags
=
cout
.
flags
(
);
uniset
::
ios_fmt_restorer
ifs
(
cout
);
try
{
...
...
@@ -616,7 +610,6 @@ int omap()
if
(
!
quiet
)
cerr
<<
" configuration init failed: "
<<
ex
<<
endl
;
cout
.
setf
(
old_flags
);
return
1
;
}
catch
(
const
std
::
exception
&
ex
)
...
...
@@ -624,11 +617,9 @@ int omap()
if
(
!
quiet
)
cerr
<<
"std::exception: "
<<
ex
.
what
()
<<
endl
;
cout
.
setf
(
old_flags
);
return
1
;
}
cout
.
setf
(
old_flags
);
return
0
;
}
...
...
extensions/IOControl/iocalibr.cc
View file @
dbed50d4
...
...
@@ -506,7 +506,7 @@ void openXML()
// --------------------------------------------------------------------------
void
dispDiagram
()
{
std
::
ios_base
::
fmtflags
old_flags
=
cout
.
flags
(
);
uniset
::
ios_fmt_restorer
l
(
cout
);
cout
.
setf
(
ios
::
right
,
ios
::
adjustfield
);
cout
<<
endl
<<
"================================="
<<
endl
;
cout
<<
"| data | calibrated |"
<<
endl
;
...
...
@@ -521,7 +521,6 @@ void dispDiagram()
cout
<<
"================================="
<<
endl
;
cout
<<
sortedMass
.
size
()
<<
" "
<<
massDat
.
size
()
<<
endl
;
cout
.
setf
(
old_flags
);
}
// --------------------------------------------------------------------------
...
...
extensions/lib/MTR.cc
View file @
dbed50d4
...
...
@@ -642,29 +642,26 @@ namespace uniset
}
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
MTR
::
T8
&
t
)
{
std
::
ios_base
::
fmtflags
old_flags
=
os
.
flags
(
);
uniset
::
ios_fmt_restorer
l
(
os
);
os
<<
setfill
(
'0'
)
<<
hex
<<
setw
(
2
)
<<
t
.
hour
()
<<
":"
<<
setw
(
2
)
<<
t
.
min
()
<<
" "
<<
setw
(
2
)
<<
t
.
day
()
<<
"/"
<<
setw
(
2
)
<<
t
.
mon
();
os
.
setf
(
old_flags
);
return
os
;
}
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
MTR
::
T9
&
t
)
{
std
::
ios_base
::
fmtflags
old_flags
=
os
.
flags
(
);
uniset
::
ios_fmt_restorer
l
(
os
);
os
<<
setfill
(
'0'
)
<<
hex
<<
setw
(
2
)
<<
t
.
hour
()
<<
":"
<<
setw
(
2
)
<<
t
.
min
()
<<
":"
<<
setw
(
2
)
<<
t
.
sec
()
<<
"."
<<
setw
(
2
)
<<
t
.
ssec
();
os
.
setf
(
old_flags
);
return
os
;
}
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
MTR
::
T10
&
t
)
{
std
::
ios_base
::
fmtflags
old_flags
=
os
.
flags
(
);
uniset
::
ios_fmt_restorer
l
(
os
);
os
<<
setfill
(
'0'
)
<<
dec
<<
setw
(
4
)
<<
t
.
year
()
<<
"/"
<<
setw
(
2
)
<<
t
.
mon
()
<<
"/"
<<
setw
(
2
)
<<
t
.
day
();
os
.
setf
(
old_flags
);
return
os
;
}
std
::
ostream
&
operator
<<
(
std
::
ostream
&
os
,
MTR
::
T16
&
t
)
...
...
include/DebugStream.h
View file @
dbed50d4
...
...
@@ -275,31 +275,6 @@ class DebugStream : public std::ostream
return
logname
;
}
// ------------------------------------------------------------------------------------------------
// RAII
// https://stackoverflow.com/questions/2273330/restore-the-state-of-stdcout-after-manipulating-it
class
IosFlagSaver
{
public
:
explicit
IosFlagSaver
(
std
::
ostream
&
_ios
)
:
ios
(
_ios
),
f
(
_ios
.
flags
())
{
}
~
IosFlagSaver
()
{
ios
.
flags
(
f
);
}
IosFlagSaver
(
const
IosFlagSaver
&
rhs
)
=
delete
;
IosFlagSaver
&
operator
=
(
const
IosFlagSaver
&
rhs
)
=
delete
;
private
:
std
::
ostream
&
ios
;
std
::
ios
::
fmtflags
f
;
};
// ------------------------------------------------------------------------------------------------
protected
:
void
sbuf_overflow
(
const
std
::
string
&
s
)
noexcept
;
...
...
include/UniSetTypes.h
View file @
dbed50d4
...
...
@@ -317,6 +317,27 @@ namespace uniset
return
destBegin
;
}
// RAII для флагов форматирования ostream..
class
ios_fmt_restorer
{
public
:
ios_fmt_restorer
(
std
::
ostream
&
s
)
:
os
(
s
),
f
(
nullptr
){
f
.
copyfmt
(
s
);
}
~
ios_fmt_restorer
()
{
os
.
copyfmt
(
f
);
}
ios_fmt_restorer
(
const
ios_fmt_restorer
&
)
=
delete
;
ios_fmt_restorer
&
operator
=
(
const
ios_fmt_restorer
&
)
=
delete
;
private
:
std
::
ostream
&
os
;
std
::
ios
f
;
};
// -----------------------------------------------------------------------------------------
}
// end of namespace uniset
// -----------------------------------------------------------------------------------------
...
...
src/Communications/Modbus/ModbusTypes.cc
View file @
dbed50d4
...
...
@@ -212,7 +212,7 @@ namespace uniset
std
::
ostream
&
ModbusRTU
::
mbPrintMessage
(
std
::
ostream
&
os
,
ModbusByte
*
m
,
size_t
len
)
{
DebugStream
::
IosFlagSav
er
ifs
(
os
);
uniset
::
ios_fmt_restor
er
ifs
(
os
);
// << setiosflags(ios::showbase) // для вывода в формате 0xNN
ostringstream
s
;
...
...
@@ -3067,7 +3067,7 @@ namespace uniset
// -------------------------------------------------------------------------
std
::
ostream
&
ModbusRTU
::
operator
<<
(
std
::
ostream
&
os
,
SetDateTimeMessage
&
m
)
{
DebugStream
::
IosFlagSav
er
ifs
(
os
);
uniset
::
ios_fmt_restor
er
ifs
(
os
);
ostringstream
s
;
s
<<
setfill
(
'0'
)
...
...
src/Log/DebugStream.cc
View file @
dbed50d4
...
...
@@ -145,7 +145,7 @@ std::ostream& DebugStream::debug(Debug::type t) noexcept
{
if
(
dt
&
t
)
{
IosFlagSav
er
ifs
(
*
this
);
uniset
::
ios_fmt_restor
er
ifs
(
*
this
);
if
(
show_datetime
)
printDateTime
(
t
);
...
...
@@ -192,7 +192,7 @@ std::ostream& DebugStream::printTime(Debug::type t, char brk) noexcept
{
if
(
dt
&&
t
)
{
IosFlagSav
er
ifs
(
*
this
);
uniset
::
ios_fmt_restor
er
ifs
(
*
this
);
timespec
tv
=
uniset
::
now_to_timespec
();
// gettimeofday(tv,0);
std
::
tm
tms
=
*
std
::
localtime
(
&
tv
.
tv_sec
);
...
...
@@ -222,7 +222,7 @@ std::ostream& DebugStream::printDateTime(Debug::type t) noexcept
{
if
(
dt
&
t
)
{
IosFlagSav
er
ifs
(
*
this
);
uniset
::
ios_fmt_restor
er
ifs
(
*
this
);
timespec
tv
=
uniset
::
now_to_timespec
();
// gettimeofday(tv,0);
std
::
tm
tms
=
*
std
::
localtime
(
&
tv
.
tv_sec
);
...
...
@@ -238,15 +238,11 @@ std::ostream& DebugStream::printDateTime(Debug::type t) noexcept
<<
std
::
setw
(
2
)
<<
std
::
setfill
(
'0'
)
<<
tms
.
tm_sec
;
#endif
std
::
ios_base
::
fmtflags
old_flags
=
this
->
flags
();
if
(
show_usec
)
(
*
this
)
<<
"."
<<
std
::
setw
(
6
)
<<
std
::
setfill
(
'0'
)
<<
(
tv
.
tv_nsec
/
1000
);
else
if
(
show_msec
)
(
*
this
)
<<
"."
<<
std
::
setw
(
3
)
<<
std
::
setfill
(
'0'
)
<<
(
tv
.
tv_nsec
/
1000000
);
this
->
setf
(
old_flags
);
return
*
this
;
}
...
...
tests/test_utypes.cc
View file @
dbed50d4
...
...
@@ -2,6 +2,7 @@
// -----------------------------------------------------------------------------
#include <sstream>
#include <limits>
#include <iomanip>
#include <cstdint>
#include "Configuration.h"
#include "UniSetTypes.h"
...
...
@@ -338,3 +339,34 @@ TEST_CASE("UniSetTypes: getIOType", "[utypes][getIOType]" )
REQUIRE
(
getIOType
(
""
)
==
UniversalIO
::
UnknownIOType
);
}
// -----------------------------------------------------------------------------
TEST_CASE
(
"UniSetTypes: ostream_guard"
,
"[utypes][ostream_guard]"
)
{
std
::
ostringstream
s
;
int
value
=
5
;
s
<<
setw
(
2
)
<<
value
;
REQUIRE
(
s
.
str
()
==
" 5"
);
{
s
.
str
(
""
);
uniset
::
ios_fmt_restorer
l
(
s
);
s
<<
std
::
setfill
(
'0'
)
<<
setw
(
2
)
<<
value
;
REQUIRE
(
s
.
str
()
==
"05"
);
}
{
s
.
str
(
""
);
uniset
::
ios_fmt_restorer
l
(
s
);
s
.
setf
(
ios
::
left
,
ios
::
adjustfield
);
s
<<
setw
(
2
)
<<
value
;
REQUIRE
(
s
.
str
()
==
"5 "
);
}
s
.
str
(
""
);
s
<<
setw
(
2
)
<<
value
;
REQUIRE
(
s
.
str
()
==
" 5"
);
}
// -----------------------------------------------------------------------------
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment