Service.h

Go to the documentation of this file.
00001 #ifndef __SERVICE_H__
00002 #define __SERVICE_H__
00003 
00004 /*
00005 
00006 $Header$
00007 
00008 */
00009 
00010 #include "ServiceBase.h"
00011 #include "Error.h"
00012 #include "String.h"
00013 
00014 // Messages.
00015 static const char *const SERVICE_MSG_INVALID_PARAMS     = "Invalid parameters for service '%s'.";
00016 static const char *const SERVICE_MSG_INVALID_PARAM_TYPE = "Invalid parameter type for service '%s'.";
00017 
00018 using namespace std;
00019 
00020 // Declaration.
00021 
00022 template<class T>
00023 class Service : public ServiceBase
00024 {
00025 
00026   PYLON_OBJECT
00027 
00028 public:
00029 
00030   static T *spawn(const ValueList &params = ValueList());
00031   virtual ServiceBase *fork();
00032 
00033   static String NAME();
00034   static String MANUAL();
00035 
00036   static unsigned int instancesNumber();
00037 
00038 protected:
00039 
00040   Service(const ValueList &params);
00041   virtual ~Service();
00042 
00043 private:
00044 
00045   static unsigned int _instancesNumber;
00046   static unsigned int _forkedInstancesNumber;
00047   bool _forked;
00048 };
00049 
00050 // Implementation.
00051 
00052 template<class T>
00053 unsigned int Service<T>::_instancesNumber = 0;
00054 
00055 template<class T>
00056 unsigned int Service<T>::_forkedInstancesNumber = 0;
00057 
00058 // Public methods.
00059 
00060 template<class T>
00061 T *Service<T>::spawn(const ValueList &params)
00062 {
00063   T *instance;
00064   int res;
00065 
00066   if (!(instance = new T(params))) {
00067     ERROR(MSG_OBJECT_CANNOT_CREATE, CSTRING(T::CLASS_NAME()));
00068     return NULL;
00069   }
00070   if (FAILED(res = instance->failureCode())) {
00071     delete instance;
00072     ERROR_BACKTRACE(res);
00073     return NULL;
00074   }
00075 
00076   return instance;
00077 }
00078 
00079 template<class T>
00080 ServiceBase *Service<T>::fork()
00081 {
00082   T *instance;
00083   int res;
00084 
00085   if (!(instance = new T(_params))) {
00086     ERROR(MSG_OBJECT_CANNOT_CREATE, CSTRING(T::CLASS_NAME()));
00087     return NULL;
00088   }
00089   if (FAILED(res = instance->failureCode())) {
00090     delete instance;
00091     ERROR_BACKTRACE(res);
00092     return NULL;
00093   }
00094 
00095   ++_forkedInstancesNumber;
00096   instance->_forked = true;
00097   EMIT(forked);
00098 
00099   return instance;
00100 }
00101 
00102 template<class T>
00103 String Service<T>::NAME()
00104 {
00105   return T::NAME();
00106 }
00107 
00108 template<class T>
00109 String Service<T>::MANUAL()
00110 {
00111   return T::MANUAL();
00112 }
00113 
00114 template<class T>
00115 unsigned int Service<T>::instancesNumber()
00116 {
00117   return _instancesNumber;
00118 }
00119 
00120 // Protected methods.
00121 
00122 template<class T>
00123 Service<T>::Service(const ValueList &params) : ServiceBase(NAME(), params, MANUAL()),
00124   _forked(false)
00125 {
00126   ++_instancesNumber;
00127 }
00128 
00129 template<class T>
00130 Service<T>::~Service()
00131 {
00132   --_instancesNumber;
00133   if (_forked)
00134     --_forkedInstancesNumber;
00135   if (_forkedInstancesNumber == 0)
00136     _params.clean();
00137 }
00138 
00139 #endif

Generated on Thu Sep 6 20:11:25 2007 for Pylon Application Platform by  doxygen 1.5.1