billyquith / ponder

C++ reflection library with Lua binding, and JSON and XML serialisation.
http://billyquith.github.io/ponder/
Other
647 stars 95 forks source link

UserObject function that returns a pointer to the wrapped object #25

Closed Weeena closed 8 years ago

Weeena commented 8 years ago

We would like UserObject to have an addtional function similar to get() that returns a pointer to the wrapped object rather than a reference (e.g. a function getPointer()).

Motivation: At initialization time where performance is not key we like - with the help of ponder - to prepare a data repository and some worker objects that work with the data objects in that repository. We would like to initialize those worker objects such that they store some kind of reference to the data objects. During processing time where performance is key the worker objects should have fast access to the data objects by way of the stored references.

We consider wrapping our data objects in UserObjects. Now, the only way to get some reference to the wrapped object is by way of get() which returns a reference T&. Alas, this reference cannot easily be stored for later use in the processing phase (and we would not like to use a std::reference_wrapper). Hence, we would like to have a function that returns a pointer to the wrapped object and the returned pointer we can store for fast access during processing time.

billyquith commented 8 years ago

Does this not exist already?

    /**
     * \brief Retrieve the address of the stored object
     *
     * This function must be used with caution, as the returned address
     * may not be what you expect it to be!
     *
     * \return Pointer to the stored object
     */
    void* pointer() const;
Weeena commented 8 years ago

Well, I would prefer a function that is as typesafe as get() is. Furthermore, as the comment to pointer() states, "This function must be used with caution": It does not handle multiple inheritance (but get() does).

Weeena commented 8 years ago

I would like to somehow withdraw my claim. I can't remember exactly how it happened, but I overlooked that I can simply apply & to the returned reference from get() to get the pointer to the wrapped object. (I guess I mixed it somewhat up with the fact that one cannot take a pointer to a reference.)

billyquith commented 8 years ago

Ah good, sorry didn't have time to look at this today anyway.