lmi-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lmi-commits] [lmi] master 65c3c95 5/5: Modernize logic


From: Greg Chicares
Subject: [lmi-commits] [lmi] master 65c3c95 5/5: Modernize logic
Date: Fri, 31 Jan 2020 11:29:40 -0500 (EST)

branch: master
commit 65c3c95a3f9cd9424714925fc447489602d91dbf
Author: Gregory W. Chicares <address@hidden>
Commit: Gregory W. Chicares <address@hidden>

    Modernize logic
---
 stream_cast.hpp | 41 ++++++++++++++++++++++-------------------
 1 file changed, 22 insertions(+), 19 deletions(-)

diff --git a/stream_cast.hpp b/stream_cast.hpp
index a1d1b47..816ca43 100644
--- a/stream_cast.hpp
+++ b/stream_cast.hpp
@@ -104,8 +104,22 @@ To stream_cast(From from, To = To())
         );
     static_assert(!std::is_pointer<To>::value);
 
-    To result = To();
-    std::ostringstream err;
+    auto complain = [&](auto const& reason)
+        {
+        std::ostringstream err;
+        err
+            << reason
+            << " converting '"
+            << from
+            << "' from type '"
+            << lmi::TypeInfo(typeid(From))
+            << "' to type '"
+            << lmi::TypeInfo(typeid(To))
+            << "'."
+            ;
+        throw std::runtime_error(err.str());
+        };
+
     static std::stringstream interpreter = []
         {
         std::stringstream ss {};
@@ -115,33 +129,22 @@ To stream_cast(From from, To = To())
     interpreter.str(std::string{});
     interpreter.clear();
 
+    To result = To();
+
     if(!(interpreter << from))
         {
-        err << "Failure in ostream inserter ";
+        complain("Failure in ostream inserter");
         }
     else if(!(interpreter >> result))
         {
-        err << "Failure in istream extractor ";
+        complain("Failure in istream extractor");
         }
     else if(!(interpreter >> std::ws).eof())
         {
-        err << "Unconverted data remains ";
-        }
-    else
-        {
-        return result;
+        complain("Unconverted data remains");
         }
 
-    err
-        << "converting '"
-        << from
-        << "' from type '"
-        << lmi::TypeInfo(typeid(From))
-        << "' to type '"
-        << lmi::TypeInfo(typeid(To))
-        << "'."
-        ;
-    throw std::runtime_error(err.str());
+    return result;
 }
 
 template<>



reply via email to

[Prev in Thread] Current Thread [Next in Thread]