Boost logo

Boost :

From: David B. Held (dheld_at_[hidden])
Date: 2003-02-06 14:42:14

"Philippe A. Bouchard" <philippeb_at_[hidden]> wrote in message
> [...]
> $100 Question:
> When I am looking at the "Storage Policy", p. 189, Modern C++ Design;
> the class must be initialized by a StoredType object while in my situation
> the StoredType is a shifted_object<T> and 'new (so) T' returns a pointer
> to T (member of shifted_boject<T>). What am I supposed to do if I want
> to keep it optimized and simple?

Step 1) Check out policy_ptr in the sandbox. Step 2) Use the template
c'tor in smart_ptr. Step 3) Make your storage policy take the appropriate

Here's the "raw pointer c'tor":

        template <typename U>
        smart_ptr(U const& p)
        : base_type(p, detail::init_first_tag())
        { checking_policy::on_init(get_impl(*this)); }

Here's what your storage_policy might look like:

    template <typename T>
    class shifted_storage
        typedef shifted_object<T>* stored_type; // the type of the
        typedef shifted_object<T const>* const_stored_type; // object
        typedef T* pointer_type; // type returned by
        typedef T const* const_pointer_type;
        typedef add_reference<T>::type
                            reference_type; // type returned by
        typedef add_reference<T const>::type

        typedef shifted_storage type;
        typedef storage_policy_tag policy_category;

        : pointee_(default_value())
        { }

        template <typename U>
        shifted_storage(shifted_storage<U> const& rhs)
        : pointee_(default_value())
        { }

        shifted_storage(scalar_storage const&)
        : pointee_(default_value())
        { }

        shifted_storage(pointer_type p)
        : pointee_(static_cast<stored_type>(
            static_cast<char*>(static_cast<void*>(p)) -
        { }
        // I think the arithmetic above is valid, because we're dealing with
        // POD types, but IANALL, so take it with a grain of salt

        { boost::checked_delete(pointee_); }

        void swap(scalar_storage& rhs)
        { std::swap(pointee_, rhs.pointee_); }

        pointer_type get_pointer() const
        { return &pointee_->m_object; }

        reference_type get_reference() const
        { return pointee_->m_object; }

        bool is_valid() const
        { return pointee_ != default_value(); }

        friend inline pointer_type get_impl(shifted_storage const& sp)
        { return &sp.pointee_->m_object; }

        friend inline stored_type& get_impl_ref(shifted_storage& sp)
        { return sp.pointee_; }

        void release()
        { pointee_ = 0; }

        static stored_type default_value()
        { return 0; }

        stored_type pointee_;

        BOOST_MPL_AUX_LAMBDA_SUPPORT(1, shifted_storage, (T))

#ifdef __BORLANDC__
    namespace mpl { BOOST_MPL_AUX_VOID_SPEC(1, shifted_storage) }


Boost list run by bdawes at, gregod at, cpdaniel at, john at