I'm pretty new to C++ and the more I read the more questions I have.
I want to convert an old C code of mine, which lists the devices on a machine, to a well designed C++ code that does the same.
As I have read, one of the most basic concept of C++ is isolation of Containers, Iterators and Algorithms. That said, I came into conclusion that I should create 3 classes:
-
DeviceInterface - Holds a single interface info such as SP_DEVICE_INTERFACE_DATA, PSP_DEVICE_INTERFACE_DETAIL_DATA and SP_DEVINFO_DATA
-
DevicesInterfaceContainer - Container that somehow holds all the devices information in form of DeviceInterface classes.
-
DeviceInterfaceIterator - Used by the algorithm to iterate over the items in the DevicesInterfaceContainer.
I didn't manage to implement it because I wasn't sure how.
-
Should the iterator be an output iterator that initializes the DevicesInterfaceContainer? Or whether the DevicesInterfaceContainer should initialize and acquire all the devices by its own?
-
What happens when an Win32 api fails in the constructor? Should I throw an exception? if so, what kind of exception? std::exception? std::runtime_error? or should I define a Win32Exception of my own that holds information about the LastError? (All I need is the last error in order to format the message.)
Something like:
class DeviceInterfacesList
{
public:
DeviceInterfacesList(GUID& rtInterfaceGuid)
{
m_hDevInfo = SetupDiGetClassDevs(
&rtInterfaceGuid,
NULL,
NULL,
DIGCF_DEVICEINTERFACE
);
if (INVALID_HANDLE_VALUE == m_hDevInfo)
{
throw Win32Exception();
}
}
...
...
...
HDEVINFO m_hDevInfo;
};
I will be glad to get some guidance / good updated examples for using STL + win32api or anything that will clarify my questions.
Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire