Get rid of unnecessary string streams

pull/2119/head
Andrei Kortunov 6 years ago
parent 88edc1a120
commit 0937f02598

@ -3,7 +3,6 @@
#include <stdexcept> #include <stdexcept>
#include <algorithm> #include <algorithm>
#include <sstream>
#include "intsetting.hpp" #include "intsetting.hpp"
#include "doublesetting.hpp" #include "doublesetting.hpp"
@ -393,9 +392,7 @@ CSMPrefs::IntSetting& CSMPrefs::State::declareInt (const std::string& key,
if (mCurrentCategory==mCategories.end()) if (mCurrentCategory==mCategories.end())
throw std::logic_error ("no category for setting"); throw std::logic_error ("no category for setting");
std::ostringstream stream; setDefault(key, std::to_string(default_));
stream << default_;
setDefault (key, stream.str());
default_ = mSettings.getInt (key, mCurrentCategory->second.getKey()); default_ = mSettings.getInt (key, mCurrentCategory->second.getKey());
@ -414,9 +411,7 @@ CSMPrefs::DoubleSetting& CSMPrefs::State::declareDouble (const std::string& key,
if (mCurrentCategory==mCategories.end()) if (mCurrentCategory==mCategories.end())
throw std::logic_error ("no category for setting"); throw std::logic_error ("no category for setting");
std::ostringstream stream; setDefault(key, std::to_string(default_));
stream << default_;
setDefault (key, stream.str());
default_ = mSettings.getFloat (key, mCurrentCategory->second.getKey()); default_ = mSettings.getFloat (key, mCurrentCategory->second.getKey());

@ -77,14 +77,9 @@ void CSMTools::Search::searchRecordStateCell (const CSMWorld::IdTableBase *model
{ {
std::vector<std::string> states = std::vector<std::string> states =
CSMWorld::Columns::getEnums (CSMWorld::Columns::ColumnId_Modification); CSMWorld::Columns::getEnums (CSMWorld::Columns::ColumnId_Modification);
std::ostringstream message;
message << states.at (data);
std::ostringstream hint; const std::string hint = "r: " + model->getColumnId (index.column());
hint << "r: " << model->getColumnId (index.column()); messages.add (id, states.at(data), hint);
messages.add (id, message.str(), hint.str());
} }
} }

@ -77,7 +77,7 @@ int CSMWorld::Resources::getIndex (const std::string& id) const
std::ostringstream stream; std::ostringstream stream;
stream << "Invalid resource: " << mBaseDirectory << '/' << id; stream << "Invalid resource: " << mBaseDirectory << '/' << id;
throw std::runtime_error (stream.str().c_str()); throw std::runtime_error (stream.str());
} }
return index; return index;

@ -150,10 +150,8 @@ std::string CSVWorld::SceneSubView::getTitle() const
void CSVWorld::SceneSubView::cellSelectionChanged (const CSMWorld::UniversalId& id) void CSVWorld::SceneSubView::cellSelectionChanged (const CSMWorld::UniversalId& id)
{ {
setUniversalId(id); setUniversalId(id);
std::ostringstream stream;
stream << "Scene: " << getUniversalId().getId();
mTitle = stream.str(); mTitle = "Scene: " + getUniversalId().getId();
setWindowTitle (QString::fromUtf8 (mTitle.c_str())); setWindowTitle (QString::fromUtf8 (mTitle.c_str()));
emit updateTitle(); emit updateTitle();
} }

