Commit dd69c7e7 authored by Laurent Claustre's avatar Laurent Claustre

Merge branch 'fix_sip_str' into 'master'

SIP: fix PyString/Unicode vs. std::string conversion

See merge request !91
parents f9043a36 918b6cf8
Pipeline #7217 passed with stages
in 10 minutes and 44 seconds
...@@ -43,36 +43,36 @@ namespace lima ...@@ -43,36 +43,36 @@ namespace lima
Setting(const Setting &setting) : m_setting(setting.m_setting) {} Setting(const Setting &setting) : m_setting(setting.m_setting) {}
// --- lookup // --- lookup
bool get(const char* alias,bool& value) const; bool get(const std::string& alias,bool& value) const;
bool get(const char* alias,int& value) const; bool get(const std::string& alias,int& value) const;
bool get(const char* alias,long& value) const; bool get(const std::string& alias,long& value) const;
bool get(const char* alias,long long& value) const; bool get(const std::string& alias,long long& value) const;
bool get(const char* alias,double& value) const; bool get(const std::string& alias,double& value) const;
bool get(const char* alias,const char*& value) const; bool get(const std::string& alias,const char*& value) const;
bool get(const char* alias,std::string &value) const; bool get(const std::string& alias,std::string& value) const;
bool get(const char* alias,Data&) const; bool get(const std::string& alias,Data&) const;
// --- modifiers // --- modifiers
void set(const char* alias,bool value); void set(const std::string& alias,bool value);
void set(const char* alias,int value); void set(const std::string& alias,int value);
void set(const char* alias,long value); void set(const std::string& alias,long value);
void set(const char* alias,long long value); void set(const std::string& alias,long long value);
void set(const char* alias,double value); void set(const std::string& alias,double value);
void set(const char* alias,const char* value); void set(const std::string& alias,const char* value);
void set(const char* alias,const std::string &value); void set(const std::string& alias,const std::string& value);
void set(const char* alias,const Data&); void set(const std::string& alias,const Data&);
// --- child // --- child
Setting addChild(const char* alias); Setting addChild(const std::string& alias);
bool getChild(const char* alias,Setting& child) const; bool getChild(const std::string& alias,Setting& child) const;
// --- list // --- list
Setting addList(const char* alias); Setting addList(const std::string& alias);
bool getList(const char* alias,Setting& list) const; bool getList(const std::string& alias,Setting& list) const;
// --- array // --- array
Setting addArray(const char* alias); Setting addArray(const std::string& alias);
bool getArray(const char* alias,Setting& array) const; bool getArray(const std::string& alias,Setting& array) const;
// --- list & array mgt // --- list & array mgt
void append(bool value); void append(bool value);
...@@ -80,43 +80,38 @@ namespace lima ...@@ -80,43 +80,38 @@ namespace lima
void append(long value); void append(long value);
void append(long long value); void append(long long value);
void append(double value); void append(double value);
void append(const char* value); void append(const std::string& value);
void append(const std::string &value);
void get(int pos,bool& value) const; void get(int pos,bool& value) const;
void get(int pos,int& value) const; void get(int pos,int& value) const;
void get(int pos,long& value) const; void get(int pos,long& value) const;
void get(int pos,long long& value) const; void get(int pos,long long& value) const;
void get(int pos,double& value) const; void get(int pos,double& value) const;
void get(int pos,const char*& value) const; void get(int pos,std::string& value) const;
void get(int pos,std::string &value) const;
void set(int pos,bool value); void set(int pos,bool value);
void set(int pos,int value); void set(int pos,int value);
void set(int pos,long value); void set(int pos,long value);
void set(int pos,long long value); void set(int pos,long long value);
void set(int pos,double value); void set(int pos,double value);
void set(int pos,const char* value); void set(int pos,const std::string& value);
void set(int pos,const std::string &value);
// --- settings // --- settings
Type getType() const; Type getType() const;
int getLength() const; int getLength() const;
const char* getName() const; std::string getName() const;
bool isGroup() const; bool isGroup() const;
operator bool() const; operator bool() const;
operator int() const; operator int() const;
operator long long() const; operator long long() const;
operator double() const; operator double() const;
operator const char*() const;
operator std::string() const; operator std::string() const;
Setting& operator=(bool value); Setting& operator=(bool value);
Setting& operator=(int value); Setting& operator=(int value);
Setting& operator=(long long value); Setting& operator=(long long value);
Setting& operator=(double value); Setting& operator=(double value);
Setting& operator=(const char* value);
Setting& operator=(const std::string& value); Setting& operator=(const std::string& value);
const libconfig::Setting* get_raw_setting() const {return m_setting;} const libconfig::Setting* get_raw_setting() const {return m_setting;}
......
...@@ -34,19 +34,19 @@ using namespace lima; ...@@ -34,19 +34,19 @@ using namespace lima;
DEB_MEMBER_FUNCT(); \ DEB_MEMBER_FUNCT(); \
if(!m_setting) THROW_COM_ERROR(Error) << "Setting is Null"; if(!m_setting) THROW_COM_ERROR(Error) << "Setting is Null";
bool Setting::get(const char* alias,bool& value) const bool Setting::get(const std::string& alias,bool& value) const
{ {
CHECK_NULL(); CHECK_NULL();
return m_setting->lookupValue(alias,value); return m_setting->lookupValue(alias,value);
} }
bool Setting::get(const char* alias,int& value) const bool Setting::get(const std::string& alias,int& value) const
{ {
CHECK_NULL(); CHECK_NULL();
return m_setting->lookupValue(alias,value); return m_setting->lookupValue(alias,value);
} }
bool Setting::get(const char* alias,long& value) const bool Setting::get(const std::string& alias,long& value) const
{ {
CHECK_NULL(); CHECK_NULL();
...@@ -56,35 +56,35 @@ bool Setting::get(const char* alias,long& value) const ...@@ -56,35 +56,35 @@ bool Setting::get(const char* alias,long& value) const
return rFlag; return rFlag;
} }
bool Setting::get(const char* alias,long long& value) const bool Setting::get(const std::string& alias,long long& value) const
{ {
CHECK_NULL(); CHECK_NULL();
return m_setting->lookupValue(alias,value); return m_setting->lookupValue(alias,value);
} }
bool Setting::get(const char* alias,double& value) const bool Setting::get(const std::string& alias,double& value) const
{ {
CHECK_NULL(); CHECK_NULL();
return m_setting->lookupValue(alias,value); return m_setting->lookupValue(alias,value);
} }
bool Setting::get(const char* alias,const char*& value) const bool Setting::get(const std::string& alias,const char*& value) const
{ {
CHECK_NULL(); CHECK_NULL();
return m_setting->lookupValue(alias,value); return m_setting->lookupValue(alias,value);
} }
bool Setting::get(const char* alias,std::string& value) const bool Setting::get(const std::string& alias,std::string& value) const
{ {
CHECK_NULL(); CHECK_NULL();
return m_setting->lookupValue(alias,value); return m_setting->lookupValue(alias,value);
} }
bool Setting::get(const char* alias,Data& data) const bool Setting::get(const std::string& alias,Data& data) const
{ {
CHECK_NULL(); CHECK_NULL();
Setting data_setting; Setting data_setting;
...@@ -105,9 +105,9 @@ bool Setting::get(const char* alias,Data& data) const ...@@ -105,9 +105,9 @@ bool Setting::get(const char* alias,Data& data) const
i != header_group.end();++i) i != header_group.end();++i)
{ {
const Setting& setting = *i; const Setting& setting = *i;
const char* name = setting.getName(); const std::string name = setting.getName();
const char* value = setting; const std::string value = setting;
data.header.insert(name,value); data.header.insert(name.c_str(),value.c_str());
} }
} }
Setting buffer_setting; Setting buffer_setting;
...@@ -173,43 +173,41 @@ bool Setting::get(const char* alias,Data& data) const ...@@ -173,43 +173,41 @@ bool Setting::get(const char* alias,Data& data) const
nSetting = value; \ nSetting = value; \
} }
void Setting::set(const char* alias,bool value) void Setting::set(const std::string& alias,bool value)
{ {
SET_VALUE(libconfig::Setting::TypeBoolean); SET_VALUE(libconfig::Setting::TypeBoolean);
} }
void Setting::set(const char* alias,int value) void Setting::set(const std::string& alias,int value)
{ {
SET_VALUE(libconfig::Setting::TypeInt); SET_VALUE(libconfig::Setting::TypeInt);
} }
void Setting::set(const char* alias,long value) void Setting::set(const std::string& alias,long value)
{ {
SET_VALUE(libconfig::Setting::TypeInt64); SET_VALUE(libconfig::Setting::TypeInt64);
} }
void Setting::set(const char* alias,long long value) void Setting::set(const std::string& alias,long long value)
{ {
SET_VALUE(libconfig::Setting::TypeInt64); SET_VALUE(libconfig::Setting::TypeInt64);
} }
void Setting::set(const char* alias,double value) void Setting::set(const std::string& alias,double value)
{ {
SET_VALUE(libconfig::Setting::TypeFloat); SET_VALUE(libconfig::Setting::TypeFloat);
} }
void Setting::set(const char* alias,const char* value) void Setting::set(const std::string& alias,const char* value)
{ {
SET_VALUE(libconfig::Setting::TypeString); SET_VALUE(libconfig::Setting::TypeString);
} }
void Setting::set(const std::string& alias,const std::string& value)
void Setting::set(const char* alias,
const std::string &value)
{ {
SET_VALUE(libconfig::Setting::TypeString); SET_VALUE(libconfig::Setting::TypeString);
} }
void Setting::set(const char* alias, void Setting::set(const std::string& alias,
const Data& data) const Data& data)
{ {
CHECK_NULL(); CHECK_NULL();
...@@ -269,7 +267,7 @@ void Setting::set(const char* alias, ...@@ -269,7 +267,7 @@ void Setting::set(const char* alias,
CHECK_NULL(); \ CHECK_NULL(); \
try \ try \
{ \ { \
if(alias) \ if(!alias.empty()) \
{ \ { \
libconfig::Setting &alias_setting = \ libconfig::Setting &alias_setting = \
m_setting->add(alias,type); \ m_setting->add(alias,type); \
...@@ -303,34 +301,34 @@ void Setting::set(const char* alias, ...@@ -303,34 +301,34 @@ void Setting::set(const char* alias,
} \ } \
return returnFlag; return returnFlag;
Setting Setting::addChild(const char *alias) Setting Setting::addChild(const std::string& alias)
{ {
ADD_NESTED_TYPE(libconfig::Setting::TypeGroup); ADD_NESTED_TYPE(libconfig::Setting::TypeGroup);
} }
bool Setting::getChild(const char* alias,Setting& child) const bool Setting::getChild(const std::string& alias,Setting& child) const
{ {
GET_NESTED_TYPE(alias_setting.isGroup()); GET_NESTED_TYPE(alias_setting.isGroup());
} }
// --- list management // --- list management
Setting Setting::addList(const char* alias) Setting Setting::addList(const std::string& alias)
{ {
ADD_NESTED_TYPE(libconfig::Setting::TypeList); ADD_NESTED_TYPE(libconfig::Setting::TypeList);
} }
bool Setting::getList(const char* alias,Setting& child) const bool Setting::getList(const std::string& alias,Setting& child) const
{ {
GET_NESTED_TYPE(alias_setting.isList()); GET_NESTED_TYPE(alias_setting.isList());
} }
// --- array management // --- array management
Setting Setting::addArray(const char* alias) Setting Setting::addArray(const std::string& alias)
{ {
ADD_NESTED_TYPE(libconfig::Setting::TypeArray); ADD_NESTED_TYPE(libconfig::Setting::TypeArray);
} }
bool Setting::getArray(const char* alias,Setting& child) const bool Setting::getArray(const std::string& alias,Setting& child) const
{ {
GET_NESTED_TYPE(alias_setting.isArray()); GET_NESTED_TYPE(alias_setting.isArray());
} }
...@@ -373,12 +371,7 @@ void Setting::append(double value) ...@@ -373,12 +371,7 @@ void Setting::append(double value)
APPEND(libconfig::Setting::TypeFloat); APPEND(libconfig::Setting::TypeFloat);
} }
void Setting::append(const char* value) void Setting::append(const std::string& value)
{
APPEND(libconfig::Setting::TypeString);
}
void Setting::append(const std::string &value)
{ {
APPEND(libconfig::Setting::TypeString); APPEND(libconfig::Setting::TypeString);
} }
...@@ -420,11 +413,7 @@ void Setting::set(int pos,double value) ...@@ -420,11 +413,7 @@ void Setting::set(int pos,double value)
{ {
SET_VALUE_AT_POS(); SET_VALUE_AT_POS();
} }
void Setting::set(int pos,const char* value) void Setting::set(int pos,const std::string& value)
{
SET_VALUE_AT_POS();
}
void Setting::set(int pos,const std::string &value)
{ {
SET_VALUE_AT_POS(); SET_VALUE_AT_POS();
} }
...@@ -498,7 +487,7 @@ int Setting::getLength() const ...@@ -498,7 +487,7 @@ int Setting::getLength() const
return m_setting->getLength(); return m_setting->getLength();
} }
const char* Setting::getName() const std::string Setting::getName() const
{ {
CHECK_NULL(); CHECK_NULL();
...@@ -543,14 +532,9 @@ Setting::operator double() const ...@@ -543,14 +532,9 @@ Setting::operator double() const
CAST_TYPE(double); CAST_TYPE(double);
} }
Setting::operator const char*() const
{
CAST_TYPE(const char*);
}
Setting::operator std::string() const Setting::operator std::string() const
{ {
CAST_TYPE(const char*); CAST_TYPE(const std::string&);
} }
#define ASSIGN() \ #define ASSIGN() \
...@@ -586,11 +570,6 @@ Setting& Setting::operator=(double value) ...@@ -586,11 +570,6 @@ Setting& Setting::operator=(double value)
ASSIGN(); ASSIGN();
} }
Setting& Setting::operator=(const char* value)
{
ASSIGN();
}
Setting& Setting::operator=(const std::string& value) Setting& Setting::operator=(const std::string& value)
{ {
ASSIGN(); ASSIGN();
......
...@@ -39,37 +39,42 @@ namespace lima ...@@ -39,37 +39,42 @@ namespace lima
{ {
DEB_CLASS_NAMESPC(DebModControl,"Config","Control"); DEB_CLASS_NAMESPC(DebModControl,"Config","Control");
public: public:
typedef const char* ModuleType; typedef std::string ModuleType;
static ModuleType All; typedef std::list<ModuleType> ModuleListType;
typedef std::string AliasType;
typedef std::list<AliasType> AliasListType;
static const ModuleType All;
CtConfig(CtControl &); CtConfig(CtControl &);
~CtConfig(); ~CtConfig();
// --- set current config into a context alias // --- set current config into a context alias
void store(const std::string& alias, void store(const AliasType& alias,
ModuleType); const ModuleType& module_to_save);
void store(const std::string& alias, void store(const AliasType& alias,
const std::list<ModuleType>&); const ModuleListType& modules_to_save);
// --- add current config to a context alias // --- add current config to a context alias
void update(const std::string& alias, void update(const AliasType& alias,
ModuleType); const ModuleType& module_to_save);
void update(const std::string& alias, void update(const AliasType& alias,
const std::list<ModuleType>&); const ModuleListType& modules_to_save);
// --- get all context aliases // --- get all context aliases
void getAlias(std::list<std::string>&) const; void getAlias(AliasListType& aliases) const;
// --- get all register module type // --- get all register module type
void getAvailableModule(std::list<ModuleType>&) const; void getAvailableModule(ModuleListType& modules) const;
// --- apply context to current parameters // --- apply context to current parameters
void apply(const std::string&); void apply(const AliasType& alias);
void pop(const std::string&); void pop(const AliasType& alias);
// --- remove part/all context // --- remove part/all context
void remove(const std::string&,ModuleType = All); void remove(const AliasType& alias,
void remove(const std::string&, const ModuleType& module_to_remove = All);
const std::list<ModuleType>&); void remove(const AliasType& alias,
const ModuleListType& modules_to_remove);
// --- file management // --- file management
void setFilename(const std::string&); void setFilename(const std::string& full_path);
void getFilename(std::string&) const; void getFilename(std::string& full_path) const;
void save(); void save();
void load(); void load();
...@@ -89,12 +94,12 @@ namespace lima ...@@ -89,12 +94,12 @@ namespace lima
protected: protected:
virtual ~ModuleTypeCallback(); virtual ~ModuleTypeCallback();
private: private:
std::string m_module_type; ModuleType m_module_type;
int m_ref_count; int m_ref_count;
}; };
void registerModule(ModuleTypeCallback*); void registerModule(ModuleTypeCallback*);
void unregisterModule(const std::string& module_type); void unregisterModule(const ModuleType& module_type);
private: private:
typedef std::map<std::string,ModuleTypeCallback*> ModuleMap; typedef std::map<std::string,ModuleTypeCallback*> ModuleMap;
...@@ -103,7 +108,7 @@ namespace lima ...@@ -103,7 +108,7 @@ namespace lima
CtControl& m_ctrl; CtControl& m_ctrl;
libconfig::Config* m_config; libconfig::Config* m_config;
std::string m_file_name; std::string m_file_name;
ModuleMap m_module_type; ModuleMap m_module_map;
}; };
} }
#endif #endif
......
...@@ -75,8 +75,8 @@ inline int _fill_setting_with_list(Setting &setting, ...@@ -75,8 +75,8 @@ inline int _fill_setting_with_list(Setting &setting,
{ {
PyObject* value = PyList_GetItem(list,index); PyObject* value = PyList_GetItem(list,index);
if(PyString_Check(value)) if(sipCheckConvertFromPyStr(value))
setting.append(PyString_AS_STRING(value)); setting.append(sipConvertFromPyStr(value));
else if(PyFloat_Check(value)) else if(PyFloat_Check(value))
setting.append(PyFloat_AS_DOUBLE(value)); setting.append(PyFloat_AS_DOUBLE(value));
else if(PyLong_Check(value)) else if(PyLong_Check(value))
...@@ -126,7 +126,7 @@ inline int _fill_setting_with_dict(Setting &setting, ...@@ -126,7 +126,7 @@ inline int _fill_setting_with_dict(Setting &setting,
{ {
while(PyDict_Next(dict, &pos, &key, &value)) while(PyDict_Next(dict, &pos, &key, &value))
{ {
if(!PyString_Check(key)) if(!sipCheckConvertFromPyStr(key))
{ {
PyErr_SetString(PyExc_ValueError, PyErr_SetString(PyExc_ValueError,
"key can only by string type"); "key can only by string type");
...@@ -134,32 +134,28 @@ inline int _fill_setting_with_dict(Setting &setting, ...@@ -134,32 +134,28 @@ inline int _fill_setting_with_dict(Setting &setting,
break; break;
} }
if(PyString_Check(value)) std::string key_str = sipConvertFromPyStr(key);
setting.set(PyString_AS_STRING(key), if(sipCheckConvertFromPyStr(value))
PyString_AS_STRING(value)); setting.set(key_str, sipConvertFromPyStr(value));
else if(PyFloat_Check(value)) else if(PyFloat_Check(value))
setting.set(PyString_AS_STRING(key), setting.set(key_str, PyFloat_AS_DOUBLE(value));
PyFloat_AS_DOUBLE(value));
else if(PyLong_Check(value)) else if(PyLong_Check(value))
{ {
long cValue = _from_python_long_2_c(value,return_value); long cValue = _from_python_long_2_c(value,return_value);
setting.set(PyString_AS_STRING(key), setting.set(key_str, cValue);
cValue);
} }
else if(PyInt_Check(value)) else if(PyInt_Check(value))
setting.set(PyString_AS_STRING(key), setting.set(key_str, int(PyInt_AsLong(value)));
int(PyInt_AsLong(value)));
else if(PyBool_Check(value)) else if(PyBool_Check(value))
setting.set(PyString_AS_STRING(key), setting.set(key_str, PyObject_IsTrue(value));
PyObject_IsTrue(value));
else if(PyDict_Check(value)) else if(PyDict_Check(value))
{ {
Setting child = setting.addChild(PyString_AS_STRING(key)); Setting child = setting.addChild(key_str);
return_value = _fill_setting_with_dict(child,value); return_value = _fill_setting_with_dict(child,value);
} }
else if(PyList_Check(value)) else if(PyList_Check(value))
{ {
Setting child = setting.addList(PyString_AS_STRING(key)); Setting child = setting.addList(key_str);
return_value = _fill_setting_with_list(child,value); return_value = _fill_setting_with_list(child,value);
} }
else if(sipCanConvertToType(value,sipType_Data,SIP_NOT_NONE)) else if(sipCanConvertToType(value,sipType_Data,SIP_NOT_NONE))
...@@ -183,7 +179,7 @@ inline int _fill_setting_with_dict(Setting &setting, ...@@ -183,7 +179,7 @@ inline int _fill_setting_with_dict(Setting &setting,
} }
else else
{ {
setting.set(PyString_AS_STRING(key),*data); setting.set(key_str,*data);
sipReleaseType(data,sipType_Data,state); sipReleaseType(data,sipType_Data,state);
} }
} }
...@@ -238,8 +234,8 @@ inline bool _build_dict_from_setting(const Setting &setting, ...@@ -238,8 +234,8 @@ inline bool _build_dict_from_setting(const Setting &setting,
} }
case Setting::String: case Setting::String:
{ {
const char* value = sub_setting; std::string value = sub_setting;
py_value = PyString_FromString(value); py_value = sipConvertToPyStr(value);
break; break;
} }
case Setting::Boolean: case Setting::Boolean:
...@@ -281,7 +277,7 @@ inline bool _build_dict_from_setting(const Setting &setting, ...@@ -281,7 +277,7 @@ inline bool _build_dict_from_setting(const Setting &setting,
{ {
if(PyDict_Check(dict)) if(PyDict_Check(dict))
return_value = PyDict_SetItemString(dict, return_value = PyDict_SetItemString(dict,