Skip to content

Commit

Permalink
Move generateSetter and generateGetter methods to Struct class
Browse files Browse the repository at this point in the history
  • Loading branch information
kornilova203 committed Jul 13, 2018
1 parent 699f4d5 commit 8fabd88
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 38 deletions.
70 changes: 36 additions & 34 deletions bindgen/ir/Struct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,6 @@
Field::Field(std::string name, std::shared_ptr<Type> type)
: TypeAndName(std::move(name), std::move(type)) {}

std::string Field::generateSetter(int fieldIndex) {
std::string setter = handleReservedWords(getName(), "_=");
std::string parameterType = type->str();
std::string value = "value";
if (isAliasForType<ArrayType>(type.get()) ||
isAliasForType<Struct>(type.get())) {
parameterType = "native.Ptr[" + parameterType + "]";
value = "!" + value;
}
std::stringstream s;
s << " def " << setter << "(value: " + parameterType + "): Unit = !p._"
<< std::to_string(fieldIndex + 1) << " = " << value;
return s.str();
}

std::string Field::generateGetter(int fieldIndex) {
std::string getter = handleReservedWords(getName());
std::string returnType = type->str();
std::string methodBody;
if (isAliasForType<ArrayType>(type.get()) ||
isAliasForType<Struct>(type.get())) {
returnType = "native.Ptr[" + returnType + "]";
methodBody = "p._" + std::to_string(fieldIndex + 1);
} else {
methodBody = "!p._" + std::to_string(fieldIndex + 1);
}
std::stringstream s;
s << " def " << getter << ": " << returnType << " = " << methodBody;
return s.str();
}

StructOrUnion::StructOrUnion(std::string name,
std::vector<std::shared_ptr<Field>> fields,
std::shared_ptr<Location> location)
Expand Down Expand Up @@ -101,11 +70,11 @@ std::string Struct::generateHelperClass() const {
s << " implicit class " << type << "_ops(val p: native.Ptr[" << type
<< "])"
<< " extends AnyVal {\n";
int fieldIndex = 0;
unsigned fieldIndex = 0;
for (const auto &field : fields) {
if (!field->getName().empty()) {
s << field->generateGetter(fieldIndex) << "\n";
s << field->generateSetter(fieldIndex) << "\n";
s << generateGetter(fieldIndex) << "\n";
s << generateSetter(fieldIndex) << "\n";
}
fieldIndex++;
}
Expand Down Expand Up @@ -158,6 +127,39 @@ bool Struct::operator==(const Type &other) const {
return false;
}

std::string Struct::generateSetter(unsigned fieldIndex) const {
std::shared_ptr<Field> field = fields[fieldIndex];
std::string setter = handleReservedWords(field->getName(), "_=");
std::string parameterType = field->getType()->str();
std::string value = "value";
if (isAliasForType<ArrayType>(field->getType().get()) ||
isAliasForType<Struct>(field->getType().get())) {
parameterType = "native.Ptr[" + parameterType + "]";
value = "!" + value;
}
std::stringstream s;
s << " def " << setter << "(value: " + parameterType + "): Unit = !p._"
<< std::to_string(fieldIndex + 1) << " = " << value;
return s.str();
}

std::string Struct::generateGetter(unsigned fieldIndex) const {
std::shared_ptr<Field> field = fields[fieldIndex];
std::string getter = handleReservedWords(field->getName());
std::string returnType = field->getType()->str();
std::string methodBody;
if (isAliasForType<ArrayType>(field->getType().get()) ||
isAliasForType<Struct>(field->getType().get())) {
returnType = "native.Ptr[" + returnType + "]";
methodBody = "p._" + std::to_string(fieldIndex + 1);
} else {
methodBody = "!p._" + std::to_string(fieldIndex + 1);
}
std::stringstream s;
s << " def " << getter << ": " << returnType << " = " << methodBody;
return s.str();
}

Union::Union(std::string name, std::vector<std::shared_ptr<Field>> fields,
uint64_t maxSize, std::shared_ptr<Location> location)
: StructOrUnion(std::move(name), std::move(fields), std::move(location)),
Expand Down
8 changes: 4 additions & 4 deletions bindgen/ir/Struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
class Field : public TypeAndName {
public:
Field(std::string name, std::shared_ptr<Type> type);

std::string generateSetter(int fieldIndex);

std::string generateGetter(int fieldIndex);
};

class StructOrUnion {
Expand Down Expand Up @@ -66,6 +62,10 @@ class Struct : public StructOrUnion,

bool operator==(const Type &other) const override;

std::string generateSetter(unsigned fieldIndex) const;

std::string generateGetter(unsigned fieldIndex) const;

private:
/* type size is needed if number of fields is bigger than 22 */
uint64_t typeSize;
Expand Down

0 comments on commit 8fabd88

Please sign in to comment.