Add colide support

master
Marko Semet 2019-07-12 13:56:25 +02:00
parent 11a65ace44
commit a05191e50c
3 changed files with 82 additions and 3 deletions

View File

@ -53,10 +53,13 @@ namespace sirEdit::data {
FieldMeta __meta;
std::unordered_map<std::string, std::vector<std::string>> __hints;
std::unordered_map<std::string, std::vector<std::string>> __restrictions;
std::vector<const Field*> __collide;
public:
Field() {}
Field(std::string name, std::string comment, FieldType type) : __name(std::move(name)), __comment(std::move(comment)), __type(std::move(type)) {}
Field(std::string name, std::string comment, FieldType type) : __name(std::move(name)), __comment(std::move(comment)), __type(std::move(type)) {
this->__meta.type = FieldMeta::META_TYPE::NORMAL;
}
const std::string& getName() const { return this->__name; }
const std::string& getComment() const { return this->__comment; }
@ -64,12 +67,14 @@ namespace sirEdit::data {
const FieldMeta& getMeta() const { return this->__meta; }
const std::unordered_map<std::string, std::vector<std::string>>& getHints() const { return this->__hints; }
const std::unordered_map<std::string, std::vector<std::string>>& getRestrictions() const { return this->__restrictions; }
const std::vector<const Field*> getCollide() const { return this->__collide; }
std::string& getName() { return this->__name; }
std::string& getComment() { return this->__comment; }
FieldType& getType() { return this->__type; }
FieldMeta& getMeta() { return this->__meta; }
std::unordered_map<std::string, std::vector<std::string>>& getHints() { return this->__hints; }
std::unordered_map<std::string, std::vector<std::string>>& getRestrictions() { return this->__restrictions; }
std::vector<const Field*> getCollide() { return this->__collide; }
std::string printType() const;
};

View File

@ -53,6 +53,43 @@ namespace sirEdit::data {
// Create new subtype information
for(auto& i : this->__types)
this->updateTypeInfo(i);
// Calculating collide of types
std::vector<Type*> onlySubtype; // Types that are at then end of the hirarchie
std::unordered_map<std::string, std::vector<Type*>> collideInfo;
for(auto& i : this->__types) {
if(i->getSubTypes().size() == 0)
onlySubtype.push_back(i);
auto tmp = collideInfo.find(i->getName());
if(tmp == collideInfo.end())
collideInfo[i->getName()] = {i};
else
tmp->second.push_back(i);
}
for(auto& i : collideInfo) // Set colide info for types
if(i.second.size() > 1)
for(auto& j : i.second) {
j->getCollides() = {i.second.begin(), i.second.end()};
j->getCollides().erase(std::find(j->getCollides().begin(), j->getCollides().end(), j));
}
// Calculating collide of fields
for(auto& i : onlySubtype) {
std::unordered_map<std::string, std::vector<Field*>> fieldName;
for(auto& j : listAllFields(*i)) { // Search for broken fields
auto tmp = fieldName.find(j->getName());
if(tmp == fieldName.end())
fieldName[j->getName()] = {const_cast<Field*>(j)};
else
fieldName[j->getName()].push_back(const_cast<Field*>(j));
}
for(auto& j : fieldName) // Updte fields
if(j.second.size() > 1)
for(auto& k : j.second) {
k->getCollide() = {j.second.begin(), j.second.end()};
k->getCollide().erase(std::find(k->getCollide().begin(), k->getCollide().end(), k));
}
}
}
virtual void addBaseType(Type* type) = 0;
@ -81,6 +118,9 @@ namespace sirEdit::data {
this->__tools.erase(std::find(this->__tools.begin(), this->__tools.end(), tool));
this->removeBaseTool(tool);
}
void update() {
this->updateRelationships();
}
virtual void prepareSave() = 0;
virtual void save() = 0;
@ -108,7 +148,7 @@ namespace sirEdit::data {
std::vector<std::function<void()>> __change_callback;
template<class T>
inline void updateCall(T& list) {
void updateCall(T& list) {
for(auto& i : list)
i();
}
@ -160,6 +200,12 @@ namespace sirEdit::data {
this->__serializer.removeTool(const_cast<Tool*>(&tool));
updateCall(this->__change_callback);
}
void updateTool(const Tool& tool, std::string name, std::string description, std::string cmd) {
const_cast<Tool*>(&tool)->getName() = std::move(name);
const_cast<Tool*>(&tool)->getDescription() = std::move(description);
const_cast<Tool*>(&tool)->getCommand() = std::move(cmd);
updateCall(this->__change_callback);
}
void importTools(const std::vector<Tool*>& tools) {
std::unordered_map<std::string, const Tool*> toolNames; // load tool names
for(auto& i : this->__serializer.getTools())

View File

@ -2,6 +2,7 @@
#include <memory>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <sirEdit/data/fields.hpp>
@ -26,9 +27,10 @@ namespace sirEdit::data
std::string __metaTypeName = "<NOT SET>";
std::unordered_map<std::string, std::vector<std::string>> __hints;
std::unordered_map<std::string, std::vector<std::string>> __restrictions;
std::vector<const Type*> __collide;
public:
Type(const Type& type) : __name(type.__name), __comment(type.__comment) {}
Type(const Type& type) : __name(type.__name), __comment(type.__comment), __hints(type.__hints), __restrictions(type.__restrictions) {}
Type(std::string name, std::string comment) : __name(std::move(name)), __comment(std::move(comment)) {}
virtual ~Type() {}
@ -45,6 +47,7 @@ namespace sirEdit::data
const std::string& getMetaTypeName() const { return this->__metaTypeName; }
const std::unordered_map<std::string, std::vector<std::string>>& getHints() const { return this->__hints; }
const std::unordered_map<std::string, std::vector<std::string>>& getRestrictions() const { return this->__restrictions; }
const std::vector<const Type*>& getCollides() const { return this->__collide; }
std::string& getName() { return this->__name; }
std::string& getComment() { return this->__comment; }
std::vector<Type*>& getSubTypes() { return this->__subTypes; }
@ -52,6 +55,7 @@ namespace sirEdit::data
std::string& getMetaTypeName() { return this->__metaTypeName; }
std::unordered_map<std::string, std::vector<std::string>>& getHints() { return this->__hints; }
std::unordered_map<std::string, std::vector<std::string>>& getRestrictions() { return this->__restrictions; }
std::vector<const Type*>& getCollides() { return this->__collide; }
};
class TypeWithFields : public Type {
private:
@ -195,6 +199,30 @@ namespace sirEdit::data
return doBaseType(&type, isBase, isInterface, isClass, isEnum, isBase);
}
inline std::unordered_set<const Field*> listAllFields(const Type& type) {
// Add own fields
std::unordered_set<const Field*> result;
for(auto& i : getFields(type))
result.insert(&i);
// Add super fields
{
auto tmp = getSuper(type);
if(tmp != nullptr) {
auto tmp2 = listAllFields(*tmp);
result.insert(tmp2.begin(), tmp2.end());
}
}
// Add interfaces
for(auto& i : getInterfaces(type)) {
auto tmp = listAllFields(*i);
result.insert(tmp.begin(), tmp.end());
}
return result;
}
inline std::string Field::printType() const {
// Type parse
std::string result;