Boost logo

Boost :

Subject: Re: [boost] [Serialization] (and [TypeTraits]?) Overloaded new operator are not called from serialization
From: Robert Ramey (ramey_at_[hidden])
Date: 2008-11-19 10:56:07


Package this as a TRAK item.

Robert Ramey

Runar Undheim wrote:
> 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
> _______________________________________________
> Unsubscribe & other changes:
> http://lists.boost.org/mailman/listinfo.cgi/boost


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