[niepce] Use std::to_string and std::stoi instead of lexical_cast<>



commit 033a0fc3946fa8bc6d38396581122699448d6034
Author: Hubert Figuière <hub figuiere net>
Date:   Sat Aug 31 21:30:41 2013 -0400

    Use std::to_string and std::stoi instead of lexical_cast<>

 src/engine/db/library.cpp             |    5 ++---
 src/engine/library/thumbnailcache.cpp |   11 +++++------
 src/fwk/base/geometry.cpp             |   19 ++++++++++++++-----
 src/fwk/base/geometry.hpp             |   18 +++++++++++++-----
 src/fwk/base/t/testgeometry.cpp       |   30 +++++++++++++++---------------
 src/fwk/toolkit/application.cpp       |    5 ++---
 src/fwk/toolkit/frame.cpp             |   24 ++++++++++++------------
 src/fwk/utils/db/sqlstatement.hpp     |    2 +-
 src/fwk/utils/exempi.cpp              |    2 --
 src/niepce/ui/imageliststore.cpp      |    2 +-
 src/niepce/ui/niepcewindow.cpp        |    2 +-
 src/niepce/ui/workspacecontroller.cpp |   11 ++++-------
 12 files changed, 70 insertions(+), 61 deletions(-)
---
diff --git a/src/engine/db/library.cpp b/src/engine/db/library.cpp
index 08a99e6..bc70a26 100644
--- a/src/engine/db/library.cpp
+++ b/src/engine/db/library.cpp
@@ -23,7 +23,6 @@
 #include <iostream>
 #include <functional>
 
-#include <boost/lexical_cast.hpp>
 #include <boost/format.hpp>
 
 #include <glibmm/i18n.h>
@@ -215,7 +214,7 @@ int Library::checkDatabaseVersion()
         if(m_dbdrv->execute_statement(sql)) {
             if(m_dbdrv->read_next_row()
                && m_dbdrv->get_column_content(0, version)) {
-                v = boost::lexical_cast<int>(version);
+                v = std::stoi(version);
             }
         }
     }
@@ -224,7 +223,7 @@ int Library::checkDatabaseVersion()
         DBG_OUT("db exception %s", e.what());
         v = -1;
     }