@ -125,9 +125,7 @@ namespace MWGui
for (int i=0; ids[i]; ++i) for (int i=0; ids[i]; ++i)
if (ids[i]==id) if (ids[i]==id)
{ {
std::ostringstream valueString; setText (id, std::to_string(value.getModified()));
valueString << value.getModified();
setText (id, valueString.str());
MyGUI::TextBox* box; MyGUI::TextBox* box;
getWidget(box, id); getWidget(box, id);

@ -599,9 +599,7 @@ namespace MWGui
std::string ToolTips::toString(const int value) std::string ToolTips::toString(const int value)
{ {
std::ostringstream stream; return std::to_string(value);
stream << value;
return stream.str();
} }
std::string ToolTips::getWeightString(const float weight, const std::string& prefix) std::string ToolTips::getWeightString(const float weight, const std::string& prefix)

@ -87,10 +87,7 @@ namespace MWGui
else else
toAdd->setUserString("interior","n"); toAdd->setUserString("interior","n");
std::ostringstream oss; toAdd->setUserString("price", std::to_string(price));
oss << price;
toAdd->setUserString("price",oss.str());
toAdd->setCaptionWithReplacing("#{sCell=" + name + "} - " + MyGUI::utility::toString(price)+"#{sgp}"); toAdd->setCaptionWithReplacing("#{sCell=" + name + "} - " + MyGUI::utility::toString(price)+"#{sgp}");
toAdd->setSize(mDestinationsView->getWidth(),lineHeight); toAdd->setSize(mDestinationsView->getWidth(),lineHeight);
toAdd->eventMouseWheel += MyGUI::newDelegate(this, &TravelWindow::onMouseWheel); toAdd->eventMouseWheel += MyGUI::newDelegate(this, &TravelWindow::onMouseWheel);

@ -154,9 +154,8 @@ void MWMechanics::Alchemy::updateEffects()
if (magicEffect->mData.mBaseCost<=0) if (magicEffect->mData.mBaseCost<=0)
{ {
std::ostringstream os; const std::string os = "invalid base cost for magic effect " + iter->mId;
os << "invalid base cost for magic effect " << iter->mId; throw std::runtime_error (os);
throw std::runtime_error (os.str());
} }
float fPotionT1MagMul = float fPotionT1MagMul =

@ -2,8 +2,6 @@
#include <osg/Stats> #include <osg/Stats>
#include <sstream>
#include <components/resource/objectcache.hpp> #include <components/resource/objectcache.hpp>
#include "../mwbase/environment.hpp" #include "../mwbase/environment.hpp"
@ -21,9 +19,7 @@ LandManager::LandManager(int loadFlags)
osg::ref_ptr<ESMTerrain::LandObject> LandManager::getLand(int x, int y) osg::ref_ptr<ESMTerrain::LandObject> LandManager::getLand(int x, int y)
{ {
std::ostringstream id; std::string idstr = std::to_string(x) + " " + std::to_string(y);
id << x << " " << y;
std::string idstr = id.str();
osg::ref_ptr<osg::Object> obj = mCache->getRefFromObjectCache(idstr); osg::ref_ptr<osg::Object> obj = mCache->getRefFromObjectCache(idstr);
if (obj) if (obj)

@ -58,10 +58,8 @@ void MWState::Character::addSlot (const ESM::SavedGame& profile)
int i=0; int i=0;
while (boost::filesystem::exists(slot.mPath)) while (boost::filesystem::exists(slot.mPath))
{ {
std::ostringstream test; const std::string test = stream.str() + " - " + std::to_string(++i);
test << stream.str(); slot.mPath = mPath / (test + ext);
test << " - " << ++i;
slot.mPath = mPath / (test.str() + ext);
} }
slot.mProfile = profile; slot.mProfile = profile;

@ -170,19 +170,19 @@ namespace MWWorld
/// Insert a custom record (i.e. with a generated ID that will not clash will pre-existing records) /// Insert a custom record (i.e. with a generated ID that will not clash will pre-existing records)
template <class T> template <class T>
const T *insert(const T &x) { const T *insert(const T &x)
std::ostringstream id; {
id << "$dynamic" << mDynamicCount++; const std::string id = "$dynamic" + std::to_string(mDynamicCount++);
Store<T> &store = const_cast<Store<T> &>(get<T>()); Store<T> &store = const_cast<Store<T> &>(get<T>());
if (store.search(id.str()) != 0) { if (store.search(id) != 0)
std::ostringstream msg; {
msg << "Try to override existing record '" << id.str() << "'"; const std::string msg = "Try to override existing record '" + id + "'";
throw std::runtime_error(msg.str()); throw std::runtime_error(msg);
} }
T record = x; T record = x;
record.mId = id.str(); record.mId = id;
T *ptr = store.insert(record); T *ptr = store.insert(record);
for (iterator it = mStores.begin(); it != mStores.end(); ++it) { for (iterator it = mStores.begin(); it != mStores.end(); ++it) {
@ -208,15 +208,15 @@ namespace MWWorld
} }
template <class T> template <class T>
const T *insertStatic(const T &x) { const T *insertStatic(const T &x)
std::ostringstream id; {
id << "$dynamic" << mDynamicCount++; const std::string id = "$dynamic" + std::to_string(mDynamicCount++);
Store<T> &store = const_cast<Store<T> &>(get<T>()); Store<T> &store = const_cast<Store<T> &>(get<T>());
if (store.search(id.str()) != 0) { if (store.search(id) != 0)
std::ostringstream msg; {
msg << "Try to override existing record '" << id.str() << "'"; const std::string msg = "Try to override existing record '" + id + "'";
throw std::runtime_error(msg.str()); throw std::runtime_error(msg);
} }
T record = x; T record = x;
@ -247,20 +247,22 @@ namespace MWWorld
} }
template <> template <>
inline const ESM::NPC *ESMStore::insert<ESM::NPC>(const ESM::NPC &npc) { inline const ESM::NPC *ESMStore::insert<ESM::NPC>(const ESM::NPC &npc)
std::ostringstream id; {
id << "$dynamic" << mDynamicCount++; const std::string id = "$dynamic" + std::to_string(mDynamicCount++);
if (Misc::StringUtils::ciEqual(npc.mId, "player")) { if (Misc::StringUtils::ciEqual(npc.mId, "player"))
{
return mNpcs.insert(npc); return mNpcs.insert(npc);
} else if (mNpcs.search(id.str()) != 0) { }
std::ostringstream msg; else if (mNpcs.search(id) != 0)
msg << "Try to override existing record '" << id.str() << "'"; {
throw std::runtime_error(msg.str()); const std::string msg = "Try to override existing record '" + id + "'";
throw std::runtime_error(msg);
} }
ESM::NPC record = npc; ESM::NPC record = npc;
record.mId = id.str(); record.mId = id;
ESM::NPC *ptr = mNpcs.insert(record); ESM::NPC *ptr = mNpcs.insert(record);
mIds[ptr->mId] = ESM::REC_NPC_; mIds[ptr->mId] = ESM::REC_NPC_;

@ -109,11 +109,10 @@ namespace
if (projectileEffects.mList.size() > 1) // insert a VFX_Multiple projectile if there are multiple projectile effects if (projectileEffects.mList.size() > 1) // insert a VFX_Multiple projectile if there are multiple projectile effects
{ {
std::ostringstream ID; const std::string ID = "VFX_Multiple" + std::to_string(effects->mList.size());
ID << "VFX_Multiple" << effects->mList.size();
std::vector<std::string>::iterator it; std::vector<std::string>::iterator it;
it = projectileIDs.begin(); it = projectileIDs.begin();
it = projectileIDs.insert(it, ID.str()); it = projectileIDs.insert(it, ID);
} }
return projectileEffects; return projectileEffects;
} }

@ -9,7 +9,6 @@
#include <components/misc/rng.hpp> #include <components/misc/rng.hpp>
#include <stdexcept> #include <stdexcept>
#include <sstream>
namespace namespace
{ {
@ -102,10 +101,10 @@ namespace MWWorld
const T *IndexedStore<T>::find(int index) const const T *IndexedStore<T>::find(int index) const
{ {
const T *ptr = search(index); const T *ptr = search(index);
if (ptr == 0) { if (ptr == 0)
std::ostringstream msg; {
msg << T::getRecordType() << " with index " << index << " not found"; const std::string msg = T::getRecordType() + " with index " + std::to_string(index) + " not found";
throw std::runtime_error(msg.str()); throw std::runtime_error(msg);
} }
return ptr; return ptr;
} }
@ -171,10 +170,10 @@ namespace MWWorld
const T *Store<T>::find(const std::string &id) const const T *Store<T>::find(const std::string &id) const
{ {
const T *ptr = search(id); const T *ptr = search(id);
if (ptr == 0) { if (ptr == 0)
std::ostringstream msg; {
msg << T::getRecordType() << " '" << id << "' not found"; const std::string msg = T::getRecordType() + " '" + id + "' not found";
throw std::runtime_error(msg.str()); throw std::runtime_error(msg);
} }
return ptr; return ptr;
} }
@ -184,14 +183,13 @@ namespace MWWorld
const T *ptr = searchRandom(id); const T *ptr = searchRandom(id);
if(ptr == 0) if(ptr == 0)
{ {
std::ostringstream msg; const std::string msg = T::getRecordType() + " starting with '" + id + "' not found";
msg << T::getRecordType() << " starting with '"<<id<<"' not found"; throw std::runtime_error(msg);
throw std::runtime_error(msg.str());
} }
return ptr; return ptr;
} }
template<typename T> template<typename T>
RecordId Store<T>::load(ESM::ESMReader &esm) RecordId Store<T>::load(ESM::ESMReader &esm)
{ {
T record; T record;
bool isDeleted = false; bool isDeleted = false;
@ -364,10 +362,10 @@ namespace MWWorld
const ESM::LandTexture *Store<ESM::LandTexture>::find(size_t index, size_t plugin) const const ESM::LandTexture *Store<ESM::LandTexture>::find(size_t index, size_t plugin) const
{ {
const ESM::LandTexture *ptr = search(index, plugin); const ESM::LandTexture *ptr = search(index, plugin);
if (ptr == 0) { if (ptr == 0)
std::ostringstream msg; {
msg << "Land texture with index " << index << " not found"; const std::string msg = "Land texture with index " + std::to_string(index) + " not found";
throw std::runtime_error(msg.str()); throw std::runtime_error(msg);
} }
return ptr; return ptr;
} }
@ -456,10 +454,10 @@ namespace MWWorld
const ESM::Land *Store<ESM::Land>::find(int x, int y) const const ESM::Land *Store<ESM::Land>::find(int x, int y) const
{ {
const ESM::Land *ptr = search(x, y); const ESM::Land *ptr = search(x, y);
if (ptr == 0) { if (ptr == 0)
std::ostringstream msg; {
msg << "Land at (" << x << ", " << y << ") not found"; const std::string msg = "Land at (" + std::to_string(x) + ", " + std::to_string(y) + ") not found";
throw std::runtime_error(msg.str()); throw std::runtime_error(msg);
} }
return ptr; return ptr;
} }
@ -591,20 +589,20 @@ namespace MWWorld
const ESM::Cell *Store<ESM::Cell>::find(const std::string &id) const const ESM::Cell *Store<ESM::Cell>::find(const std::string &id) const
{ {
const ESM::Cell *ptr = search(id); const ESM::Cell *ptr = search(id);
if (ptr == 0) { if (ptr == 0)
std::ostringstream msg; {
msg << "Cell '" << id << "' not found"; const std::string msg = "Cell '" + id + "' not found";
throw std::runtime_error(msg.str()); throw std::runtime_error(msg);
} }
return ptr; return ptr;
} }
const ESM::Cell *Store<ESM::Cell>::find(int x, int y) const const ESM::Cell *Store<ESM::Cell>::find(int x, int y) const
{ {
const ESM::Cell *ptr = search(x, y); const ESM::Cell *ptr = search(x, y);
if (ptr == 0) { if (ptr == 0)
std::ostringstream msg; {
msg << "Exterior at (" << x << ", " << y << ") not found"; const std::string msg = "Exterior at (" + std::to_string(x) + ", " + std::to_string(y) + ") not found";
throw std::runtime_error(msg.str()); throw std::runtime_error(msg);
} }
return ptr; return ptr;
} }
@ -784,13 +782,10 @@ namespace MWWorld
} }
ESM::Cell *Store<ESM::Cell>::insert(const ESM::Cell &cell) ESM::Cell *Store<ESM::Cell>::insert(const ESM::Cell &cell)
{ {
if (search(cell) != 0) { if (search(cell) != 0)
std::ostringstream msg; {
msg << "Failed to create "; const std::string cellType = (cell.isExterior()) ? "exterior" : "interior";
msg << ((cell.isExterior()) ? "exterior" : "interior"); throw std::runtime_error("Failed to create " + cellType + " cell");
msg << " cell";
throw std::runtime_error(msg.str());
} }
ESM::Cell *ptr; ESM::Cell *ptr;
if (cell.isExterior()) { if (cell.isExterior()) {
@ -931,9 +926,8 @@ namespace MWWorld
const ESM::Pathgrid* pathgrid = search(x,y); const ESM::Pathgrid* pathgrid = search(x,y);
if (!pathgrid) if (!pathgrid)
{ {
std::ostringstream msg; const std::string msg = "Pathgrid in cell '" + std::to_string(x) + " " + std::to_string(y) + "' not found";
msg << "Pathgrid in cell '" << x << " " << y << "' not found"; throw std::runtime_error(msg);
throw std::runtime_error(msg.str());
} }
return pathgrid; return pathgrid;
} }
@ -942,9 +936,8 @@ namespace MWWorld
const ESM::Pathgrid* pathgrid = search(name); const ESM::Pathgrid* pathgrid = search(name);
if (!pathgrid) if (!pathgrid)
{ {
std::ostringstream msg; const std::string msg = "Pathgrid in cell '" + name + "' not found";
msg << "Pathgrid in cell '" << name << "' not found"; throw std::runtime_error(msg);
throw std::runtime_error(msg.str());
} }
return pathgrid; return pathgrid;
} }
@ -998,10 +991,10 @@ namespace MWWorld
const ESM::Attribute *Store<ESM::Attribute>::find(size_t index) const const ESM::Attribute *Store<ESM::Attribute>::find(size_t index) const
{ {
const ESM::Attribute *ptr = search(index); const ESM::Attribute *ptr = search(index);
if (ptr == 0) { if (ptr == 0)
std::ostringstream msg; {
msg << "Attribute with index " << index << " not found"; const std::string msg = "Attribute with index " + std::to_string(index) + " not found";
throw std::runtime_error(msg.str()); throw std::runtime_error(msg);
} }
return ptr; return ptr;
} }

@ -1,7 +1,6 @@
#include "loadcell.hpp" #include "loadcell.hpp"
#include <string> #include <string>
#include <sstream>
#include <list> #include <list>
#include <boost/concept_check.hpp> #include <boost/concept_check.hpp>
@ -209,9 +208,7 @@ namespace ESM
} }
else else
{ {
std::ostringstream stream; return std::to_string(mData.mX) + ", " + std::to_string(mData.mY);
stream << mData.mX << ", " << mData.mY;
return stream.str();
} }
} }

@ -1,7 +1,6 @@
#include "interpreter.hpp" #include "interpreter.hpp"
#include <cassert> #include <cassert>
#include <sstream>
#include <stdexcept> #include <stdexcept>
#include "opcodes.hpp" #include "opcodes.hpp"
@ -116,20 +115,14 @@ namespace Interpreter
void Interpreter::abortUnknownCode (int segment, int opcode) void Interpreter::abortUnknownCode (int segment, int opcode)
{ {
std::ostringstream error; const std::string error = "unknown opcode " + std::to_string(opcode) + " in segment " + std::to_string(segment);
throw std::runtime_error (error);
error << "unknown opcode " << opcode << " in segment " << segment;
throw std::runtime_error (error.str());
} }
void Interpreter::abortUnknownSegment (Type_Code code) void Interpreter::abortUnknownSegment (Type_Code code)
{ {
std::ostringstream error; const std::string error = "opcode outside of the allocated segment range: " + std::to_string(code);
throw std::runtime_error (error);
error << "opcode outside of the allocated segment range: " << code;
throw std::runtime_error (error.str());
} }
void Interpreter::begin() void Interpreter::begin()

@ -8,48 +8,6 @@
#include <boost/filesystem/fstream.hpp> #include <boost/filesystem/fstream.hpp>
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>
namespace
{
bool parseBool(const std::string& string)
{
return (Misc::StringUtils::ciEqual(string, "true"));
}
float parseFloat(const std::string& string)
{
std::stringstream stream;
stream << string;
float ret = 0.f;
stream >> ret;
return ret;
}
int parseInt(const std::string& string)
{
std::stringstream stream;
stream << string;
int ret = 0;
stream >> ret;
return ret;
}
template <typename T>
std::string toString(T val)
{
std::ostringstream stream;
stream << val;
return stream.str();
}
template <>
std::string toString(bool val)
{
return val ? "true" : "false";
}
}
namespace Settings namespace Settings
{ {
@ -393,17 +351,36 @@ std::string Manager::getString(const std::string &setting, const std::string &ca
float Manager::getFloat (const std::string& setting, const std::string& category) float Manager::getFloat (const std::string& setting, const std::string& category)
{ {
return parseFloat( getString(setting, category) ); const std::string value = getString(setting, category);
try
{
return std::stof(value);
}
catch(const std::exception& e)
{
Log(Debug::Warning) << "Cannot parse setting '" << setting << "' (invalid setting value: " << value << ").";
return 0;
}
} }
int Manager::getInt (const std::string& setting, const std::string& category) int Manager::getInt (const std::string& setting, const std::string& category)
{ {
return parseInt( getString(setting, category) ); const std::string value = getString(setting, category);
try
{
return std::stoi(value);
}
catch(const std::exception& e)
{
Log(Debug::Warning) << "Cannot parse setting '" << setting << "' (invalid setting value: " << value << ").";
return 0;
}
} }
bool Manager::getBool (const std::string& setting, const std::string& category) bool Manager::getBool (const std::string& setting, const std::string& category)
{ {
return parseBool( getString(setting, category) ); const std::string& string = getString(setting, category);
return Misc::StringUtils::ciEqual(string, "true");
} }
void Manager::setString(const std::string &setting, const std::string &category, const std::string &value) void Manager::setString(const std::string &setting, const std::string &category, const std::string &value)
@ -424,17 +401,17 @@ void Manager::setString(const std::string &setting, const std::string &category,
void Manager::setInt (const std::string& setting, const std::string& category, const int value) void Manager::setInt (const std::string& setting, const std::string& category, const int value)
{ {
setString(setting, category, toString(value)); setString(setting, category, std::to_string(value));
} }
void Manager::setFloat (const std::string &setting, const std::string &category, const float value) void Manager::setFloat (const std::string &setting, const std::string &category, const float value)
{ {
setString(setting, category, toString(value)); setString(setting, category, std::to_string(value));
} }
void Manager::setBool(const std::string &setting, const std::string &category, const bool value) void Manager::setBool(const std::string &setting, const std::string &category, const bool value)
{ {
setString(setting, category, toString(value)); setString(setting, category, value ? "true" : "false");
} }
const CategorySettingVector Manager::apply() const CategorySettingVector Manager::apply()

Loading…
Cancel
Save