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
Setting(const Setting &setting) : m_setting(setting.m_setting) {}
// --- lookup
bool get(const char* alias,bool& value) const;
bool get(const char* alias,int& value) const;
bool get(const char* alias,long& value) const;
bool get(const char* alias,long long& value) const;
bool get(const char* alias,double& value) const;
bool get(const char* alias,const char*& value) const;
bool get(const char* alias,std::string &value) const;
bool get(const char* alias,Data&) const;
bool get(const std::string& alias,bool& value) const;
bool get(const std::string& alias,int& value) const;
bool get(const std::string& alias,long& value) const;
bool get(const std::string& alias,long long& value) const;
bool get(const std::string& alias,double& value) const;
bool get(const std::string& alias,const char*& value) const;
bool get(const std::string& alias,std::string& value) const;
bool get(const std::string& alias,Data&) const;
// --- modifiers
void set(const char* alias,bool value);
void set(const char* alias,int value);
void set(const char* alias,long value);
void set(const char* alias,long long value);
void set(const char* alias,double value);
void set(const char* alias,const char* value);
void set(const char* alias,const std::string &value);
void set(const char* alias,const Data&);
void set(const std::string& alias,bool value);
void set(const std::string& alias,int value);
void set(const std::string& alias,long value);
void set(const std::string& alias,long long value);
void set(const std::string& alias,double value);
void set(const std::string& alias,const char* value);
void set(const std::string& alias,const std::string& value);
void set(const std::string& alias,const Data&);
// --- child
Setting addChild(const char* alias);
bool getChild(const char* alias,Setting& child) const;
Setting addChild(const std::string& alias);
bool getChild(const std::string& alias,Setting& child) const;
// --- list
Setting addList(const char* alias);
bool getList(const char* alias,Setting& list) const;
Setting addList(const std::string& alias);
bool getList(const std::string& alias,Setting& list) const;
// --- array
Setting addArray(const char* alias);
bool getArray(const char* alias,Setting& array) const;
Setting addArray(const std::string& alias);
bool getArray(const std::string& alias,Setting& array) const;
// --- list & array mgt
void append(bool value);
......@@ -80,43 +80,38 @@ namespace lima
void append(long value);
void append(long long 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,int& value) const;
void get(int pos,long& value) const;
void get(int pos,long long& 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,int value);
void set(int pos,long value);
void set(int pos,long long 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
Type getType() const;
int getLength() const;
const char* getName() const;
std::string getName() const;
bool isGroup() const;
operator bool() const;
operator int() const;
operator long long() const;
operator double() const;
operator const char*() const;
operator std::string() const;
Setting& operator=(bool value);
Setting& operator=(int value);
Setting& operator=(long long value);
Setting& operator=(double value);
Setting& operator=(const char* value);
Setting& operator=(const std::string& value);
const libconfig::Setting* get_raw_setting() const {return m_setting;}
......
......@@ -34,19 +34,19 @@ using namespace lima;
DEB_MEMBER_FUNCT(); \
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();
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();
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();
......@@ -56,35 +56,35 @@ bool Setting::get(const char* alias,long& value) const
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();
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();
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();
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();
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();
Setting data_setting;
......@@ -105,9 +105,9 @@ bool Setting::get(const char* alias,Data& data) const
i != header_group.end();++i)
{
const Setting& setting = *i;
const char* name = setting.getName();
const char* value = setting;
data.header.insert(name,value);
const std::string name = setting.getName();
const std::string value = setting;
data.header.insert(name.c_str(),value.c_str());
}
}
Setting buffer_setting;
......@@ -173,43 +173,41 @@ bool Setting::get(const char* alias,Data& data) const
nSetting = value; \
}
void Setting::set(const char* alias,bool value)
void Setting::set(const std::string& alias,bool value)
{
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);
}
void Setting::set(const char* alias,long value)
void Setting::set(const std::string& alias,long value)
{
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);
}
void Setting::set(const char* alias,double value)
void Setting::set(const std::string& alias,double value)
{
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);
}
void Setting::set(const char* alias,
const std::string &value)
void Setting::set(const std::string& alias,const std::string& value)
{
SET_VALUE(libconfig::Setting::TypeString);
}
void Setting::set(const char* alias,
void Setting::set(const std::string& alias,
const Data& data)
{
CHECK_NULL();
......@@ -269,7 +267,7 @@ void Setting::set(const char* alias,
CHECK_NULL(); \
try \
{ \
if(alias) \
if(!alias.empty()) \
{ \
libconfig::Setting &alias_setting = \
m_setting->add(alias,type); \
......@@ -303,34 +301,34 @@ void Setting::set(const char* alias,
} \
return returnFlag;
Setting Setting::addChild(const char *alias)
Setting Setting::addChild(const std::string& alias)
{
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());
}
// --- list management
Setting Setting::addList(const char* alias)
Setting Setting::addList(const std::string& alias)
{
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());
}
// --- array management
Setting Setting::addArray(const char* alias)
Setting Setting::addArray(const std::string& alias)
{
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());
}
......@@ -373,12 +371,7 @@ void Setting::append(double value)
APPEND(libconfig::Setting::TypeFloat);
}
void Setting::append(const char* value)
{
APPEND(libconfig::Setting::TypeString);
}
void Setting::append(const std::string &value)
void Setting::append(const std::string& value)
{
APPEND(libconfig::Setting::TypeString);
}
......@@ -420,11 +413,7 @@ void Setting::set(int pos,double value)
{
SET_VALUE_AT_POS();
}
void Setting::set(int pos,const char* value)
{
SET_VALUE_AT_POS();
}
void Setting::set(int pos,const std::string &value)
void Setting::set(int pos,const std::string& value)
{
SET_VALUE_AT_POS();
}
......@@ -498,7 +487,7 @@ int Setting::getLength() const
return m_setting->getLength();
}
const char* Setting::getName() const
std::string Setting::getName() const
{
CHECK_NULL();
......@@ -543,14 +532,9 @@ Setting::operator double() const
CAST_TYPE(double);
}
Setting::operator const char*() const
{
CAST_TYPE(const char*);
}
Setting::operator std::string() const
{
CAST_TYPE(const char*);
CAST_TYPE(const std::string&);
}
#define ASSIGN() \
......@@ -586,11 +570,6 @@ Setting& Setting::operator=(double value)
ASSIGN();
}
Setting& Setting::operator=(const char* value)
{
ASSIGN();
}
Setting& Setting::operator=(const std::string& value)
{
ASSIGN();
......
......@@ -39,37 +39,42 @@ namespace lima
{
DEB_CLASS_NAMESPC(DebModControl,"Config","Control");
public:
typedef const char* ModuleType;
static ModuleType All;
typedef std::string ModuleType;
typedef std::list<ModuleType> ModuleListType;
typedef std::string AliasType;
typedef std::list<AliasType> AliasListType;
static const ModuleType All;
CtConfig(CtControl &);
~CtConfig();
// --- set current config into a context alias
void store(const std::string& alias,
ModuleType);
void store(const std::string& alias,
const std::list<ModuleType>&);
void store(const AliasType& alias,
const ModuleType& module_to_save);
void store(const AliasType& alias,
const ModuleListType& modules_to_save);
// --- add current config to a context alias
void update(const std::string& alias,
ModuleType);
void update(const std::string& alias,
const std::list<ModuleType>&);
void update(const AliasType& alias,
const ModuleType& module_to_save);
void update(const AliasType& alias,
const ModuleListType& modules_to_save);
// --- get all context aliases
void getAlias(std::list<std::string>&) const;
void getAlias(AliasListType& aliases) const;
// --- get all register module type
void getAvailableModule(std::list<ModuleType>&) const;
void getAvailableModule(ModuleListType& modules) const;
// --- apply context to current parameters
void apply(const std::string&);
void pop(const std::string&);
void apply(const AliasType& alias);
void pop(const AliasType& alias);
// --- remove part/all context
void remove(const std::string&,ModuleType = All);
void remove(const std::string&,
const std::list<ModuleType>&);
void remove(const AliasType& alias,
const ModuleType& module_to_remove = All);
void remove(const AliasType& alias,
const ModuleListType& modules_to_remove);
// --- file management
void setFilename(const std::string&);
void getFilename(std::string&) const;
void setFilename(const std::string& full_path);
void getFilename(std::string& full_path) const;
void save();
void load();
......@@ -89,12 +94,12 @@ namespace lima
protected:
virtual ~ModuleTypeCallback();
private:
std::string m_module_type;
ModuleType m_module_type;
int m_ref_count;
};
void registerModule(ModuleTypeCallback*);
void unregisterModule(const std::string& module_type);
void unregisterModule(const ModuleType& module_type);
private:
typedef std::map<std::string,ModuleTypeCallback*> ModuleMap;
......@@ -103,7 +108,7 @@ namespace lima
CtControl& m_ctrl;
libconfig::Config* m_config;
std::string m_file_name;
ModuleMap m_module_type;
ModuleMap m_module_map;
};
}
#endif
......
......@@ -75,8 +75,8 @@ inline int _fill_setting_with_list(Setting &setting,
{
PyObject* value = PyList_GetItem(list,index);
if(PyString_Check(value))
setting.append(PyString_AS_STRING(value));
if(sipCheckConvertFromPyStr(value))
setting.append(sipConvertFromPyStr(value));
else if(PyFloat_Check(value))
setting.append(PyFloat_AS_DOUBLE(value));
else if(PyLong_Check(value))
......@@ -126,7 +126,7 @@ inline int _fill_setting_with_dict(Setting &setting,
{
while(PyDict_Next(dict, &pos, &key, &value))
{
if(!PyString_Check(key))
if(!sipCheckConvertFromPyStr(key))
{
PyErr_SetString(PyExc_ValueError,
"key can only by string type");
......@@ -134,32 +134,28 @@ inline int _fill_setting_with_dict(Setting &setting,
break;
}
if(PyString_Check(value))
setting.set(PyString_AS_STRING(key),
PyString_AS_STRING(value));
std::string key_str = sipConvertFromPyStr(key);
if(sipCheckConvertFromPyStr(value))
setting.set(key_str, sipConvertFromPyStr(value));
else if(PyFloat_Check(value))
setting.set(PyString_AS_STRING(key),
PyFloat_AS_DOUBLE(value));
setting.set(key_str, PyFloat_AS_DOUBLE(value));
else if(PyLong_Check(value))
{
long cValue = _from_python_long_2_c(value,return_value);
setting.set(PyString_AS_STRING(key),
cValue);
setting.set(key_str, cValue);
}
else if(PyInt_Check(value))
setting.set(PyString_AS_STRING(key),
int(PyInt_AsLong(value)));
setting.set(key_str, int(PyInt_AsLong(value)));
else if(PyBool_Check(value))
setting.set(PyString_AS_STRING(key),
PyObject_IsTrue(value));
setting.set(key_str, PyObject_IsTrue(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);
}
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);
}
else if(sipCanConvertToType(value,sipType_Data,SIP_NOT_NONE))
......@@ -183,7 +179,7 @@ inline int _fill_setting_with_dict(Setting &setting,
}
else
{
setting.set(PyString_AS_STRING(key),*data);
setting.set(key_str,*data);
sipReleaseType(data,sipType_Data,state);
}
}
......@@ -238,8 +234,8 @@ inline bool _build_dict_from_setting(const Setting &setting,
}
case Setting::String:
{
const char* value = sub_setting;
py_value = PyString_FromString(value);
std::string value = sub_setting;
py_value = sipConvertToPyStr(value);
break;
}
case Setting::Boolean:
......@@ -281,7 +277,7 @@ inline bool _build_dict_from_setting(const Setting &setting,
{
if(PyDict_Check(dict))
return_value = PyDict_SetItemString(dict,
sub_setting.getName(),
sub_setting.getName().c_str(),
py_value);
else
return_value = PyList_Append(dict,py_value);
......@@ -313,33 +309,34 @@ class CtConfig
using namespace lima;
%End
public:
static const char* All;
static const std::string All;
CtConfig(CtControl &);
~CtConfig();
// --- set current config into a context alias
void store(const std::string& alias,
const char*);
const std::string& module_to_save);
void store(const std::string& alias,
const std::list<const char*>&);
const std::list<std::string>& modules_to_save);
// --- add current config to a context alias
void update(const std::string& alias,
const char*);
const std::string& module_to_save);
void update(const std::string& alias,
const std::list<const char*>&);
const std::list<std::string>& modules_to_save);
void getAlias(std::list<std::string>& /Out/) const;
void getAvailableModule(std::list<const char*>& /Out/) const;
void getAvailableModule(std::list<std::string>& /Out/) const;
void apply(const std::string&);
void pop(const std::string&);
void remove(const std::string&,const char* = CtConfig::All);
void remove(const std::string&,
const std::list<const char*>&);
void apply(const std::string& alias);
void pop(const std::string& alias);
void remove(const std::string& alias,
const std::string& module_to_remove = CtConfig::All);
void remove(const std::string& alias,
const std::list<std::string>& modules_to_remove);
void setFilename(const std::string&);
void getFilename(std::string& /Out/) const;
void setFilename(const std::string& full_path);
void getFilename(std::string& full_path /Out/) const;
void save();
void load();
......@@ -353,7 +350,7 @@ using namespace lima;
public:
explicit ModuleTypeCallback(const char*);
explicit ModuleTypeCallback(std::string);
virtual SIP_PYDICT store() = 0 [void (Setting&)];
%MethodCode
......
This diff is collapsed.
......@@ -42,11 +42,11 @@
using namespace lima;
const char* CtConfig::All = "All";
const CtConfig::ModuleType CtConfig::All = "All";
//Static function
static void _remove_if_exists(libconfig::Setting &setting,
const std::string& alias)
const CtConfig::AliasType& alias)
{
if(setting.exists(alias))
setting.remove(alias);
......@@ -91,8 +91,8 @@ CtConfig::~CtConfig()
}
delete m_config;
for(ModuleMap::iterator i = m_module_type.begin();
i != m_module_type.end();++i)
for(ModuleMap::iterator i = m_module_map.begin();
i != m_module_map.end();++i)
i->second->unref();
}
/** @brief set the full path for the filename used for saving configuration
......@@ -110,17 +110,17 @@ void CtConfig::getFilename(std::string &full_path) const
full_path = m_file_name;
}
void CtConfig::store(const std::string& alias,
ModuleType module_to_save)
void CtConfig::store(const AliasType& alias,
const ModuleType& module_to_save)
{
std::list<ModuleType> modules_to_save;
ModuleListType modules_to_save;
modules_to_save.push_back(module_to_save);
store(alias,modules_to_save);
}
void CtConfig::store(const std::string& alias,
const std::list<ModuleType>& modules_to_save)
void CtConfig::store(const AliasType& alias,
const ModuleListType& modules_to_save)
{
DEB_MEMBER_FUNCT();
......@@ -132,14 +132,14 @@ void CtConfig::store(const std::string& alias,
root.add(alias,libconfig::Setting::TypeGroup);
bool anAllFlag = false;
for(std::list<ModuleType>::const_iterator i = modules_to_save.begin();
for(ModuleListType::const_iterator i = modules_to_save.begin();
i != modules_to_save.end() && !anAllFlag;++i)
anAllFlag = *i == All;
if(anAllFlag)
{
for(ModuleMap::iterator module = m_module_type.begin();
module != m_module_type.end();++module)
for(ModuleMap::iterator module = m_module_map.begin();
module != m_module_map.end();++module)
{
libconfig::Setting &setting = alias_setting.add(module->first,
libconfig::Setting::TypeGroup);
......@@ -149,11 +149,11 @@ void CtConfig::store(const std::string& alias,
}
else
{
for(std::list<ModuleType>::const_iterator i = modules_to_save.begin();
for(ModuleListType::const_iterator i = modules_to_save.begin();
i != modules_to_save.end();++i)
{
ModuleMap::iterator module = m_module_type.find(*i);
if(module != m_module_type.end())
ModuleMap::iterator module = m_module_map.find(*i);
if(module != m_module_map.end())
{
libconfig::Setting &setting = alias_setting.add(module->first,
libconfig::Setting::TypeGroup);
......@@ -179,23 +179,23 @@ void CtConfig::store(const std::string& alias,
}
}
void CtConfig::update(const std::string& alias,
ModuleType module_to_save)
void CtConfig::update(const AliasType& alias,
const ModuleType& module_to_save)
{
std::list<ModuleType> modules_to_save;
ModuleListType modules_to_save;
modules_to_save.push_back(module_to_save);
update(alias,modules_to_save);
}
void CtConfig::update(const std::string& alias,
const std::list<ModuleType>& modules_to_save)
void CtConfig::update(const AliasType& alias,
const ModuleListType& modules_to_save)
{
DEB_MEMBER_FUNCT();
libconfig::Setting& root = m_config->getRoot();
bool anAllFlag = false;
for(std::list<ModuleType>::const_iterator i = modules_to_save.begin();
for(ModuleListType::const_iterator i = modules_to_save.begin();
i != modules_to_save.end() && !anAllFlag;++i)
anAllFlag = *i == All;
......@@ -203,11 +203,11 @@ void CtConfig::update(const std::string& alias,
{
libconfig::Setting& alias_setting = root[alias];
for(std::list<ModuleType>::const_iterator i = modules_to_save.begin();
for(ModuleListType::const_iterator i = modules_to_save.begin();
i != modules_to_save.end();++i)
{
ModuleMap::iterator module = m_module_type.find(*i);
if(module != m_module_type.end())
ModuleMap::iterator module = m_module_map.find(*i);
if(module != m_module_map.end())
{
_remove_if_exists(alias_setting,*i);
try
......@@ -237,7 +237,7 @@ void CtConfig::update(const std::string& alias,
store(alias,modules_to_save);
}
void CtConfig::getAlias(std::list<std::string>& aliases) const
void CtConfig::getAlias(AliasListType& aliases) const
{
libconfig::Setting& root = m_config->getRoot();
int nbAlias = root.getLength();
......@@ -249,14 +249,14 @@ void CtConfig::update(const std::string& alias,
}
}
void CtConfig::getAvailableModule