Commit dbed50d4 authored by Pavel Vainerman's avatar Pavel Vainerman

Переименовал IosFlagSaver --> ios_fmt_restorer

и перенёс его в UniSetTypes.h из DebugStream.h А также задействовал его везде, где был старый способ.
parent b4a1c093
...@@ -80,12 +80,9 @@ int apiRequest( const string& args, UInterface& ui, const string& query ); ...@@ -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 = " " ) static void print_help(int width, const string& cmd, const string& help, const string& tab = " " )
{ {
// чтобы не менять параметры основного потока uniset::ios_fmt_restorer ifs(cout);
// создаём свой stream... cout.setf(ios::left, ios::adjustfield);
ostringstream info; cout << tab << setw(width) << cmd << " - " << help;
info.setf(ios::left, ios::adjustfield);
info << tab << setw(width) << cmd << " - " << help;
cout << info.str();
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
static void short_usage() static void short_usage()
...@@ -430,7 +427,7 @@ static bool commandToAll(const string& section, std::shared_ptr<ObjectRepository ...@@ -430,7 +427,7 @@ static bool commandToAll(const string& section, std::shared_ptr<ObjectRepository
if( verb ) if( verb )
cout << "\n||=======******** " << section << " ********=========||\n" << endl; cout << "\n||=======******** " << section << " ********=========||\n" << endl;
std::ios_base::fmtflags old_flags = cout.flags(); uniset::ios_fmt_restorer ifs(cout);
try try
{ {
...@@ -549,7 +546,6 @@ static bool commandToAll(const string& section, std::shared_ptr<ObjectRepository ...@@ -549,7 +546,6 @@ static bool commandToAll(const string& section, std::shared_ptr<ObjectRepository
if( !quiet ) if( !quiet )
cout << "неизвестная команда -" << cmd << endl; cout << "неизвестная команда -" << cmd << endl;
cout.setf(old_flags);
return false; return false;
} }
} }
...@@ -576,11 +572,9 @@ static bool commandToAll(const string& section, std::shared_ptr<ObjectRepository ...@@ -576,11 +572,9 @@ static bool commandToAll(const string& section, std::shared_ptr<ObjectRepository
if( !quiet ) if( !quiet )
cerr << "..ORepFailed.." << endl; cerr << "..ORepFailed.." << endl;
cout.setf(old_flags);
return false; return false;
} }
cout.setf(old_flags);
return true; return true;
} }
...@@ -602,7 +596,7 @@ static void createSections( const std::shared_ptr<uniset::Configuration>& rconf ...@@ -602,7 +596,7 @@ static void createSections( const std::shared_ptr<uniset::Configuration>& rconf
// ============================================================================================== // ==============================================================================================
int omap() int omap()
{ {
std::ios_base::fmtflags old_flags = cout.flags(); uniset::ios_fmt_restorer ifs(cout);
try try
{ {
...@@ -616,7 +610,6 @@ int omap() ...@@ -616,7 +610,6 @@ int omap()
if( !quiet ) if( !quiet )
cerr << " configuration init failed: " << ex << endl; cerr << " configuration init failed: " << ex << endl;
cout.setf(old_flags);
return 1; return 1;
} }
catch( const std::exception& ex ) catch( const std::exception& ex )
...@@ -624,11 +617,9 @@ int omap() ...@@ -624,11 +617,9 @@ int omap()
if( !quiet ) if( !quiet )
cerr << "std::exception: " << ex.what() << endl; cerr << "std::exception: " << ex.what() << endl;
cout.setf(old_flags);
return 1; return 1;
} }
cout.setf(old_flags);
return 0; return 0;
} }
......
...@@ -506,7 +506,7 @@ void openXML() ...@@ -506,7 +506,7 @@ void openXML()
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
void dispDiagram() void dispDiagram()
{ {
std::ios_base::fmtflags old_flags = cout.flags(); uniset::ios_fmt_restorer l(cout);
cout.setf( ios::right, ios::adjustfield ); cout.setf( ios::right, ios::adjustfield );
cout << endl << "=================================" << endl; cout << endl << "=================================" << endl;
cout << "| data | calibrated |" << endl; cout << "| data | calibrated |" << endl;
...@@ -521,7 +521,6 @@ void dispDiagram() ...@@ -521,7 +521,6 @@ void dispDiagram()
cout << "=================================" << endl; cout << "=================================" << endl;
cout << sortedMass.size() << " " << massDat.size() << endl; cout << sortedMass.size() << " " << massDat.size() << endl;
cout.setf(old_flags);
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
......
...@@ -642,29 +642,26 @@ namespace uniset ...@@ -642,29 +642,26 @@ namespace uniset
} }
std::ostream& operator<<(std::ostream& os, MTR::T8& t ) 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 os << setfill('0') << hex
<< setw(2) << t.hour() << ":" << setw(2) << t.min() << setw(2) << t.hour() << ":" << setw(2) << t.min()
<< " " << setw(2) << t.day() << "/" << setw(2) << t.mon(); << " " << setw(2) << t.day() << "/" << setw(2) << t.mon();
os.setf(old_flags);
return os; return os;
} }
std::ostream& operator<<(std::ostream& os, MTR::T9& t ) 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 os << setfill('0') << hex
<< setw(2) << t.hour() << ":" << setw(2) << t.min() << setw(2) << t.hour() << ":" << setw(2) << t.min()
<< ":" << setw(2) << t.sec() << "." << setw(2) << t.ssec(); << ":" << setw(2) << t.sec() << "." << setw(2) << t.ssec();
os.setf(old_flags);
return os; return os;
} }
std::ostream& operator<<(std::ostream& os, MTR::T10& t ) 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 os << setfill('0') << dec
<< setw(4) << t.year() << "/" << setw(2) << t.mon() << setw(4) << t.year() << "/" << setw(2) << t.mon()
<< "/" << setw(2) << t.day(); << "/" << setw(2) << t.day();
os.setf(old_flags);
return os; return os;
} }
std::ostream& operator<<(std::ostream& os, MTR::T16& t ) std::ostream& operator<<(std::ostream& os, MTR::T16& t )
......
...@@ -275,31 +275,6 @@ class DebugStream : public std::ostream ...@@ -275,31 +275,6 @@ class DebugStream : public std::ostream
return logname; 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: protected:
void sbuf_overflow( const std::string& s ) noexcept; void sbuf_overflow( const std::string& s ) noexcept;
......
...@@ -317,6 +317,27 @@ namespace uniset ...@@ -317,6 +317,27 @@ namespace uniset
return destBegin; 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 } // end of namespace uniset
// ----------------------------------------------------------------------------------------- // -----------------------------------------------------------------------------------------
......
...@@ -212,7 +212,7 @@ namespace uniset ...@@ -212,7 +212,7 @@ namespace uniset
std::ostream& ModbusRTU::mbPrintMessage( std::ostream& os, ModbusByte* m, size_t len ) std::ostream& ModbusRTU::mbPrintMessage( std::ostream& os, ModbusByte* m, size_t len )
{ {
DebugStream::IosFlagSaver ifs(os); uniset::ios_fmt_restorer ifs(os);
// << setiosflags(ios::showbase) // для вывода в формате 0xNN // << setiosflags(ios::showbase) // для вывода в формате 0xNN
ostringstream s; ostringstream s;
...@@ -3067,7 +3067,7 @@ namespace uniset ...@@ -3067,7 +3067,7 @@ namespace uniset
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
std::ostream& ModbusRTU::operator<<(std::ostream& os, SetDateTimeMessage& m ) std::ostream& ModbusRTU::operator<<(std::ostream& os, SetDateTimeMessage& m )
{ {
DebugStream::IosFlagSaver ifs(os); uniset::ios_fmt_restorer ifs(os);
ostringstream s; ostringstream s;
s << setfill('0') s << setfill('0')
......
...@@ -145,7 +145,7 @@ std::ostream& DebugStream::debug(Debug::type t) noexcept ...@@ -145,7 +145,7 @@ std::ostream& DebugStream::debug(Debug::type t) noexcept
{ {
if(dt & t) if(dt & t)
{ {
IosFlagSaver ifs(*this); uniset::ios_fmt_restorer ifs(*this);
if( show_datetime ) if( show_datetime )
printDateTime(t); printDateTime(t);
...@@ -192,7 +192,7 @@ std::ostream& DebugStream::printTime(Debug::type t, char brk) noexcept ...@@ -192,7 +192,7 @@ std::ostream& DebugStream::printTime(Debug::type t, char brk) noexcept
{ {
if(dt && t) if(dt && t)
{ {
IosFlagSaver ifs(*this); uniset::ios_fmt_restorer ifs(*this);
timespec tv = uniset::now_to_timespec(); // gettimeofday(tv,0); timespec tv = uniset::now_to_timespec(); // gettimeofday(tv,0);
std::tm tms = *std::localtime(&tv.tv_sec); std::tm tms = *std::localtime(&tv.tv_sec);
...@@ -222,7 +222,7 @@ std::ostream& DebugStream::printDateTime(Debug::type t) noexcept ...@@ -222,7 +222,7 @@ std::ostream& DebugStream::printDateTime(Debug::type t) noexcept
{ {
if(dt & t) if(dt & t)
{ {
IosFlagSaver ifs(*this); uniset::ios_fmt_restorer ifs(*this);
timespec tv = uniset::now_to_timespec(); // gettimeofday(tv,0); timespec tv = uniset::now_to_timespec(); // gettimeofday(tv,0);
std::tm tms = *std::localtime(&tv.tv_sec); std::tm tms = *std::localtime(&tv.tv_sec);
...@@ -238,15 +238,11 @@ std::ostream& DebugStream::printDateTime(Debug::type t) noexcept ...@@ -238,15 +238,11 @@ std::ostream& DebugStream::printDateTime(Debug::type t) noexcept
<< std::setw(2) << std::setfill('0') << tms.tm_sec; << std::setw(2) << std::setfill('0') << tms.tm_sec;
#endif #endif
std::ios_base::fmtflags old_flags = this->flags();
if( show_usec ) if( show_usec )
(*this) << "." << std::setw(6) << std::setfill('0') << (tv.tv_nsec / 1000); (*this) << "." << std::setw(6) << std::setfill('0') << (tv.tv_nsec / 1000);
else if( show_msec ) else if( show_msec )
(*this) << "." << std::setw(3) << std::setfill('0') << (tv.tv_nsec / 1000000); (*this) << "." << std::setw(3) << std::setfill('0') << (tv.tv_nsec / 1000000);
this->setf(old_flags);
return *this; return *this;
} }
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
#include <sstream> #include <sstream>
#include <limits> #include <limits>
#include <iomanip>
#include <cstdint> #include <cstdint>
#include "Configuration.h" #include "Configuration.h"
#include "UniSetTypes.h" #include "UniSetTypes.h"
...@@ -338,3 +339,34 @@ TEST_CASE("UniSetTypes: getIOType", "[utypes][getIOType]" ) ...@@ -338,3 +339,34 @@ TEST_CASE("UniSetTypes: getIOType", "[utypes][getIOType]" )
REQUIRE( getIOType("") == UniversalIO::UnknownIOType ); 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" );
}
// -----------------------------------------------------------------------------
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment