[Boost-bugs] [Boost C++ Libraries] #2520: Overloaded new operator are not called from serialization

Subject: [Boost-bugs] [Boost C++ Libraries] #2520: Overloaded new operator are not called from serialization
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2008-11-20 10:54:48


#2520: Overloaded new operator are not called from serialization
-------------------------------------------------+--------------------------
 Reporter: Runar Undheim <R.Undheim_at_[hidden]> | Owner: ramey
     Type: Feature Requests | Status: new
Milestone: Boost 1.38.0 | Component: serialization
  Version: Boost 1.37.0 | Severity: Problem
 Keywords: overloading new operator |
-------------------------------------------------+--------------------------
 Could someone help to update the serialization code in boost to call the
 overloaded new operator, please?
 There already exist some code, but the code is not active because of a #if
 0 statement. And if I set the code to #if 1 the code is not compiling with
 Visual C++ 2005. So I have updated the heap_allocator implementation in
 archive\detail\iserializer.hpp to support overload of new operator, but
 I'm not sure if it will work on all compilers. I have just tested the code
 with Visual C++ 2005.

 First the code that I used to check if the new operator is overloaded. If
 there are compilers that don't support the code, the implementation could
 be updated using defines and only return false. This way we take advantage
 of the information on supported compilers, an the code will work as before
 on unsupported compilers. Should the code that check if it has a new
 operator be part of the Type Traits library?

 {{{
 typedef char True; // sizeof(True) == 1
 typedef struct { char a[2]; } False; // sizeof(False) > 1

 template <class U, U x> struct test;

 template <typename C>
 const True *hasNewOperator(C*,
   test<void *(*)(size_t),&C::operator new>* = NULL) {
     // Function must be implemented on
     // Visual C++ 2005 to prevent linking error
     return NULL;
   }

 template <typename R>
 const False *hasNewOperator(...) {
   // Function must be implemented on
   // Visual C++ 2005 to prevent linking error
   return NULL;
 }

 /* #define hasNewOperator(f) (sizeof(*hasNewOperator<f>
   (static_cast<f*>(NULL))) == sizeof(True)) */
 }}}

 The heap_allocator is then updated to use the hasNewOperator:

 {{{
 template<class T>
 struct heap_allocator
 {
   template<class U>
   static T* new_operator(U);

   static T* new_operator(const True*) {
     return static_cast<T *>((T::operator new)(sizeof(T)));
   }

   static T* new_operator(const False*) {
     return static_cast<T *>(operator new(sizeof(T)));
   }

   static T * invoke() {
     return new_operator(hasNewOperator<T>(
       static_cast<T *>(NULL)));
   }
 };
 }}}

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/2520>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:49:59 UTC