Fixed builders, fixed passing arguments to ClassBuilder

This commit is contained in:
Łukasz Czyż 2019-11-22 15:22:52 +01:00
parent 04a24f9bbf
commit 0605e58c05
9 changed files with 64 additions and 56 deletions

View file

@ -1,39 +1,47 @@
#include <Builders.hpp> #include <Builders.hpp>
#include <iostream>
void ZeroPointerBuilder::build(std::stringstream& ss){
void ZeroPointerBuilder::build(std::stringstream& ss) const{
ss << "template <unsigned int zero = 0>\n" ss << "template <unsigned int zero = 0>\n"
"constexpr unsigned int *zeroVal = reinterpret_cast<unsigned int *>(zero);\n"; "constexpr unsigned int *zeroVal = reinterpret_cast<unsigned int *>(zero);\n";
} }
void PeripheralBuilder::build(std::stringstream& ss){ void PeripheralBuilder::build(std::stringstream& ss) const{
ss << "namespace " << peripheral.name.c_str() << "{\n"; ss << "namespace " << peripheral.name << "{\n";
for(auto& registe : peripheral.registers){ for(auto& registe : peripheral.registers){
RegisterBuilder(registe).build(ss); RegisterBuilder(registe, peripheral.baseAddress).build(ss);
} }
ss << "}\n"; ss << "}\n";
} }
void RegisterBuilder::build(std::stringstream& ss){ void RegisterBuilder::build(std::stringstream& ss) const{
//TODO: fix INVALID: some derived names are not copied
ss << " " ss << " "
<< "namespace " << (registe.name.length() > 100 ? "INVALID" : registe.name) << "{\n"; << "namespace " << registe.name << "{\n";
for(auto& field : registe.fields){ for(auto& field : registe.fields){
FieldBuilder(field).build(ss); FieldBuilder(field, getRegisterAddress()).build(ss);
} }
ss << " " << "}\n"; ss << " " << "}\n";
} }
void FieldBuilder::build(std::stringstream& ss){ unsigned int RegisterBuilder::getRegisterAddress() const{
ss << " " << "struct " << (field.name.length() > 100 ? "INVALID" : field.name) << "{\n"; return baseAddress + registe.addressOffset;
}
void FieldBuilder::build(std::stringstream& ss) const{
ss << " " << "struct " << field.name << "{\n";
//TODO: fix INVALID: some derived items are not copied
ss << " " << " " << "constexpr static inline unsigned int bitOffset(){" ss << " " << " " << "constexpr static inline unsigned int bitOffset(){"
<< "return " << field.bitOffset << "}\n"; << "return " << field.bitOffset << ";}\n";
ss << " " << " " << "constexpr static inline unsigned int bitMask(){" ss << " " << " " << "constexpr static inline unsigned int bitMask(){"
<< "return " << field.bitWidth << "}\n"; << "return " << field.bitWidth << ";}\n";
ss << " " << " " << "constexpr static inline unsigned int address(){" ss << " " << " " << "constexpr static inline unsigned int* address(){"
<< "return " << "TODO" << "}\n"; << "return zeroVal<> + " << getAddress() << ";}\n";
ss << " }\n"; ss << " };\n";
}
std::string FieldBuilder::getAddress() const{
return std::to_string(registerAddress);
} }

View file