-    catch(const boost::bad_lexical_cast &)
+    catch(const std::bad_cast &)
     {
         DBG_OUT("version is %s, can't convert to int", version.c_str());
         v = 0;
diff --git a/src/engine/library/thumbnailcache.cpp b/src/engine/library/thumbnailcache.cpp
index 67776d7..6bead60 100644
--- a/src/engine/library/thumbnailcache.cpp
+++ b/src/engine/library/thumbnailcache.cpp
@@ -21,7 +21,6 @@
 
 #include <functional>
 #include <boost/any.hpp>
-#include <boost/lexical_cast.hpp>
 #include <boost/format.hpp>
 
 #include <gdkmm/pixbuf.h>
@@ -84,7 +83,7 @@ Glib::RefPtr<Gdk::Pixbuf> getThumbnail(const LibFile::Ptr & f, int w, int h, con
     }
 
     DBG_OUT("MIME type %s", mime_type.string().c_str());
-       
+
     Glib::RefPtr<Gdk::Pixbuf> pix;
 
     if(mime_type.isUnknown()) {
@@ -114,9 +113,9 @@ Glib::RefPtr<Gdk::Pixbuf> getThumbnail(const LibFile::Ptr & f, int w, int h, con
         catch(const Glib::Error & e) {
             ERR_OUT("exception thumbnailing image %s", e.what().c_str());
         }
-    }  
-    else {     
-        GdkPixbuf *pixbuf = or_gdkpixbuf_extract_rotated_thumbnail(filename.c_str(), 
+    }
+    else {
+        GdkPixbuf *pixbuf = or_gdkpixbuf_extract_rotated_thumbnail(filename.c_str(),
                                                                    std::min(w, h));
         if(pixbuf) {
             pix = Glib::wrap(pixbuf, true); // take ownership
@@ -176,7 +175,7 @@ std::string ThumbnailCache::path_for_thumbnail(const std::string & filename, lib
 
 std::string ThumbnailCache::dir_for_thumbnail(int size) const
 {
-    std::string subdir = size ? boost::lexical_cast<std::string>(size) : "full";
+    std::string subdir = size ? std::to_string(size) : "full";
     return Glib::build_filename(m_cacheDir, subdir);
 }
 
diff --git a/src/fwk/base/geometry.cpp b/src/fwk/base/geometry.cpp
index 49761f2..38a3065 100644
--- a/src/fwk/base/geometry.cpp
+++ b/src/fwk/base/geometry.cpp
@@ -52,13 +52,20 @@ Rect::Rect(const std::string & s)
     v.reserve(4);
     boost::split(v, s, boost::is_any_of(" "));
     if(v.size() < 4) {
-        throw(std::bad_cast());
+        throw std::bad_cast();
     }
     int i = 0;
     for_each(v.begin(), v.end(),
              [&i, this] (const std::string &s) {
-                 _r[i] = boost::lexical_cast<int>(s);
-                 i++;
+                 try {
+                     _r[i] = std::stoi(s);
+                     i++;
+                 }
+                 catch(...) {
+                     // we likely got an invalid_argument.
+                     // Doesn't matter, at that point it is a bad_cast
+                     throw std::bad_cast();
+                 }
              }
         );
 }
@@ -122,12 +129,14 @@ Rect Rect::fill_into(const Rect & dest) const
     return result;
 }
 
+}
 
+namespace std {
 
-std::string Rect::to_string() const
+std::string to_string(const fwk::Rect & r)
 {
     return str(boost::format("%1% %2% %3% %4%")
-               % _r[X] % _r[Y] % _r[W] % _r[H]); 
+               % r._r[fwk::Rect::X] % r._r[fwk::Rect::Y] % r._r[fwk::Rect::W] % r._r[fwk::Rect::H]);
 }
 
 }
diff --git a/src/fwk/base/geometry.hpp b/src/fwk/base/geometry.hpp
index e238929..2011663 100644
--- a/src/fwk/base/geometry.hpp
+++ b/src/fwk/base/geometry.hpp
@@ -25,7 +25,17 @@
 #include <array>
 #include <string>
 
-#include <boost/lexical_cast.hpp>
+namespace fwk {
+class Rect;
+}
+
+namespace std {
+/** convert to a string in the same format as
+ * accepted by the %Rect(const std::string & s) constructor.
+ * Override of std::to_string/.
+ */
+std::string to_string(const fwk::Rect &);
+}
 
 namespace fwk {
 
@@ -50,10 +60,8 @@ public:
         { return _r[W]; }
     int h() const
         { return _r[H]; }
-    /** convert to a string in the same format as
-     * accepted by the %Rect(const std::string & s) constructor.
-     */
-    std::string to_string() const;
+
+    friend std::string std::to_string(const Rect &);
 
     Rect & scale(double _s);
 
diff --git a/src/fwk/base/t/testgeometry.cpp b/src/fwk/base/t/testgeometry.cpp
index c386fc1..11ccdd3 100644
--- a/src/fwk/base/t/testgeometry.cpp
+++ b/src/fwk/base/t/testgeometry.cpp
@@ -29,8 +29,8 @@ using fwk::Rect;
 int test_main( int, char *[] )             // note the name!
 {
        Rect r1(0,1,2,3);
-       BOOST_CHECK(r1.to_string() == "0 1 2 3");
-       
+       BOOST_CHECK(std::to_string(r1) == "0 1 2 3");
+
        std::string s("100 100 250 250");
        Rect r2(s);
        BOOST_CHECK(r2.x() == 100);
@@ -64,34 +64,34 @@ int test_main( int, char *[] )             // note the name!
 
     // FIT
     result = source1.fit_into(dest1);
-    std::cout << result.to_string() << std::endl;
+    std::cout << std::to_string(result) << std::endl;
     BOOST_CHECK(result == Rect(0, 0, 640, 320));
     result = source1.fit_into(dest2);
-    std::cout << result.to_string() << std::endl;
-    BOOST_CHECK(result.w() == 480);    
+    std::cout << std::to_string(result) << std::endl;
+    BOOST_CHECK(result.w() == 480);
 
     result = source2.fit_into(dest1);
-    std::cout << result.to_string() << std::endl;
+    std::cout << std::to_string(result) << std::endl;
     BOOST_CHECK(result.h() == 480);
     result = source2.fit_into(dest2);
-    std::cout << result.to_string() << std::endl;
-    BOOST_CHECK(result == Rect(0, 0, 320, 640));    
+    std::cout << std::to_string(result) << std::endl;
+    BOOST_CHECK(result == Rect(0, 0, 320, 640));
 
     // FILL
     result = source1.fill_into(dest1);
-    std::cout << result.to_string() << std::endl;
+    std::cout << std::to_string(result) << std::endl;
     BOOST_CHECK(result.h() == 480);
     result = source1.fill_into(dest2);
-    std::cout << result.to_string() << std::endl;
-    BOOST_CHECK(result == Rect(0, 0, 1280, 640));    
+    std::cout << std::to_string(result) << std::endl;
+    BOOST_CHECK(result == Rect(0, 0, 1280, 640));
 
     result = source2.fill_into(dest1);
-    std::cout << result.to_string() << std::endl;
+    std::cout << std::to_string(result) << std::endl;
     BOOST_CHECK(result == Rect(0, 0, 640, 1280));
     result = source2.fill_into(dest2);
-    std::cout << result.to_string() << std::endl;
-    BOOST_CHECK(result.w() == 480);    
+    std::cout << std::to_string(result) << std::endl;
+    BOOST_CHECK(result.w() == 480);
 
-       return 0;
+    return 0;
 }
 
diff --git a/src/fwk/toolkit/application.cpp b/src/fwk/toolkit/application.cpp
index 4ea66d5..c6416e3 100644
--- a/src/fwk/toolkit/application.cpp
+++ b/src/fwk/toolkit/application.cpp
@@ -19,7 +19,6 @@
  */
 
 #include <functional>
-#include <boost/lexical_cast.hpp>
 
 #include <glibmm/i18n.h>
 #include <gtkmm/main.h>
@@ -68,7 +67,7 @@ bool Application::get_use_dark_theme() const
 {
     bool v;
     try {
-        v = boost::lexical_cast<bool>(m_config.getValue("ui_dark_theme", "0"));
+        v = std::stoi(m_config.getValue("ui_dark_theme", "0"));
     }
     catch(...) {
         v = false;
@@ -79,7 +78,7 @@ bool Application::get_use_dark_theme() const
 void Application::set_use_dark_theme(bool value)
 {
     m_config.setValue("ui_dark_theme",
-                      boost::lexical_cast<Glib::ustring>(value));
+                      std::to_string(value));
 }
 
 /** Main loop.
diff --git a/src/fwk/toolkit/frame.cpp b/src/fwk/toolkit/frame.cpp
index 819c3d7..ffb4bb7 100644
--- a/src/fwk/toolkit/frame.cpp
+++ b/src/fwk/toolkit/frame.cpp
@@ -106,7 +106,7 @@ void Frame::set_title(const std::string & title)
 void Frame::toggle_tools_visible()
 {
     if(m_hide_tools_action->get_active()) {
-        signal_hide_tools.emit();   
+        signal_hide_tools.emit();
     }
     else {
         signal_show_tools.emit();
@@ -159,16 +159,16 @@ Glib::RefPtr<Gtk::Action> Frame::create_redo_action(const Glib::RefPtr<Gtk::Acti
 
 bool Frame::_close()
 {
-               if(Controller::Ptr parent = m_parent.lock()) {
+    if(Controller::Ptr parent = m_parent.lock()) {
         parent->remove(shared_from_this());
-               }
-               return false;
+    }
+    return false;
 }
 
 void Frame::frameRectFromConfig()
 {
-               DBG_OUT("loading frame rect (%s)", m_layout_cfg_key.c_str());
-               if(!m_layout_cfg_key.empty()) {
+    DBG_OUT("loading frame rect (%s)", m_layout_cfg_key.c_str());
+    if(!m_layout_cfg_key.empty()) {
         Configuration & cfg = Application::app()->config();
         std::string val;
         val = cfg.getValue(m_layout_cfg_key, "");
@@ -176,29 +176,29 @@ void Frame::frameRectFromConfig()
             try {
                 fwk::Rect r(val);
                 m_window->move(r.x(), r.y());
-                m_window->resize(r.w(), r.h());                                
+                m_window->resize(r.w(), r.h());
             }
             catch(std::bad_cast)
             {
                 ERR_OUT("wrong value in configuration: %s", val.c_str());
             }
         }
-               }
+    }
 }
 
 
 void Frame::frameRectToConfig()
 {
-               DBG_OUT("saving frame rect (%s)", m_layout_cfg_key.c_str());
-               if(!m_layout_cfg_key.empty()) {
+    DBG_OUT("saving frame rect (%s)", m_layout_cfg_key.c_str());
+    if(!m_layout_cfg_key.empty()) {
         Configuration & cfg = Application::app()->config();
         int x, y, w, h;
         x = y = w = h = 0;
         m_window->get_position(x, y);
         m_window->get_size(w, h);
         fwk::Rect r(x, y, w, h);
-        cfg.setValue(m_layout_cfg_key, r.to_string());
-               }
+        cfg.setValue(m_layout_cfg_key, std::to_string(r));
+    }
 }
 
 /*
diff --git a/src/fwk/utils/db/sqlstatement.hpp b/src/fwk/utils/db/sqlstatement.hpp
index c2b1f7c..2b2178f 100644
--- a/src/fwk/utils/db/sqlstatement.hpp
+++ b/src/fwk/utils/db/sqlstatement.hpp
@@ -63,7 +63,7 @@ public:
             uint64_t a_value,
             bool a_auto_increment=false):
             m_name (a_name),
-            m_value (boost::lexical_cast<std::string>(a_value)),
+            m_value (std::to_string(a_value)),
             m_auto_increment (a_auto_increment)
     {}
 
diff --git a/src/fwk/utils/exempi.cpp b/src/fwk/utils/exempi.cpp
index 223eb63..bd9ca8a 100644
--- a/src/fwk/utils/exempi.cpp
+++ b/src/fwk/utils/exempi.cpp
@@ -21,8 +21,6 @@
 #include <string.h>
 #include <time.h>
 
-#include <boost/lexical_cast.hpp>
-
 #include <glib.h>
 #include <giomm/file.h>
 
diff --git a/src/niepce/ui/imageliststore.cpp b/src/niepce/ui/imageliststore.cpp
index b2eb505..0913d03 100644
--- a/src/niepce/ui/imageliststore.cpp
+++ b/src/niepce/ui/imageliststore.cpp
@@ -139,7 +139,7 @@ void ImageListStore::on_lib_notification(const eng::LibNotification &ln)
         Glib::ustring xmp_pref;
         try {
             xmp_pref = cfg.getValue("write_xmp_automatically", "0");
-            write_xmp = boost::lexical_cast<int>(xmp_pref);
+            write_xmp = std::stoi(xmp_pref);
         }
         catch(const std::exception & e)
         {
diff --git a/src/niepce/ui/niepcewindow.cpp b/src/niepce/ui/niepcewindow.cpp
index 7c9415d..6a91be7 100644
--- a/src/niepce/ui/niepcewindow.cpp
+++ b/src/niepce/ui/niepcewindow.cpp
@@ -331,7 +331,7 @@ void NiepceWindow::on_open_library()
     std::string libMoniker;
     int reopen = 0;
     try {
-        reopen = boost::lexical_cast<int>(cfg.getValue("reopen_last_library", "0"));
+        reopen = std::stoi(cfg.getValue("reopen_last_library", "0"));
     }
     catch(...)
     {
diff --git a/src/niepce/ui/workspacecontroller.cpp b/src/niepce/ui/workspacecontroller.cpp
index 9acb234..70346ce 100644
--- a/src/niepce/ui/workspacecontroller.cpp
+++ b/src/niepce/ui/workspacecontroller.cpp
@@ -124,7 +124,7 @@ void WorkspaceController::on_lib_notification(const eng::LibNotification &ln)
         if(iter != m_folderidmap.end()) {
             Gtk::TreeRow row = *(iter->second);
             row[m_librarycolumns.m_count_n] = count.second;
-            row[m_librarycolumns.m_count] = boost::lexical_cast<Glib::ustring>(count.second);
+            row[m_librarycolumns.m_count] = std::to_string(count.second);
         }
 
         break;
@@ -139,7 +139,7 @@ void WorkspaceController::on_lib_notification(const eng::LibNotification &ln)
             Gtk::TreeRow row = *(iter->second);
             int new_count = row[m_librarycolumns.m_count_n] + count.second;
             row[m_librarycolumns.m_count_n] = new_count;
-            row[m_librarycolumns.m_count] = boost::lexical_cast<Glib::ustring>(new_count);
+            row[m_librarycolumns.m_count] = std::to_string(new_count);
         }
 
         break;
@@ -150,7 +150,6 @@ void WorkspaceController::on_lib_notification(const eng::LibNotification &ln)
                    "incorrect data type for the notification");
 
         std::pair<eng::library_id_t,eng::library_id_t> 
moved(boost::any_cast<std::pair<eng::library_id_t,eng::library_id_t> >(ln.param));
-        
 
         break;
     }
@@ -206,8 +205,7 @@ void WorkspaceController::on_row_expanded_collapsed(const Gtk::TreeIter& iter,
         break;
     }
     if(cfg && key) {
-        cfg->setValue(key,
-                      boost::lexical_cast<std::string>(expanded));
+        cfg->setValue(key, std::to_string(expanded));
     }
 }
 
@@ -338,8 +336,7 @@ void WorkspaceController::expand_from_cfg(const char* key,
 {
     fwk::Configuration::Ptr cfg = getLibraryConfig();
 
-    bool expanded =
-        boost::lexical_cast<int>(cfg->getValue(key, "1"));
+    bool expanded = std::stoi(cfg->getValue(key, "1"));
     if(expanded) {
         m_librarytree.expand_row(m_treestore->get_path(treenode),
                                  false);


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