Skip to content

Commit c988f0f

Browse files
committed
Refactor to reduce/improve use of stringstreams
1 parent 5466b43 commit c988f0f

136 files changed

Lines changed: 1547 additions & 2261 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

libs/attribute/src/ecflow/attribute/CronAttr.cpp

Lines changed: 19 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "ecflow/core/Calendar.hpp"
1919
#include "ecflow/core/Converter.hpp"
2020
#include "ecflow/core/Ecf.hpp"
21+
#include "ecflow/core/Message.hpp"
2122
#include "ecflow/core/Serialization.hpp"
2223
#include "ecflow/core/Str.hpp"
2324

@@ -49,31 +50,27 @@ void CronAttr::addWeekDays(const std::vector<int>& w) {
4950
weekDays_ = w;
5051
for (int day : weekDays_) {
5152
if (day < 0 || day > 6) {
52-
std::stringstream ss;
53-
ss << "Invalid range for day(" << day << ") of the week expected range is 0==Sun to 6==Sat";
54-
throw std::out_of_range(ss.str());
53+
throw std::out_of_range(
54+
MESSAGE("Invalid range for day(" << day << ") of the week expected range is 0==Sun to 6==Sat"));
5555
}
5656
auto result = std::find(std::begin(last_week_days_of_month_), std::end(last_week_days_of_month_), day);
5757
if (result != std::end(last_week_days_of_month_)) {
58-
std::stringstream ss;
59-
ss << "Duplicate day(" << day << ") of the week also found in last week day of the month";
60-
throw std::runtime_error(ss.str());
58+
throw std::runtime_error(
59+
MESSAGE("Duplicate day(" << day << ") of the week also found in last week day of the month"));
6160
}
6261
}
6362
}
6463
void CronAttr::add_last_week_days_of_month(const std::vector<int>& w) {
6564
last_week_days_of_month_ = w;
6665
for (int day : last_week_days_of_month_) {
6766
if (day < 0 || day > 6) {
68-
std::stringstream ss;
69-
ss << "Invalid range for day(" << day << ") of the week expected range is 0==Sun to 6==Sat";
70-
throw std::out_of_range(ss.str());
67+
throw std::out_of_range(
68+
MESSAGE("Invalid range for day(" << day << ") of the week expected range is 0==Sun to 6==Sat"));
7169
}
7270
auto result = std::find(std::begin(weekDays_), std::end(weekDays_), day);
7371
if (result != std::end(weekDays_)) {
74-
std::stringstream ss;
75-
ss << "Duplicate last week day (" << day << ") of the month also found in week day";
76-
throw std::runtime_error(ss.str());
72+
throw std::runtime_error(
73+
MESSAGE("Duplicate last week day (" << day << ") of the month also found in week day"));
7774
}
7875
}
7976
}
@@ -82,9 +79,8 @@ void CronAttr::addDaysOfMonth(const std::vector<int>& d) {
8279
daysOfMonth_ = d;
8380
for (int day_of_month : daysOfMonth_) {
8481
if (day_of_month < 1 || day_of_month > 31) {
85-
std::stringstream ss;
86-
ss << "Invalid range for day of month(" << day_of_month << ") expected range is 1-31";
87-
throw std::out_of_range(ss.str());
82+
throw std::out_of_range(
83+
MESSAGE("Invalid range for day of month(" << day_of_month << ") expected range is 1-31"));
8884
}
8985
}
9086
}
@@ -93,9 +89,8 @@ void CronAttr::addMonths(const std::vector<int>& m) {
9389
months_ = m;
9490
for (int month : months_) {
9591
if (month < 1 || month > 12) {
96-
std::stringstream ss;
97-
ss << "Invalid range for month(" << month << ") expected range is 1==Jan to 12==Dec";
98-
throw std::out_of_range(ss.str());
92+
throw std::out_of_range(
93+
MESSAGE("Invalid range for month(" << month << ") expected range is 1==Jan to 12==Dec"));
9994
}
10095
}
10196
}
@@ -180,15 +175,7 @@ void CronAttr::write(std::string& ret) const {
180175
}
181176

