Rule & ARule constructors do not need 'Name valueStr' anymore.

It is automatically generated based on 'double value' argument
    Python binding is up to date
This commit is contained in:
Damien Dupuis 2010-06-15 11:29:15 +00:00
parent c3a210e4a5
commit ba80733d5e
4 changed files with 48 additions and 52 deletions

View File

@ -12,6 +12,13 @@ void translator(DTRException const& e) {
PyErr_SetString(PyExc_UserWarning, e.what()); PyErr_SetString(PyExc_UserWarning, e.what());
} }
// specify that Techno::getValue & Techno::getValueAsString methods have optional arguments
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(getValue_overloads , getValue , 1, 3);
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(getValueAsString_overloads, getValueAsString, 1, 3);
// specify that Techno::addRule method has optional arguments
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(addRule_overloads, addRule, 3, 5);
BOOST_PYTHON_MODULE(pyDTR) { BOOST_PYTHON_MODULE(pyDTR) {
// class DTR::Name // class DTR::Name
class_<Name>("Name", init<std::string>()) class_<Name>("Name", init<std::string>())
@ -19,7 +26,7 @@ BOOST_PYTHON_MODULE(pyDTR) {
; ;
// class DTR::Rule // class DTR::Rule
class_<Rule>("Rule", init<Name, double, Name, Name, Name, Name>()) class_<Rule>("Rule", init<Name, double, Name, Name, Name>())
// accessors // accessors
.def("getName" , &Rule::getName ) .def("getName" , &Rule::getName )
.def("getType" , &Rule::getType ) .def("getType" , &Rule::getType )
@ -28,27 +35,31 @@ BOOST_PYTHON_MODULE(pyDTR) {
.def("getRef" , &Rule::getRef ) .def("getRef" , &Rule::getRef )
.def("getLayer1" , &Rule::getLayer1 ) .def("getLayer1" , &Rule::getLayer1 )
.def("getLayer2" , &Rule::getLayer2 ) .def("getLayer2" , &Rule::getLayer2 )
// modifiers // modifiers
.def("setType" , &Rule::setType ) .def("setType" , &Rule::setType )
; ;
// class DTR::ARule derived from DTR::Rule // class DTR::ARule derived from DTR::Rule
class_<ARule, bases<Rule> >("ARule", init<Name, double, Name, Name, Name, Name>()) class_<ARule, bases<Rule> >("ARule", init<Name, double, Name, Name, Name>())
; ;
// class DTR::Techno // class DTR::Techno
class_<Techno>("Techno", init<Name, Name>()) class_<Techno>("Techno", init<Name, Name>())
// accessors
.def("getName" , &Techno::getName)
.def("getUnit" , &Techno::getUnit)
.def("getValue" , &Techno::getValue, getValue_overloads())
.def("getValueAsString", &Techno::getValueAsString, getValueAsString_overloads())
// modifiers
.def("addRule" , &Techno::addRule , addRule_overloads()[return_value_policy<reference_existing_object>()])
.def("addARule" , &Techno::addARule, return_value_policy<reference_existing_object>())
// others
.def("readFromFile", &Techno::readFromFile, return_value_policy<manage_new_object>()) .def("readFromFile", &Techno::readFromFile, return_value_policy<manage_new_object>())
.staticmethod("readFromFile") .staticmethod("readFromFile")
.def("writeToFile" , &Techno::writeToFile)
.def("getName" , &Techno::getName )
.def("getUnit" , &Techno::getUnit )
.def("getValue" , static_cast<double(Techno::*)(Name )>(&Techno::getValue))
.def("getValue" , static_cast<double(Techno::*)(Name, Name )>(&Techno::getValue))
.def("getValue" , static_cast<double(Techno::*)(Name, Name, Name)>(&Techno::getValue))
.def("getValueAsString", static_cast<string(Techno::*)(Name )>(&Techno::getValueAsString))
.def("getValueAsString", static_cast<string(Techno::*)(Name, Name )>(&Techno::getValueAsString))
.def("getValueAsString", static_cast<string(Techno::*)(Name, Name, Name)>(&Techno::getValueAsString))
; ;
// DTRException translator // DTRException translator

View File

@ -34,26 +34,18 @@ namespace {
namespace DTR { namespace DTR {
Techno::Techno(Name name, Name unit) : _name(name), _unit(unit) {} Techno::Techno(Name name, Name unit) : _name(name), _unit(unit) {}
Rule* Techno::addRule (Name name, double value, Name valueStr, Name ref, Name layer1, Name layer2) { Rule* Techno::addRule (Name name, double value, Name ref, Name layer1, Name layer2) {
Rule* rule = new Rule(name, value, valueStr, ref, layer1, layer2); Rule* rule = new Rule(name, value, ref, layer1, layer2);
_rules.push_back(rule); _rules.push_back(rule);
return rule; return rule;
} }
ARule* Techno::addARule (Name name, double value, Name valueStr, Name ref, Name layer1, Name layer2) { ARule* Techno::addARule (Name name, double value, Name ref, Name layer1, Name layer2) {
ARule* arule = new ARule(name, value, valueStr, ref, layer1, layer2); ARule* arule = new ARule(name, value, ref, layer1, layer2);
_rules.push_back(arule); _rules.push_back(arule);
return arule; return arule;
} }
double Techno::getValue(Name name) {
return getValue(name, Name(""), Name(""));
}
double Techno::getValue(Name name, Name layer) {
return getValue(name, layer, Name(""));
}
double Techno::getValue(Name name, Name layer1, Name layer2) { double Techno::getValue(Name name, Name layer1, Name layer2) {
bool testL1 = (layer1 == Name("")) ? false : true; bool testL1 = (layer1 == Name("")) ? false : true;
bool testL2 = (layer2 == Name("")) ? false : true; bool testL2 = (layer2 == Name("")) ? false : true;
@ -86,14 +78,6 @@ double Techno::getValue(Name name, Name layer1, Name layer2) {
throw DTRException(error); throw DTRException(error);
} }
string Techno::getValueAsString(Name name) {
return getValueAsString(name, Name(""), Name(""));
}
string Techno::getValueAsString(Name name, Name layer) {
return getValueAsString(name, layer, Name(""));
}
string Techno::getValueAsString(Name name, Name layer1, Name layer2) { string Techno::getValueAsString(Name name, Name layer1, Name layer2) {
bool testL1 = (layer1 == Name("")) ? false : true; bool testL1 = (layer1 == Name("")) ? false : true;
bool testL2 = (layer2 == Name("")) ? false : true; bool testL2 = (layer2 == Name("")) ? false : true;
@ -176,21 +160,18 @@ Techno* Techno::readFromFile(const string filePath) {
Name layer1 ((const char*)layer1C); Name layer1 ((const char*)layer1C);
Name layer2 ((const char*)layer2C); Name layer2 ((const char*)layer2C);
double value = ::getValue<double>(valueC); double value = ::getValue<double>(valueC);
Name valueStr ((const char*)valueC); rule = techno->addRule(name, value, ref, layer1, layer2);
rule = techno->addRule(name, value, valueStr, ref, layer1, layer2);
} else if (nameC && layerC && valueC && refC) {// rule with only one layer } else if (nameC && layerC && valueC && refC) {// rule with only one layer
Name name ((const char*)nameC); Name name ((const char*)nameC);
Name ref ((const char*)refC); Name ref ((const char*)refC);
Name layer ((const char*)layerC); Name layer ((const char*)layerC);
double value = ::getValue<double>(valueC); double value = ::getValue<double>(valueC);
Name valueStr ((const char*)valueC); rule = techno->addRule(name, value, ref, layer);
rule = techno->addRule(name, value, valueStr, ref, layer);
} else if (nameC && valueC && refC) { // rule without layer } else if (nameC && valueC && refC) { // rule without layer
Name name ((const char*)nameC); Name name ((const char*)nameC);
Name ref ((const char*)refC); Name ref ((const char*)refC);
double value = ::getValue<double>(valueC); double value = ::getValue<double>(valueC);
Name valueStr ((const char*)valueC); rule = techno->addRule(name, value, ref);
rule = techno->addRule(name, value, valueStr, ref);
} else { // invalid case } else { // invalid case
throw DTRException("[ERROR] properties of 'rule' node must be ('name', 'value', 'ref') or ('name', 'layer', 'value', 'ref') or ('name', 'layer1', 'layer2', 'value', 'ref')."); throw DTRException("[ERROR] properties of 'rule' node must be ('name', 'value', 'ref') or ('name', 'layer', 'value', 'ref') or ('name', 'layer1', 'layer2', 'value', 'ref').");
return NULL; return NULL;
@ -211,8 +192,7 @@ Techno* Techno::readFromFile(const string filePath) {
Name layer2 ((const char*)layer2C); Name layer2 ((const char*)layer2C);
Name ref ((const char*)refC); Name ref ((const char*)refC);
double value = ::getValue<double>(valueC); double value = ::getValue<double>(valueC);
Name valueStr ((const char*)valueC); techno->addARule(name, value, ref, layer1, layer2);
techno->addARule(name, value, valueStr, ref, layer1, layer2);
} else { } else {
throw DTRException("[ERROR] 'arule' node must have 'name', 'layer1', 'layer2', 'value' and 'ref' properties."); throw DTRException("[ERROR] 'arule' node must have 'name', 'layer1', 'layer2', 'value' and 'ref' properties.");
return NULL; return NULL;

View File

@ -10,13 +10,23 @@
#ifndef __DTR_RULES_H__ #ifndef __DTR_RULES_H__
#define __DTR_RULES_H__ #define __DTR_RULES_H__
#include <string>
#include <sstream>
#include "vlsisapd/dtr/Name.h" #include "vlsisapd/dtr/Name.h"
namespace DTR { namespace DTR {
template <typename T>
std::string to_string(T const& value) {
std::stringstream sstr;
sstr << value;
return sstr.str();
}
class Rule { class Rule {
public: public:
Rule(Name name, double value, Name valueStr, Name ref, Name layer1, Name layer2) Rule(Name name, double value, Name ref, Name layer1, Name layer2)
: _name(name), _type(Name("")), _value(value), _valueStr(valueStr), _ref(ref), _layer1(layer1), _layer2(layer2) {}; : _name(name), _type(Name("")), _value(value), _valueStr(to_string(value)), _ref(ref), _layer1(layer1), _layer2(layer2) {};
// accessors // accessors
inline Name getName(); inline Name getName();
@ -43,8 +53,8 @@ class Rule {
class ARule : public Rule { class ARule : public Rule {
public: public:
ARule(Name name, double value, Name valueStr, Name ref, Name layer1, Name layer2) ARule(Name name, double value, Name ref, Name layer1, Name layer2)
: Rule(name, value, valueStr, ref, layer1, layer2) {}; : Rule(name, value, ref, layer1, layer2) {};
}; };
inline Name Rule::getName() { return _name; }; inline Name Rule::getName() { return _name; };

View File

@ -28,16 +28,11 @@ class Techno {
inline Name getUnit(); inline Name getUnit();
inline std::vector<Rule*>& getRules(); inline std::vector<Rule*>& getRules();
Rule* addRule (Name name, double value, Name valueStr, Name ref, Name layer1=Name(""), Name layer2=Name("")); Rule* addRule (Name name, double value, Name ref, Name layer1=Name(""), Name layer2=Name(""));
ARule* addARule(Name name, double value, Name valueStr, Name ref, Name layer1 , Name layer2); ARule* addARule(Name name, double value, Name ref, Name layer1 , Name layer2);
double getValue(Name name); double getValue(Name name, Name layer1=Name(""), Name layer2=Name(""));
double getValue(Name name, Name layer); std::string getValueAsString(Name name, Name layer1=Name(""), Name layer2=Name(""));
double getValue(Name name, Name layer1, Name layer2);
std::string getValueAsString(Name name);
std::string getValueAsString(Name name, Name layer);
std::string getValueAsString(Name name, Name layer1, Name layer2);
bool writeToFile(std::string filePath); bool writeToFile(std::string filePath);
static Techno* readFromFile(const std::string filePath); static Techno* readFromFile(const std::string filePath);