@ -7,30 +7,35 @@
#include <sstream> #include <sstream>
struct ZeroPointerBuilder : public IBuilder{ struct ZeroPointerBuilder : public IBuilder{
void build(std::stringstream& ss) final; void build(std::stringstream& ss) const final;
}; };
struct PeripheralBuilder : public IBuilder{ struct PeripheralBuilder : public IBuilder{
PeripheralBuilder(const Peripheral &peripheral_) : peripheral(peripheral_) {} PeripheralBuilder(const Peripheral &peripheral_) : peripheral(peripheral_) {}
void build(std::stringstream& ss) final; void build(std::stringstream& ss) const final;
private: private:
const Peripheral& peripheral; const Peripheral& peripheral;
}; };
struct RegisterBuilder : public IBuilder{ struct RegisterBuilder : public IBuilder{
RegisterBuilder(const Register& register_) : registe(register_){} RegisterBuilder(const Register& register_, const unsigned int baseAddress_)
void build(std::stringstream& ss) final; : registe(register_), baseAddress(baseAddress_) {}
void build(std::stringstream& ss) const final;
unsigned int getRegisterAddress() const;
private: private:
const Register& registe; const Register& registe;
const unsigned int baseAddress;
}; };
struct FieldBuilder : public IBuilder{ struct FieldBuilder : public IBuilder{
FieldBuilder(const Field& field_) : field(field_){} FieldBuilder(const Field& field_, const unsigned int registerAddress_)
void build(std::stringstream& ss) final; : field(field_), registerAddress(registerAddress_) {}
void build(std::stringstream& ss) const final;
std::string getAddress() const;
private: private:
const Field& field; const Field& field;
const unsigned int registerAddress;
}; };
#endif #endif

View file