182177
std::string CronAttr::dump() const {
183-
std::stringstream ss;
184-
ss << toString();
185-
if (free_) {
186-
ss << " (free)";
187-
}
188-
else {
189-
ss << " (holding)";
190-
}
191-
return ss.str();
178+
return MESSAGE(toString() << (free_ ? " (free)" : " (holding)"));
192179
}
193180

194181
bool CronAttr::operator==(const CronAttr& rhs) const {
@@ -381,7 +368,7 @@ bool CronAttr::why(const ecf::Calendar& c, std::string& theReasonWhy) const {
381368
theReasonWhy += " ";
382369
theReasonWhy += to_simple_string(the_next_date);
383370

384-
std::stringstream ss;
371+
std::ostringstream ss;
385372
TimeSlot currentTime = TimeSlot(timeSeries_.duration(c));
386373
ss << ", current time ";
387374
if (timeSeries_.relative()) {
@@ -701,9 +688,7 @@ std::vector<int> extract_month(size_t& index, const std::vector<std::string>& li
701688
theIntVec.push_back(theInt);
702689
}
703690
catch (const ecf::bad_conversion&) {
704-
std::stringstream ss;
705-
ss << "Invalid cron option: " << option;
706-
throw std::runtime_error(ss.str());
691+
throw std::runtime_error(MESSAGE("Invalid cron option: " << option));
707692
}
708693
}
709694
return theIntVec;
@@ -736,9 +721,7 @@ void extract_days_of_week(size_t& index,
736721
try {
737722
if (theIntToken.size() == 2) {
738723
if (theIntToken[1] != 'L') {
739-
std::stringstream ss;
740-
ss << "Invalid cron option: " << option << " " << theIntToken;
741-
throw std::runtime_error(ss.str());
724+
throw std::runtime_error(MESSAGE("Invalid cron option: " << option << " " << theIntToken));
742725
}
743726
auto theInt = ecf::convert_to<int>(theIntToken[0]);
744727
last_week_days_of_month.push_back(theInt);
@@ -749,9 +732,7 @@ void extract_days_of_week(size_t& index,
749732
}
750733
}
751734
catch (const ecf::bad_conversion&) {
752-
std::stringstream ss;
753-
ss << "Invalid cron option: " << option;
754-
throw std::runtime_error(ss.str());
735+
throw std::runtime_error(MESSAGE("Invalid cron option: " << option));
755736
}
756737
}
757738
}
@@ -791,9 +772,7 @@ void extract_days_of_month(size_t& index,
791772
}
792773
}
793774
catch (const ecf::bad_conversion&) {
794-
std::stringstream ss;
795-
ss << "Invalid cron option: " << option;
796-
throw std::runtime_error(ss.str());
775+
throw std::runtime_error(MESSAGE("Invalid cron option: " << option));
797776
}
798777
}
799778
}

libs/attribute/src/ecflow/attribute/DateAttr.cpp

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "ecflow/core/Converter.hpp"
1919
#include "ecflow/core/Ecf.hpp"
2020
#include "ecflow/core/Extract.hpp"
21+
#include "ecflow/core/Message.hpp"
2122
#include "ecflow/core/Serialization.hpp"
2223

2324
using namespace ecf;
@@ -203,11 +204,9 @@ bool DateAttr::why(const ecf::Calendar& c, std::string& theReasonWhy) const {
203204
return false;
204205
}
205206

206-
std::stringstream ss;
207-
ss << " is date dependent ( next run on " << boost::gregorian::to_simple_string(next_matching_date(c))
208-
<< " the current date is ";
209-
ss << c.day_of_month() << "/" << c.month() << "/" << c.year() << " )";
210-
theReasonWhy += ss.str();
207+
theReasonWhy += MESSAGE(" is date dependent ( next run on "
208+
<< boost::gregorian::to_simple_string(next_matching_date(c)) << " the current date is "
209+
<< c.day_of_month() << "/" << c.month() << "/" << c.year() << " )");
211210
return true;
212211
}
213212

