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 );
// --------------------------------------------------------------------------
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;
}
......
......@@ -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);
}
// --------------------------------------------------------------------------
......
......@@ -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 )
......
......@@ -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;
......
......@@ -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
// -----------------------------------------------------------------------------------------
......
......@@ -212,7 +212,7 @@ namespace uniset
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
ostringstream s;
......@@ -3067,7 +3067,7 @@ namespace uniset
// -------------------------------------------------------------------------
std::ostream& ModbusRTU::operator<<(std::ostream& os, SetDateTimeMessage& m )
{
DebugStream::IosFlagSaver ifs(os);
uniset::ios_fmt_restorer ifs(os);
ostringstream s;
s << setfill('0')
......
......@@ -145,7 +145,7 @@ std::ostream& DebugStream::debug(Debug::type t) noexcept
{
if(dt & t)
{
IosFlagSaver ifs(*this);
uniset::ios_fmt_restorer ifs(*this);
if( show_datetime )
printDateTime(t);
......@@ -192,7 +192,7 @@ std::ostream& DebugStream::printTime(Debug::type t, char brk) noexcept
{
if(dt && t)
{
IosFlagSaver ifs(*this);
uniset::ios_fmt_restorer 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)
{
IosFlagSaver ifs(*this);
uniset::ios_fmt_restorer 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;
}
......
......@@ -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" );
}
// -----------------------------------------------------------------------------
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