📄 app_config.h
字号:
#ifndef __GNU_APP_CONFIG_H__
#define __GNU_APP_CONFIG_H__
#include <vector>
#include <map>
#include <string>
#include <cstdio>
#include <ace/Recursive_Thread_Mutex.h>
#include <ace/Refcounted_Auto_Ptr.h>
#include <ace/Singleton.h>
#include <ace/Null_Mutex.h>
#include <ace/Configuration.h>
#include <ace/DLL.h>
namespace gnu
{
/**
* Type :
* 0 - string
* 1 - char
* 2 - short int
* 3 - int
* 4 - long
* 5 - float
* 6 - double
* 7 - date format of string
* 8 - directory format of string
* 9 - file format of string
*/
struct ConfigItem
{
std::string key;
std::string key_title;
std::string value;
std::string tip;
short int type;
};
bool ConfigItemSort (const ConfigItem& data1,const ConfigItem& data2) ;
typedef std::vector<ConfigItem> VectConfigItem;
typedef ACE_Refcounted_Auto_Ptr<VectConfigItem,ACE_Null_Mutex> VectConfigItemPtr;
typedef ACE_Refcounted_Auto_Ptr<ACE_Configuration_Heap,ACE_Null_Mutex> ACE_Configuration_HeapPtr;
class ModuleConfig
{
private:
std::string m_str_name;
std::string m_str_title;
std::string m_str_section;
std::map<std::string,ConfigItem> m_map_config_item;
void insert_into_map(const std::string& str_key,const std::string& str_value);
void insert_into_map(const ConfigItem& item);
bool insert_into_config_heap(const std::string& str_key,const std::string& str_value);
public:
ModuleConfig();
explicit ModuleConfig(const std::string& str_name);
virtual ~ModuleConfig();
bool get_all_items_from_config_heap();
std::string get_name() const;
void set_name(const std::string& str_name);
std::string get_title() const;
void set_title(const std::string& str_title);
std::string get_section() const;
void set_section(const std::string& str_section);
void refresh();
virtual bool get_item(const std::string& str_key,ConfigItem& item)const;
virtual void get_all_items(VectConfigItem& vect_item) const;
virtual bool set_item(const ConfigItem& item);
virtual bool set_items(const VectConfigItem& arr_data);
virtual bool set_items_title(const VectConfigItem& arr_data);
virtual bool get_value(const std::string& str_key,std::string& str_value);
virtual bool get_value(const std::string& str_key,int& i_value);
virtual bool get_value(const std::string& str_key,long& l_value);
virtual bool get_value(const std::string& str_key,float& f_value);
virtual bool get_value(const std::string& str_key,double& d_value);
virtual bool set_value(const std::string& str_key,const int& i_value);
virtual bool set_value(const std::string& str_key,const long& l_value);
virtual bool set_value(const std::string& str_key,const float& f_value);
virtual bool set_value(const std::string& str_key,const double& d_value);
virtual bool set_value(const std::string& str_key,const std::string& str_value);
virtual bool remove(const std::string& str_key);
virtual bool remove_all();
//debug info
void print_info() const;
};
typedef ACE_Refcounted_Auto_Ptr<ModuleConfig,ACE_Null_Mutex> ModuleConfigPtr;
class AppConfig
{
private:
std::string m_str_section;
std::string m_root_dir;
std::string m_conf_dir;
std::string m_str_conf_filename;
std::string conf_filename;
std::string conf_dir;
protected :
ACE_Configuration_HeapPtr m_config_heap_ptr;
friend bool ModuleConfig::get_all_items_from_config_heap();
public:
AppConfig();
~AppConfig();
std::string get_conf_file();
void set_conf_file(const std::string& str_file_name);
std::string get_root_dir() const;
std::string get_conf_dir();
void set_conf_dir(const std::string& str_dir);
bool get_value(const std::string& str_key,std::string& str_value);
bool get_value(const std::string& str_key,int& i_value);
bool get_value(const std::string& str_key,long& l_value);
bool get_value(const std::string& str_key,float& f_value);
bool get_value(const std::string& str_key,double& d_value);
bool get_value(const std::string& str_section,
const std::string& str_key,std::string& str_value);
bool get_value(const std::string& str_section,
const std::string& str_key,int& i_value);
bool get_value(const std::string& str_section,
const std::string& str_key,long& l_value);
bool get_value(const std::string& str_section,
const std::string& str_key,float& f_value);
bool get_value(const std::string& str_section,
const std::string& str_key,double& d_value);
bool get_value(const std::string& str_section,
const std::string& str_key,std::string& str_value,
const std::string& str_default_value);
bool get_value(const std::string& str_section,
const std::string& str_key,int& i_value,
const int i_default_value);
bool get_value(const std::string& str_section,
const std::string& str_key,long& l_value,
const long l_default_value);
bool get_value(const std::string& str_section,
const std::string& str_key,float& f_value,
const float f_default_value);
bool get_value(const std::string& str_section,
const std::string& str_key,double& d_value,
const double d_default_value);
bool set_value(const std::string& str_key,const int& i_value);
bool set_value(const std::string& str_key,const long& l_value);
bool set_value(const std::string& str_key,const float& f_value);
bool set_value(const std::string& str_key,const double& d_value);
bool set_value(const std::string& str_key,const std::string& str_value);
bool set_value(const std::string& str_section,
const std::string& str_key,const std::string& str_value);
bool set_value(const std::string& str_section,
const std::string& str_key,const int& i_value);
bool set_value(const std::string& str_section,
const std::string& str_key,const long& l_value);
bool set_value(const std::string& str_section,
const std::string& str_key,const float& f_value);
bool set_value(const std::string& str_section,
const std::string& str_key,const double& d_value);
void reload();
bool remove(const std::string& str_section,const std::string& str_key);
bool remove_section(const std::string& str_section);
};
typedef ACE_Singleton<AppConfig,ACE_Recursive_Thread_Mutex> AppConfigInst;
void copy_init_conf(const std::string& str_conf_filename_org,const std::string& str_conf_filename );
bool load_conf(ACE_Configuration_HeapPtr config_heap_ptr,const std::string& str_conf_filename);
bool save_conf(ACE_Configuration_HeapPtr config_heap_ptr,const std::string& str_conf_filename);
class Parameter
{
public:
virtual ~Parameter(){}
virtual unsigned int get_id() const = 0; //register ID
/**
* this function will be called by Parameter manger
*/
virtual void set_id(unsigned int id) = 0;
virtual const std::string get_name() const = 0;
virtual const std::string get_title() const= 0;
virtual const std::string get_version() const= 0;
// config info
virtual void init_config_parameter() = 0;
virtual void set_default_config_parameter() = 0;
virtual void reload_config_parameter() = 0;
virtual void print_config_parameter() = 0;
virtual gnu::ModuleConfigPtr get_config() const = 0;
};
typedef ACE_Refcounted_Auto_Ptr<ACE_DLL,ACE_Null_Mutex> ACE_DLL_Ptr ;
unsigned int get_parameter_uuid(const std::string& str_name);
} // namespace gnu
#endif // #ifndef __GNU_APP_CONFIG_H__
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -