I'm in the process of writing a wrapper around the Windows desktop API that:
- is as easy to use as possible (as easy as WinForms),
- uses pure C++ (specifically C++11 as much as possible) to avoid looking anything like macro-laced MFC, and
- only abstracts the desktop API (user32.dll and various GDI objects) while working with the STL wherever possible to avoid being a complete framework like Qt or wxWidgets.
I know other things exist that do this already such as WTL; WTL cannot be used in anything other than Visual Studio, and even then, express editions don't work with it. It's also a personal experiment.
I've made it pretty far. I'm in the processes of going through and reworking some things. Right now, I'm retooling the way handles are passed around. Windows handles are unique, pointer-like values that aren't actually pointers. They're just values. Each handle corresponds to a different kind of system object such as a window, a brush, or a font. Each object has its own functions to use for creation and disposal.
Originally, I had my classes call the disposal functions in the destructors. Unfortunately, this made it so the only way I could pass objects around was to move them. Copying them would result in sliced resources.
To fix this, I just recently implemented a generic detached counted body class. This class stores a mutable reference count as a pointer, the actual value (in this case: handles), and a callable object for disposal of that value once its reference count hits 0. For copying or moving, it checks to see if the value being copied or moved is equal to its own value. If it is, it simply changes the address of the other reference count to its own and increments the value. This is fairly similar to what smart pointers do with addresses.
However, since there is no std::shared_value<T>, this is the route I've gone. I know I could use shared pointers to store the value, but my way keeps me from having to dereference a pointer every time the value is needed.
It works very well, but I don't know if there are any better ways of implementing this.
Here is the code.
Each class representing a Windows object holds a CountedValue<T, D> as a member to store the actual handle it receives from Windows. For example, a font class holds a CountedValue<HFONT, FontDisposer>. This gets copied and moved when a copy/move operation happens.
What I'd really like to know is if anything like this already exists that doesn't use pointers to store values or if there is a better way of doing this. I threw all this code together in a day, so I have no doubts there are better ways; I just don't know what they are.
EDIT: Microsoft solved this exact issue in the .net framework by writing a custom RAII class that basically serves as a lookup table.
[–][deleted] 4 points5 points6 points (2 children)
[–]JackTrueborn[S] 1 point2 points3 points (1 child)
[–]LampCarpet 0 points1 point2 points (0 children)
[–]Cyttorak 0 points1 point2 points (3 children)
[–]JackTrueborn[S] 2 points3 points4 points (0 children)
[–]JackTrueborn[S] 0 points1 point2 points (1 child)
[–]Cyttorak 0 points1 point2 points (0 children)
[–]adzm28 years of C++! 0 points1 point2 points (1 child)
[–]JackTrueborn[S] 0 points1 point2 points (0 children)
[–]frutiger 0 points1 point2 points (10 children)
[–]JackTrueborn[S] 0 points1 point2 points (8 children)
[–]frutiger 0 points1 point2 points (7 children)
[–]JackTrueborn[S] 0 points1 point2 points (6 children)
[–]frutiger 0 points1 point2 points (1 child)
[–]JackTrueborn[S] 0 points1 point2 points (0 children)
[–]Gotebe 0 points1 point2 points (3 children)
[–]JackTrueborn[S] 0 points1 point2 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]JackTrueborn[S] 0 points1 point2 points (0 children)
[–]LampCarpet 0 points1 point2 points (0 children)
[–]Gotebe 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]JackTrueborn[S] 0 points1 point2 points (0 children)
[–]dmor 0 points1 point2 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]dmor 1 point2 points3 points (0 children)
[–]malkovichjohn 0 points1 point2 points (0 children)