@ -2,13 +2,12 @@
#include <Builders.hpp> #include <Builders.hpp>
ClassBuilder::ClassBuilder(const cxxopts::ParseResult& results, ClassBuilder::ClassBuilder(const cxxopts::ParseResult& results_,
const DeviceInfo& deviceInfo, const DeviceInfo& deviceInfo_,
const std::vector<Peripheral>& peripherals) : const std::vector<Peripheral>& peripherals_) :
results(results), results(results_),
deviceInfo(deviceInfo), deviceInfo(deviceInfo_),
peripherals(peripherals){ peripherals(peripherals_){}
}
void ClassBuilder::setupBuilders(){ void ClassBuilder::setupBuilders(){
builders.push_back(std::make_unique<ZeroPointerBuilder>()); builders.push_back(std::make_unique<ZeroPointerBuilder>());

View file

@ -12,9 +12,9 @@
struct ClassBuilder struct ClassBuilder
{ {
ClassBuilder(const cxxopts::ParseResult& results, ClassBuilder(const cxxopts::ParseResult& results_,
const DeviceInfo& deviceInfo, const DeviceInfo& deviceInfo_,
const std::vector<Peripheral>& peripherals); const std::vector<Peripheral>& peripherals_);
void setupBuilders(); void setupBuilders();
void build(); void build();

View file

@ -5,7 +5,7 @@
struct IBuilder{ struct IBuilder{
virtual void build(std::stringstream&) = 0; virtual void build(std::stringstream&) const = 0;
virtual ~IBuilder() = default; virtual ~IBuilder() = default;
}; };

View file

@ -11,17 +11,13 @@ enum class EAccess{
Read_Write Read_Write
}; };
struct IDisplay{ struct Field{
virtual void display() = 0;
};
struct Field : public IDisplay{
std::string name; std::string name;
std::string description; std::string description;
unsigned int bitOffset; unsigned int bitOffset;
unsigned int bitWidth; unsigned int bitWidth;
EAccess fieldAccess; EAccess fieldAccess;
void display() final{ void display() const{
std::cout << "\t\tname: " << name << std::endl std::cout << "\t\tname: " << name << std::endl
<< "\t\tdescription: " << description << std::endl << "\t\tdescription: " << description << std::endl
<< "\t\tbitOffset: " << bitOffset << std::endl << "\t\tbitOffset: " << bitOffset << std::endl
@ -31,7 +27,7 @@ struct Field : public IDisplay{
} }
}; };
struct Register : public IDisplay{ struct Register{
std::string name; std::string name;
std::string description; std::string description;
unsigned int addressOffset; unsigned int addressOffset;
@ -40,7 +36,7 @@ struct Register : public IDisplay{
unsigned int resetValue; unsigned int resetValue;
std::vector<Field> fields; std::vector<Field> fields;
void display() final{ void display() const{
std::cout << "\tname: " << name << std::endl std::cout << "\tname: " << name << std::endl
<< "\tdescription: " << description << std::endl << "\tdescription: " << description << std::endl
<< "\taddressOffset: " << addressOffset << std::endl << "\taddressOffset: " << addressOffset << std::endl
@ -55,24 +51,24 @@ struct Register : public IDisplay{
} }
}; };
struct AddressBlock : public IDisplay{ struct AddressBlock{
unsigned int offset; unsigned int offset;
unsigned int size; unsigned int size;
void display() final{ void display() const{
std::cout << std::endl std::cout << std::endl
<< "\toffset: " << offset << std::endl << "\toffset: " << offset << std::endl
<< "\tsize: " << size << std::endl; << "\tsize: " << size << std::endl;
} }
}; };
struct Peripheral : public IDisplay{ struct Peripheral{
std::string name; std::string name;
std::string description; std::string description;
std::string groupName; std::string groupName;
unsigned int baseAddress; unsigned int baseAddress;
AddressBlock addressBlock; AddressBlock addressBlock;
std::vector<Register> registers; std::vector<Register> registers;
void display() final{ void display() const{
std::cout << std::endl std::cout << std::endl
<< "name: " << name << std::endl << "name: " << name << std::endl
<< "description: " << description << std::endl << "description: " << description << std::endl

View file

@ -41,7 +41,7 @@ void XmlParser::parseXml(){
void XmlParser::setDeviceInfoAttrib(tinyxml2::XMLElement* deviceRoot, const char* name, std::string &field) const{ void XmlParser::setDeviceInfoAttrib(tinyxml2::XMLElement* deviceRoot, const char* name, std::string &field) const{
tinyxml2::XMLElement* deviceEntry = deviceRoot->FirstChildElement(name); tinyxml2::XMLElement* deviceEntry = deviceRoot->FirstChildElement(name);
field = deviceEntry != nullptr ? deviceEntry->GetText() : noValue; field = deviceEntry != nullptr ? std::string(deviceEntry->GetText()) : noValue;
} }
void XmlParser::setDeviceInfoAttrib(tinyxml2::XMLElement* deviceRoot, const char* name, unsigned int &field) const{ void XmlParser::setDeviceInfoAttrib(tinyxml2::XMLElement* deviceRoot, const char* name, unsigned int &field) const{
@ -79,8 +79,8 @@ Peripheral XmlParser::parsePeripheral(tinyxml2::XMLElement* peripheralRoot) cons
//Find the base peripheral and copy it to the new one //Find the base peripheral and copy it to the new one
auto crit = [&](auto &periph) { return periph.name == derivedFrom; }; auto crit = [&](auto &periph) { return periph.name == derivedFrom; };
auto resultIt = std::find_if(peripherals.begin(), peripherals.end(), crit); auto resultIt = std::find_if(peripherals.begin(), peripherals.end(), crit);
if(resultIt < peripherals.end()){ if(resultIt <= peripherals.end()){
peripheral = *resultIt; peripheral = Peripheral(*resultIt);
} }
else{ else{
std::cout << "Couldn't find peripheral " << derivedFrom << std::endl; std::cout << "Couldn't find peripheral " << derivedFrom << std::endl;

View file

@ -13,10 +13,10 @@ struct XmlParser{
XmlParser(const std::string& inputFile); XmlParser(const std::string& inputFile);
std::optional<std::string> isError() const; std::optional<std::string> isError() const;
void parseXml(); void parseXml();
inline const DeviceInfo getDeviceInfo() const{ inline const DeviceInfo& getDeviceInfo() const{
return deviceInfo; return deviceInfo;
} }
inline const std::vector<Peripheral> getPeripherals() const{ inline const std::vector<Peripheral>& getPeripherals() const{
return peripherals; return peripherals;
} }

View file

@ -24,7 +24,7 @@ int main(int argc, char** argv){
} }
inputFile = result["input"].as<std::string>(); inputFile = result["input"].as<std::string>();
outputFile = result["output"].as<std::string>(); outputFile = result["output"].as<std::string>();
std::cout << "Input: " << inputFile << "\tOutput: " << outputFile << std::endl; // std::cout << "Input: " << inputFile << "\tOutput: " << outputFile << std::endl;
} }
catch(cxxopts::OptionException& ex){ catch(cxxopts::OptionException& ex){
std::cout << ex.what() << std::endl; std::cout << ex.what() << std::endl;