lmi-commits
[Top][All Lists]
Advanced

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

[lmi-commits] [6292] Resolve gcc-5 issues (VZ)


From: Greg Chicares
Subject: [lmi-commits] [6292] Resolve gcc-5 issues (VZ)
Date: Sun, 20 Sep 2015 17:58:35 +0000

Revision: 6292
          http://svn.sv.gnu.org/viewvc/?view=rev&root=lmi&revision=6292
Author:   chicares
Date:     2015-09-20 17:58:35 +0000 (Sun, 20 Sep 2015)
Log Message:
-----------
Resolve gcc-5 issues (VZ)

Modified Paths:
--------------
    lmi/trunk/ChangeLog
    lmi/trunk/Makefile.am
    lmi/trunk/actuarial_table.cpp
    lmi/trunk/soa_helpers.hpp

Added Paths:
-----------
    lmi/trunk/deserialize_cast.hpp

Modified: lmi/trunk/ChangeLog
===================================================================
--- lmi/trunk/ChangeLog 2015-09-20 15:48:30 UTC (rev 6291)
+++ lmi/trunk/ChangeLog 2015-09-20 17:58:35 UTC (rev 6292)
@@ -36826,3 +36826,12 @@
 Resolve gcc-5 issues (VZ). See:
   http://lists.nongnu.org/archive/html/lmi/2015-09/msg00000.html
 
+20150920T1758Z <address@hidden> [474]
+
+  Makefile.am
+  actuarial_table.cpp
+  deserialize_cast.hpp [new file]
+  soa_helpers.hpp
+Resolve gcc-5 issues (VZ). See:
+  http://lists.nongnu.org/archive/html/lmi/2015-09/msg00010.html
+

Modified: lmi/trunk/Makefile.am
===================================================================
--- lmi/trunk/Makefile.am       2015-09-20 15:48:30 UTC (rev 6291)
+++ lmi/trunk/Makefile.am       2015-09-20 17:58:35 UTC (rev 6292)
@@ -1086,6 +1086,7 @@
     dbvalue.hpp \
     death_benefits.hpp \
     default_view.hpp \
+    deserialize_cast.hpp \
     docmanager_ex.hpp \
     docmdichildframe_ex.hpp \
     edit_mvc_docview_parameters.hpp \

Modified: lmi/trunk/actuarial_table.cpp
===================================================================
--- lmi/trunk/actuarial_table.cpp       2015-09-20 15:48:30 UTC (rev 6291)
+++ lmi/trunk/actuarial_table.cpp       2015-09-20 17:58:35 UTC (rev 6292)
@@ -30,6 +30,7 @@
 
 #include "alert.hpp"
 #include "assert_lmi.hpp"
+#include "deserialize_cast.hpp"
 #include "materially_equal.hpp"
 #include "miscellany.hpp"
 #include "oecumenic_enumerations.hpp"   // methuselah
@@ -84,7 +85,7 @@
         t = invalid;
         char z[sizeof(T)];
         is.read(z, sizeof(T));
-        t = *reinterpret_cast<T*>(z);
+        t = deserialize_cast<T>(z);
         LMI_ASSERT(invalid != t);
         return t;
     }
@@ -574,13 +575,13 @@
     while(index_ifs)
         {
         int index_table_number =
-            *reinterpret_cast<boost::int32_t*>(index_record)
+            deserialize_cast<boost::int32_t>(index_record)
             ;
         if(table_number_ == index_table_number)
             {
             char* p = 54 + index_record;
-            boost::int32_t z = *reinterpret_cast<boost::int32_t*>(p);
-            table_offset_ = std::streampos(static_cast<int>(z));
+            int z = deserialize_cast<boost::int32_t>(p);
+            table_offset_ = std::streampos(z);
             break;
             }
         index_ifs.read(index_record, index_record_length);
@@ -810,7 +811,7 @@
     for(int j = 0; j < number_of_values; ++j)
         {
         is.read(z, sizeof(double));
-        data_[j] = *reinterpret_cast<double*>(z);
+        data_[j] = deserialize_cast<double>(z);
         }
 }
 

Added: lmi/trunk/deserialize_cast.hpp
===================================================================
--- lmi/trunk/deserialize_cast.hpp                              (rev 0)
+++ lmi/trunk/deserialize_cast.hpp      2015-09-20 17:58:35 UTC (rev 6292)
@@ -0,0 +1,57 @@
+// Safe replacement for reinterpret_cast<POD_type>(char*).
+//
+// Copyright (C) 2015 Gregory W. Chicares.
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License version 2 as
+// published by the Free Software Foundation.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software Foundation,
+// Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+//
+// http://savannah.nongnu.org/projects/lmi
+// email: <address@hidden>
+// snail: Chicares, 186 Belle Woods Drive, Glastonbury CT 06033, USA
+
+// $Id$
+
+#ifndef deserialize_cast_hpp
+#define deserialize_cast_hpp
+
+#include "config.hpp"
+
+#include <cstring>                      // std::memcpy()
+
+/// Reinterpret char* contents as a value of the given type.
+///
+/// Motivation: to replace reinterpret_cast with a safe alternative
+/// that is consistent with C++'s strict aliasing requirements, and
+/// whose behavior is therefore always well defined; and which,
+/// incidentally, doesn't elicit a type-punning diagnostic.
+///
+/// The memcpy() call is completely optimized away by all major
+/// compilers (gcc, clang, msvc), so both
+///   t = deserialize_cast<T>(z)
+/// and
+///   t = *reinterpret_cast<T*>(z)
+/// generate equally efficient code.
+///
+/// See the thread culminating in this message:
+///   http://lists.nongnu.org/archive/html/lmi/2015-09/msg00010.html
+
+template<typename T>
+inline T deserialize_cast(char const* z)
+{
+    T t;
+    std::memcpy(&t, z, sizeof(T));
+    return t;
+}
+
+#endif // deserialize_cast_hpp
+


Property changes on: lmi/trunk/deserialize_cast.hpp
___________________________________________________________________
Added: svn:keywords
   + Id

Modified: lmi/trunk/soa_helpers.hpp
===================================================================
--- lmi/trunk/soa_helpers.hpp   2015-09-20 15:48:30 UTC (rev 6291)
+++ lmi/trunk/soa_helpers.hpp   2015-09-20 17:58:35 UTC (rev 6292)
@@ -28,6 +28,7 @@
 
 #include "actuarial_table.hpp"
 #include "alert.hpp"
+#include "deserialize_cast.hpp"
 #include "miscellany.hpp"
 #include "path_utility.hpp"             // fs::path inserter
 
@@ -106,7 +107,7 @@
             }
 
         soa_record_info rec;
-        rec.index = *reinterpret_cast<boost::int32_t*>(index_record);
+        rec.index = deserialize_cast<boost::int32_t>(index_record);
         rec.name.assign(index_record + 4);
         v.push_back(rec);
         }




reply via email to

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