init
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
#ifndef __UT_SERVICE_APPLICATION_HPP__
|
||||
#define __UT_SERVICE_APPLICATION_HPP__
|
||||
|
||||
#include <unitree/common/service/base/service_base.hpp>
|
||||
|
||||
#define SERVICE_REGISTER(SERVICE) \
|
||||
class SERVICE##Register \
|
||||
{ \
|
||||
public: \
|
||||
SERVICE##Register() \
|
||||
{ \
|
||||
unitree::common::ServiceApplication::Instance()->RegistService<SERVICE>(); \
|
||||
} \
|
||||
static SERVICE##Register mRegister; \
|
||||
}; \
|
||||
SERVICE##Register SERVICE##Register::mRegister = SERVICE##Register();
|
||||
|
||||
namespace unitree
|
||||
{
|
||||
namespace common
|
||||
{
|
||||
class ServiceApplication
|
||||
{
|
||||
public:
|
||||
static ServiceApplication* Instance()
|
||||
{
|
||||
static ServiceApplication inst;
|
||||
return &inst;
|
||||
}
|
||||
|
||||
template<typename SERVICE>
|
||||
void RegistService()
|
||||
{
|
||||
mServicePtr = ServicePtr(new SERVICE());
|
||||
}
|
||||
|
||||
void Init(const std::string& configFileName);
|
||||
void Start();
|
||||
void Stop();
|
||||
|
||||
private:
|
||||
ServiceApplication();
|
||||
|
||||
protected:
|
||||
ServicePtr mServicePtr;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif//__UT_SERVICE_APPLICATION_HPP__
|
104
unitree_SDK/include/unitree/common/service/base/service_base.hpp
Normal file
104
unitree_SDK/include/unitree/common/service/base/service_base.hpp
Normal file
@@ -0,0 +1,104 @@
|
||||
#ifndef __UT_SERVICE_BASE_HPP__
|
||||
#define __UT_SERVICE_BASE_HPP__
|
||||
|
||||
#include <unitree/common/service/base/service_config.hpp>
|
||||
|
||||
namespace unitree
|
||||
{
|
||||
namespace common
|
||||
{
|
||||
class ServiceBase
|
||||
{
|
||||
public:
|
||||
ServiceBase();
|
||||
virtual ~ServiceBase();
|
||||
|
||||
virtual void Parse(const std::string& configFileName);
|
||||
|
||||
virtual void Init();
|
||||
|
||||
virtual void Register();
|
||||
|
||||
virtual void Start();
|
||||
|
||||
virtual void Wait();
|
||||
|
||||
virtual void Stop();
|
||||
|
||||
protected:
|
||||
/*
|
||||
* Parse config content
|
||||
*/
|
||||
virtual void ParseConfigContent(const std::string& content);
|
||||
|
||||
/*
|
||||
* Get field: ServiceName
|
||||
*/
|
||||
const std::string& GetServiceName() const;
|
||||
|
||||
/*
|
||||
* Get field: CpuIds
|
||||
*/
|
||||
const std::string& GetCpuIds() const;
|
||||
|
||||
/*
|
||||
* Get top-level field/value
|
||||
*/
|
||||
const Any& GetGlobalParameter(const std::string& name) const;
|
||||
|
||||
/*
|
||||
* Get top-level field: Parameter
|
||||
*/
|
||||
bool HasParameter(const std::string& name) const;
|
||||
|
||||
/*
|
||||
* Get top-level field: Parameter
|
||||
*/
|
||||
const JsonMap& GetParameter() const;
|
||||
|
||||
/*
|
||||
* Get field from top-level field: Parameter
|
||||
*/
|
||||
const Any& GetParameter(const std::string& name) const;
|
||||
|
||||
/*
|
||||
* Get field from top-level field: Parameter
|
||||
*/
|
||||
template<typename T>
|
||||
const T& GetParameter(const std::string& name) const
|
||||
{
|
||||
return mConfig.GetParameter<T>(name);
|
||||
}
|
||||
|
||||
/*
|
||||
* Get field from top-level field: Parameter
|
||||
*/
|
||||
template<typename T>
|
||||
T GetNumberParameter(const std::string& name) const
|
||||
{
|
||||
return mConfig.GetNumberParameter<T>(name);
|
||||
}
|
||||
|
||||
/*
|
||||
* Get field from top-level field: Parameter
|
||||
*/
|
||||
template<typename T>
|
||||
T GetParameter(const std::string& name, const T& defValue) const
|
||||
{
|
||||
return mConfig.GetParameter<T>(name, defValue);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T GetNumberParameter(const std::string& name, const T& defValue) const
|
||||
{
|
||||
return mConfig.GetNumberParameter<T>(name, defValue);
|
||||
}
|
||||
|
||||
private:
|
||||
ServiceConfig mConfig;
|
||||
};
|
||||
typedef std::shared_ptr<ServiceBase> ServicePtr;
|
||||
|
||||
}
|
||||
}
|
||||
#endif//__UT_SERVICE_BASE_HPP__
|
@@ -0,0 +1,37 @@
|
||||
#ifndef __UT_SERVICE_CONFIG_HPP__
|
||||
#define __UT_SERVICE_CONFIG_HPP__
|
||||
|
||||
#include <unitree/common/service/base/service_decl.hpp>
|
||||
|
||||
namespace unitree
|
||||
{
|
||||
namespace common
|
||||
{
|
||||
class ServiceConfig : public common::JsonConfig
|
||||
{
|
||||
public:
|
||||
ServiceConfig();
|
||||
virtual ~ServiceConfig();
|
||||
|
||||
ServiceConfig(const std::string& configFileName);
|
||||
|
||||
virtual void Parse(const std::string& configFileName);
|
||||
virtual void ParseContent(const std::string& content);
|
||||
|
||||
//top-level field: ServiceName
|
||||
const std::string& GetServiceName() const;
|
||||
|
||||
//top-level field: CpuIds
|
||||
const std::string& GetCpuIds() const;
|
||||
|
||||
protected:
|
||||
std::string mCpuIds;
|
||||
std::string mServiceName;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<ServiceConfig> ServiceConfigPtr;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif//__UT_SERVICE_CONFIG_HPP__
|
@@ -0,0 +1,10 @@
|
||||
#ifndef __UT_SERVICE_DECL_HPP__
|
||||
#define __UT_SERVICE_DECL_HPP__
|
||||
|
||||
#include <unitree/common/thread/thread.hpp>
|
||||
#include <unitree/common/time/time_tool.hpp>
|
||||
#include <unitree/common/json/json.hpp>
|
||||
#include <unitree/common/json/json_config.hpp>
|
||||
#include <unitree/common/log/log.hpp>
|
||||
|
||||
#endif//__UT_SERVICE_DECL_HPP__
|
Reference in New Issue
Block a user