Thursday, June 14, 2007

provide access to raw resources in resource managing classes

you need direct access to raw resource for legacy or other reasons.

so you wanna provide implicit or explicit conversions to get to the raw resource.

tr1 and auto both offer a member function to perform explicit conversions ( interesting what wud happen if i were to take a copy of the raw pointer inside the auto i need to make sure that it wud never get deleted ?)

tr1 and auto also overload the -> and * operations to allow implicit conversion to the underlying raw pointers

you can give an operator to return the original datamember, so that compiler can automatically take the raw pointer when required based on acutal arguments. e.g.

class Font{
public: operator FontHandle() const { return f ; }
FontHandle f;

where font is a reosurce manager class for the FontHandle object

note it increases the chance of an error.

make interfaces easy to use correctly and hard to use incorrectly

raii classes dont exist for resource encapsulation purpose but to ensure that resources are released. so giving out the raw pointer from them isnt really against encapsulation.

tr1 hides the entire rcsp mechanism ..thus offering clients only what they need to they kind of implement encapsulation of implementation too.

things to remember:
apis often require access to raw resource, so eahc raii class should offer a way to get at the resource it manages
access may be via explicit conversion or implicit conversion. in general explicit conversion is safer, but implicit conversion is more convenient for clients.

No comments: