dsi_cm_library.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. This software is subject to the license described in the License.txt file
  3. included with this software distribution. You may not use this file except in compliance
  4. with this license.
  5. Copyright (c) Dynastream Innovations Inc. 2014
  6. All rights reserved.
  7. */
  8. #ifndef CM_LIBRARY_HPP
  9. #define CM_LIBRARY_HPP
  10. #include "types.h"
  11. #include "DSI_CP210xManufacturingDLL_3_1.h"
  12. #include <memory>
  13. struct CMError
  14. {
  15. enum Enum
  16. {
  17. NONE,
  18. NO_LIBRARY,
  19. NO_FUNCTION
  20. };
  21. private: CMError();
  22. };
  23. /*
  24. * This class started out as a singleton, but you cannot control when a static variable destroys
  25. * itself at the end of an application. Therefore, the class was changed to a normal wrapper around
  26. * the library. This means that if you retrieve an instance of the class and then destroy it, you will
  27. * unload the whole library. Since we would rather the library stay loaded for the rest of the application if
  28. * we use it, there is an extra call to load the library so that the library is guaranteed to not unload until
  29. * the end of the application. As well, since each instance also calls to load the library, the library is
  30. * not freed until everyone is done using it.
  31. */
  32. //NOTE: Make sure this class isn't being depended upon by another global variable!
  33. //NOTE: Not thread-safe.
  34. class CMLibrary
  35. {
  36. public:
  37. //!!maybe return another smart pointer like shared_ptr?
  38. static BOOL Load(std::auto_ptr<const CMLibrary>& clAutoLibrary_); //!! Alternative to creating directly and having to worry about try statements
  39. /* Otherwise you'd have to do this...
  40. //Get a reference to library
  41. auto_ptr<CMLibrary> pclAutoLibrary(NULL);
  42. try { pclAutoLibrary.reset(new CMLibrary); }
  43. catch(...) { return clList; }
  44. const CMLibrary& clCMLibrary = *pclAutoCMLibrary;
  45. */
  46. CMLibrary(); //throw(CMError::Enum)
  47. virtual ~CMLibrary() throw();
  48. typedef CP210x_STATUS (WINAPI* GetDeviceProductString_t)(HANDLE cyHandle, LPVOID lpProduct, LPBYTE lpbLength, BOOL bConvertToASCII);
  49. typedef CP210x_STATUS (WINAPI* GetDeviceSerialNumber_t)(HANDLE cyHandle, LPVOID lpSerialNumber, LPBYTE lpbLength, BOOL bConvertToASCII);
  50. typedef CP210x_STATUS (WINAPI* GetDeviceVid_t)(HANDLE cyHandle, LPWORD lpwVid);
  51. typedef CP210x_STATUS (WINAPI* GetDevicePid_t)(HANDLE cyHandle, LPWORD lpwPid);
  52. typedef CP210x_STATUS (WINAPI* Reset_t)(HANDLE cyHandle);
  53. GetDeviceProductString_t GetDeviceProductString;
  54. GetDeviceSerialNumber_t GetDeviceSerialNumber;
  55. GetDeviceVid_t GetDeviceVid;
  56. GetDevicePid_t GetDevicePid;
  57. Reset_t ResetDevice;
  58. private:
  59. CMError::Enum LoadFunctions();
  60. void FreeFunctions();
  61. static std::auto_ptr<CMLibrary> clAutoInstance; //keeps the library loaded for the duration of the application
  62. //NOTE: There is no control when this gets destroyed at end of program
  63. // but it doesn't matter because it's main purpose is to keep the library loaded
  64. // during the duration of the whole application.
  65. HMODULE hLibHandle;
  66. static BOOL bStaticSet;
  67. //!!Could dynamically make all instances and push them onto a static list to delete when we get to it
  68. };
  69. #endif //CM_LIBRARY_HPP