@@ -253,15 +252,7 @@ void DateAttr::write(std::string& ret) const {
253252
}
254253

255254
std::string DateAttr::dump() const {
256-
std::stringstream ss;
257-
ss << toString();
258-
if (free_) {
259-
ss << " (free)";
260-
}
261-
else {
262-
ss << " (holding)";
263-
}
264-
return ss.str();
255+
return MESSAGE(toString() << (free_ ? " (free)" : " (holding)"));
265256
}
266257

267258
bool DateAttr::operator==(const DateAttr& rhs) const {

libs/attribute/src/ecflow/attribute/DayAttr.cpp

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "ecflow/core/Calendar.hpp"
1818
#include "ecflow/core/Ecf.hpp"
1919
#include "ecflow/core/Extract.hpp"
20+
#include "ecflow/core/Message.hpp"
2021
#include "ecflow/core/Serialization.hpp"
2122
#include "ecflow/core/cereal_boost_time.hpp"
2223

@@ -360,16 +361,8 @@ void DayAttr::write(std::string& ret) const {
360361
}
361362

362363
std::string DayAttr::dump() const {
363-
std::stringstream ss;
364-
ss << toString();
365-
if (free_) {
366-
ss << " (free)";
367-
}
368-
if (expired_) {
369-
ss << " (expired)";
370-
}
371-
ss << " " << as_simple_string();
372-
return ss.str();
364+
return MESSAGE(toString() << (free_ ? " (free)" : "") << (expired_ ? " (expired)" : "") << " "
365+
<< as_simple_string());
373366
}
374367

375368
std::string DayAttr::as_simple_string() const {
@@ -481,11 +474,9 @@ DayAttr::Day_t DayAttr::getDay(const std::string& day) {
481474
return DayAttr::SUNDAY;
482475
}
483476

484-
std::stringstream ss;
485-
ss << "Invalid day(" << day
486-
<< ") specification expected one of [monday,tuesday,wednesday,thursday,friday,saturday,sunday]: ";
487-
throw std::runtime_error(ss.str());
488-
477+
throw std::runtime_error(MESSAGE(
478+
"Invalid day("
479+
<< day << ") specification expected one of [monday,tuesday,wednesday,thursday,friday,saturday,sunday]: "));
489480
return DayAttr::SUNDAY;
490481
}
491482

libs/attribute/src/ecflow/attribute/NodeAttr.cpp

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "ecflow/core/Converter.hpp"
1717
#include "ecflow/core/Ecf.hpp"
18+
#include "ecflow/core/Message.hpp"
1819
#include "ecflow/core/Serialization.hpp"
1920
#include "ecflow/core/Str.hpp"
2021

@@ -112,9 +113,7 @@ void Event::set_value(bool b) {
112113

113114
std::string Event::name_or_number() const {
114115
if (n_.empty()) {
115-
std::stringstream ss;
116-
ss << number_;
117-
return ss.str();
116+
return MESSAGE(number_);
118117
}
119118
return n_;
120119
}
@@ -199,9 +198,7 @@ void Event::write(std::string& ret) const {
199198
}
200199

201200
std::string Event::dump() const {
202-
std::stringstream ss;
203-
ss << toString() << " value(" << v_ << ") used(" << used_ << ")";
204-
return ss.str();
201+
return MESSAGE(toString() << " value(" << v_ << ") used(" << used_ << ")");
205202
}
206203

207204
bool Event::isValidState(const std::string& state) {
@@ -235,10 +232,8 @@ Meter::Meter(const std::string& name, int min, int max, int colorChange, int val
235232
}
236233

237234
if (cc_ < min || cc_ > max) {
238-
std::stringstream ss;
239-
ss << "Meter::Meter: Invalid Meter(name,min,max,color_change) color_change(" << cc_ << ") must be between min("
240-
<< min_ << ") and max(" << max_ << ")";
241-
throw std::out_of_range(ss.str());
235+
throw std::out_of_range(MESSAGE("Meter::Meter: Invalid Meter(name,min,max,color_change) color_change("
236+
<< cc_ << ") must be between min(" << min_ << ") and max(" << max_ << ")"));
242237
}
243238
}
244239

@@ -249,8 +244,8 @@ Meter Meter::make_from_value(const std::string& name, const std::string& value)
249244
std::vector<std::string> tokens;
250245
ecf::algorithm::split(tokens, value, ",");
251246
if (tokens.size() != 3) {
252-
throw std::runtime_error("Meter::make_from_value: Expect three comma-separated values, but found: '" + value +
253-
"'");
247+
throw std::runtime_error(
248+
MESSAGE("Meter::make_from_value: Expect three comma-separated values, but found: '" << value << "'"));
254249
}
255250

256251
try {
@@ -260,20 +255,17 @@ Meter Meter::make_from_value(const std::string& name, const std::string& value)
260255
return Meter(name, min, max, max, value, false);
261256
}
262257
catch (const ecf::bad_conversion&) {
263-
std::stringstream ss;
264-
ss << "Meter::make_from_value: Expect three comma-separated values, but found: (" << tokens[0] << ", "
265-
<< tokens[1] << ", " << tokens[2] << ")";
266-
throw std::runtime_error(ss.str());
258+
throw std::runtime_error(MESSAGE("Meter::make_from_value: Expect three comma-separated values, but found: ("
259+
<< tokens[0] << ", " << tokens[1] << ", " << tokens[2] << ")"));
267260
}
268261
}
269262

270263
void Meter::set_value(int v) {
271264

272265
if (!isValidValue(v)) {
273-
std::stringstream ss;
274-
ss << "Meter::set_value(int): The meter(" << n_ << ") value must be in the range[" << min() << "->" << max()
275-
<< "] but found '" << v << "'";
276-
throw std::runtime_error(ss.str());
266+
throw std::runtime_error(MESSAGE("Meter::set_value(int): The meter(" << n_ << ") value must be in the range["
267+
<< min() << "->" << max()
268+
<< "] but found '" << v << "'"));
277269
}
278270

279271
v_ = v;
@@ -321,10 +313,8 @@ void Meter::write(std::string& ret) const {
321313
}
322314

323315
std::string Meter::dump() const {
324-
std::stringstream ss;
325-
ss << "meter " << n_ << " min(" << min_ << ") max (" << max_ << ") colorChange(" << cc_ << ") value(" << v_
326-
<< ") used(" << used_ << ")";
327-
return ss.str();
316+
return MESSAGE("meter " << n_ << " min(" << min_ << ") max (" << max_ << ") colorChange(" << cc_ << ") value(" << v_
317+
<< ") used(" << used_ << ")");
328318
}
329319

330320
/////////////////////////////////////////////////////////////////////////////////////////////
@@ -334,7 +324,7 @@ Label::Label(const std::string& name, const std::string& value, const std::strin
334324
v_(value),
335325
new_v_(new_value) {
336326
if (check_name && !Str::valid_name(n_)) {
337-
throw std::runtime_error("Label::Label: Invalid Label name :" + n_);
327+
throw std::runtime_error(MESSAGE("Label::Label: Invalid Label name :" << n_));
338328
}
339329
}
340330

@@ -364,9 +354,7 @@ void Label::write(std::string& ret) const {
364354
}
365355

366356
std::string Label::dump() const {
367-
std::stringstream ss;
368-
ss << toString() << " : \"" << new_v_ << "\"";
369-
return ss.str();
357+
return MESSAGE(toString() << " : \"" << new_v_ << "\"");
370358
}
371359

372360
void Label::set_new_value(const std::string& l) {

0 commit comments

Comments
 (0)