Skip to content

Commit

Permalink
Removed ostringstream from utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
ethouris committed May 26, 2024
1 parent 6ce84fe commit 8126c49
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions srtcore/utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ class FixedArray

void throw_invalid_index(int i) const
{
std::stringstream ss;
fmt::obufstream ss;
ss << "Index " << i << "out of range";
throw std::runtime_error(ss.str());
}
Expand Down Expand Up @@ -587,7 +587,7 @@ inline Stream& Print(Stream& sout, Arg1&& arg1, Args&&... args)
template <class... Args>
inline std::string Sprint(Args&&... args)
{
std::ostringstream sout;
fmt::obufstream sout;
Print(sout, args...);
return sout.str();
}
Expand All @@ -598,19 +598,32 @@ inline std::string Sprint(Args&&... args)
template <class T>
using UniquePtr = std::unique_ptr<T>;

template <class Container, class Value = typename Container::value_type, typename... Args> inline
std::string Printable(const Container& in, Value /*pseudoargument*/, Args&&... args)
template <class Container, class Value = typename Container::value_type> inline
std::string Printable(const Container& in, Value /*pseudoargument*/, const char* fmt = 0)
{
using namespace srt_pair_op;
std::ostringstream os;
Print(os, args...);
fmt::obufstream os;
os << "[ ";
for (auto i: in)
os << Value(i) << " ";
os << fmt::sfmt<Value>(i, fmt) << " ";
os << "]";
return os.str();
}

// Separate version for pairs, used for std::map
template <class Container, class Key, class Value> inline
std::string Printable(const Container& in, std::pair<Key, Value>/*pseudoargument*/, const char* fmtk = 0, const char* fmtv = 0)
{
using namespace srt_pair_op;
fmt::obufstream os;
os << "[ ";
for (auto i: in)
os << fmt::sfmt<Key>(i.first, fmtk) << ":" << fmt::sfmt<Value>(i.second, fmtv) << " ";
os << "]";
return os.str();
}


template <class Container> inline
std::string Printable(const Container& in)
{
Expand Down Expand Up @@ -694,16 +707,14 @@ class UniquePtr: public std::auto_ptr<T>
template <class Arg1>
inline std::string Sprint(const Arg1& arg)
{
std::ostringstream sout;
sout << arg;
return sout.str();
return fmt::sfmts(arg);
}

// Ok, let it be 2-arg, in case when a manipulator is needed
template <class Arg1, class Arg2>
inline std::string Sprint(const Arg1& arg1, const Arg2& arg2)
{
std::ostringstream sout;
fmt::obufstream sout;
sout << arg1 << arg2;
return sout.str();
}
Expand All @@ -713,7 +724,7 @@ std::string Printable(const Container& in)
{
using namespace srt_pair_op;
typedef typename Container::value_type Value;
std::ostringstream os;
fmt::obufstream os;
os << "[ ";
for (typename Container::const_iterator i = in.begin(); i != in.end(); ++i)
os << Value(*i) << " ";
Expand Down Expand Up @@ -759,7 +770,7 @@ std::string PrintableMod(const Container& in, const std::string& prefix)
{
using namespace srt_pair_op;
typedef typename Container::value_type Value;
std::ostringstream os;
fmt::obufstream os;
os << "[ ";
for (typename Container::const_iterator y = in.begin(); y != in.end(); ++y)
os << prefix << Value(*y) << " ";
Expand Down

0 comments on commit 8126c49

Please sign in to comment.