Boost logo

Boost :

Subject: [boost] [Serialization] (and [TypeTraits]?) Overloaded new operator are not called from serialization
From: Runar Undheim (R.Undheim_at_[hidden])
Date: 2008-11-18 08:12:42


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))

Then the heap_allocator is 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)));
        }
    };

regards, Runar


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk