Help tweaking and/or refactoring a patern.

Started by
2 comments, last by ANSI2000 22 years, 5 months ago
I have written a class that wraps windows NT service code. The class allows you to overide a few methods for simplicity. class Service { public: Service(LPTSTR pApplicationName, LPTSTR pServiceName, LPTSTR pServiceDisplayName, LPTSTR pDependencies); static void WINAPI serviceMain(DWORD dwArgc, LPTSTR *lpszArgv); static void WINAPI serviceControlHandler(DWORD dwCtrlCode); static BOOL WINAPI debugControlHandler(DWORD dwCtrlType); void install(); void remove(); void debug(int argc, char ** argv); void dispatch(); virtual void init(); virtual void start(DWORD dwArgc, LPTSTR *lpszArgv); virtual void stop(); virtual void shutDown(); bool isDebug(); bool ReportStatusToSCMgr(DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwWaitHint); void AddToMessageLog(LPTSTR lpszMsg, WORD pEventType); LPTSTR GetLastErrorText(LPTSTR lpszBuf, DWORD dwSize); virtual ~Service(); private: LPTSTR applicationName; LPTSTR serviceName; LPTSTR serviceDisplayName; LPTSTR dependencies; SERVICE_STATUS ssStatus; // Current status of the service SERVICE_STATUS_HANDLE sshStatusHandle; DWORD dwErr; BOOL bDebug; TCHAR szErr[256]; // Static pointer to Service. Used for calling non static methods within // the call back methods. static Service *thisService; }; The sub class... class MyService: public Service { public: MyService(LPTSTR pApplicationName, LPTSTR pServiceName, LPTSTR pServiceDisplayName, LPTSTR pDependencies); void init(); void start(DWORD dwArgc, LPTSTR *lpszArgv); void stop(); void shutDown(); int getRegistryValues(); static DWORD WINAPI workerThread(LPVOID pBOVStubs); static DWORD WINAPI supportThread(LPVOID pStructure); void formatException(_com_error &pException, char *pExceptionMessage); void dumpToFile(char *pFileName, char *pMessage); ~MyService(); private: // Used to track the status of the service. int serviceStatus; int supportStatus; // Port to listen on for support; int supportPort; // Location of log files. char logsLocation[256]; // Financial institution ID for BOV. int finInstitutionID; // DB connection parameters. char dbServerName[256]; char dbName[256]; char dbServerUsername[256]; char dbServerPassword[256]; // Used for tracking the DB. bool dbConnected; int dbPollInterval; // DB related. _ConnectionPtr dbConnection; _RecordsetPtr rs; _CommandPtr cmd1; _ParameterPtr cmd1Param1; _ParameterPtr cmd1Param2; char dbConString[256]; // Server Connection parameters. char serverIPAddress[16]; int serverPort; int serverMaxTransactions; // Used to access static objects through the threads. HANDLE myMutex; // Number of threads open. long threadCount; // Static pointer to Service. Used for calling non static methods within // the call back methods. static MyService *thisMyService; }; Both the Service and the sub class have static pointers to themselves, because of the callbacks. Does this imply that they are both singleton. Do I really have to force the singleton patern on them by protecting the constructors? Or is there a better way to do this. The application is working fine though... Edited by - ANSI2000 on November 5, 2001 4:31:17 PM
Advertisement
quote:
Does this imply that they are both singleton.

Only if that static property is logically const (it''s assigned once and never changed).

I used a static HWND in a CWindow class (and protected it with a critical section) so that WM_CREATE could be handled by the class. The value was static, but it was assigned to whatever window was being created - so the class was not a singleton.

In your case, it''s worse than a singleton - you can only reliably instance ONE derived class. You could make Service a template:

  template <typename TDerivedService>class Service{//....static TDerivedService* ms_pThis;};class MyService : public Service<MyService>{};  

With that you can instance one object of each derived class at least. Is there no way to attach a cookie to a service? Like the GWL_USERDATA for windows?


Magmai Kai Holmlor
- Not For Rent
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
No you canot create user data there is only one service. There can only be one service per application
Oh, so you you only want one of them - Just put an assertion in the constructor that checks that the static pointer is null.

Magmai Kai Holmlor
- Not For Rent
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara

This topic is closed to new replies.

Advertisement