}
std::string CGeneratorBase::GetDataTypeString(const BaseType& type,
- bool is_pointer, const std::string& id) {
- if (type.GetUserDefinedType() == BaseType::UserType::ENUM) {
- auto n = type.ToString().find('.');
- if (id.empty()&& n == std::string::npos) {
- if (is_pointer)
- return GetEnumTypeString(ParameterType::Direction::OUT, type,
- GetEnumBockString(type.ToString()));
- else
- return GetEnumTypeString(ParameterType::Direction::IN, type,
- GetEnumBockString(type.ToString()));
+ bool is_pointer,
+ const std::string& id) {
+ if (type.GetUserDefinedType() == BaseType::UserType::ENUM) {
+ auto n = type.ToString().find('.');
+ if (id.empty() && n == std::string::npos) {
+ if (is_pointer) {
+ return GetEnumTypeString(ParameterType::Direction::OUT, type,
+ GetEnumBockString(type.ToString()));
+ } else {
+ return GetEnumTypeString(ParameterType::Direction::IN, type,
+ GetEnumBockString(type.ToString()));
}
+ }
- if (is_pointer)
- return GetEnumTypeString(ParameterType::Direction::OUT, type, id);
- else
- return GetEnumTypeString(ParameterType::Direction::IN, type, id);
- }
+ if (is_pointer)
+ return GetEnumTypeString(ParameterType::Direction::OUT, type, id);
+ else
+ return GetEnumTypeString(ParameterType::Direction::IN, type, id);
+ }
if (type.GetUserDefinedType() == BaseType::UserType::STRUCTURE ||
type.GetUserDefinedType() == BaseType::UserType::DELEGATE)
std::string type_id;
auto n = id.find('.');
- if (n == std::string::npos) {
+ if (n == std::string::npos)
return id;
- } else {
- block_id = id.substr(0, n);
- type_id = id.substr(n + 1, id.size() - (n + 1));
- return block_id + "_" + type_id;
- }
+
+ block_id = id.substr(0, n);
+ type_id = id.substr(n + 1, id.size() - (n + 1));
+ return block_id + "_" + type_id;
}
std::string CGeneratorBase::GetEnumTypeString(
}
std::string CGeneratorBase::GetEnumBockString(const std::string& id) {
- for (auto& b : GetDocument().GetBlocks()) {
- for (auto& e : b->GetEnums()) {
+ for (auto& b : GetDocument().GetBlocks()) {
+ for (auto& e : b->GetEnums()) {
if (e->GetID() == id) {
return b->GetID();
}
namespace {
bool IsObject(const BaseType& type) {
+ if (type.IsEnumType())
+ return false;
+
if (type.IsUserDefinedType() ||
type.GetMetaType() != nullptr ||
type.GetKeyType() != nullptr ||
std::string CppGeneratorBase::ConvertTypeToString(const BaseType& type,
bool full_name) {
+ if (type.IsEnumType())
+ return GetEnumTypeString(type.ToString());
+
if (type.IsUserDefinedType()) {
if (IsDelegateType(type)) {
std::string name = type.ToString();
}
std::string CppGeneratorBase::GenStructureForHeader(const Structure& st) {
- return std::string(
- ReplaceAll(CB_HEADER_STRUCTURE_BASE)
+ std::string code;
+ if (st.GetElements().Empty()) {
+ code = ReplaceAll(CB_HEADER_STRUCTURE_BASE_EMPTY)
+ .Change("<CLS_NAME>", st.GetID())
+ .Change("<ENUMS>\n", GenEnumerations(st.GetEnums()));
+ } else {
+ code = ReplaceAll(CB_HEADER_STRUCTURE_BASE)
.Change("<CLS_NAME>", st.GetID())
.Change("<PARAMS>", GenParameters(st.GetElements()))
+ .Change("<ENUMS>\n", GenEnumerations(st.GetEnums()))
.Change("<GETTER_SETTER>",
GenStructureGetterSetterForHeader(st.GetElements()))
.Change("<MEMBERS>\n",
- GenStructureMembersForHeader(st.GetElements())));
+ GenStructureMembersForHeader(st.GetElements()));
+ }
+
+ return code;
}
std::string CppGeneratorBase::GenStructureGetterSetterForHeader(
}
std::string CppGeneratorBase::GenStructure(const Structure& st) {
- return std::string(
- ReplaceAll(CB_BODY_STRUCTURE_BASE)
- .Change("<CLS_NAME>", st.GetID())
- .Change("<PARAMS>", GenParameters(st.GetElements()))
- .Change("<MEMBER_INIT>", GenStructureMemberInit(st.GetElements()))
- .Change("<GETTER_SETTER>", GenStructureGetterSetter(st)));
+ std::string code;
+ if (st.GetElements().Empty()) {
+ code = ReplaceAll(CB_BODY_STRUCTURE_BASE_EMPTY)
+ .Change("<CLS_NAME>", st.GetID());
+ } else {
+ code = ReplaceAll(CB_BODY_STRUCTURE_BASE)
+ .Change("<CLS_NAME>", st.GetID())
+ .Change("<PARAMS>", GenParameters(st.GetElements()))
+ .Change("<MEMBER_INIT>", GenStructureMemberInit(st.GetElements()))
+ .Change("<GETTER_SETTER>", GenStructureGetterSetter(st));
+ }
+
+ return code;
}
std::string CppGeneratorBase::GenStructureMemberInit(const Elements& elms) {
}
void CppGeneratorBase::InitUnitTypes(bool use_file) {
- AddUnitType("int", BaseType("int", ""));
- AddUnitType("bool", BaseType("bool", ""));
- AddUnitType("string", BaseType("string", ""));
- AddUnitType("bundle", BaseType("bundle", ""));
+ AddUnitType("int", BaseType("int", ""), "");
+ AddUnitType("bool", BaseType("bool", ""), "");
+ AddUnitType("string", BaseType("string", ""), "");
+ AddUnitType("bundle", BaseType("bundle", ""), "");
if (use_file)
- AddUnitType("file", BaseType("file", ""));
+ AddUnitType("file", BaseType("file", ""), "");
for (auto& block : GetDocument().GetBlocks()) {
if (block->GetType() == Block::TYPE_INTERFACE) {
auto& iface = static_cast<const Interface&>(*block);
+ for (const auto& e : iface.GetEnums()) {
+ std::string name = iface.GetID() + "." + e->GetID();
+ AddUnitType(name, BaseType(name, "", BaseType::UserType::ENUM),
+ iface.GetID());
+ }
+
for (const auto& decl : iface.GetDeclarations()) {
if (decl->GetMethodType() == Declaration::MethodType::DELEGATE) {
AddUnitType("delegate",
- BaseType(iface.GetID() + "::CallbackBase", "", true));
- AddUnitType(decl->GetID(), BaseType(decl->GetID(), "", true));
+ BaseType(iface.GetID() + "::CallbackBase", "", true),
+ iface.GetID());
+ AddUnitType(decl->GetID(), BaseType(decl->GetID(), "", true),
+ iface.GetID());
} else if (decl->GetMethodType() == Declaration::MethodType::SYNC) {
auto& type = decl->GetType();
- AddUnitType(type.GetFullName(false), BaseType(type));
+ AddUnitType(type.GetFullName(false), BaseType(type), iface.GetID());
}
for (const auto& param : decl->GetParameters()) {
auto& type = param->GetParameterType().GetBaseType();
- AddUnitType(type.GetFullName(false), BaseType(type));
+ if (type.IsEnumType()) continue;
+
+ AddUnitType(type.GetFullName(false), BaseType(type), iface.GetID());
}
}
} else if (block->GetType() == Block::TYPE_STRUCTURE) {
auto& st = static_cast<const Structure&>(*block);
- AddUnitType(st.GetID(), BaseType(st.GetID(), "", true));
+ AddUnitType(st.GetID(), BaseType(st.GetID(), "", true), st.GetID());
+ for (const auto& e : st.GetEnums()) {
+ std::string name = st.GetID() + "." + e->GetID();
+ AddUnitType(name, BaseType(name, "", BaseType::UserType::ENUM),
+ st.GetID());
+ }
+
for (const auto& elm : st.GetElements()) {
auto& type = elm->GetType();
- AddUnitType(type.GetFullName(false), BaseType(type));
+ if (type.IsEnumType()) continue;
+
+ AddUnitType(type.GetFullName(false), BaseType(type), st.GetID());
}
}
}
}
-void CppGeneratorBase::AddUnitType(std::string name, BaseType type) {
+std::string CppGeneratorBase::GetUnitTypeKey(std::string name,
+ const BaseType& type,
+ const std::string& block_id) {
+ if (type.GetMetaType() != nullptr) {
+ auto* meta_type = type.GetMetaType();
+ if (meta_type->IsEnumType() &&
+ meta_type->ToString().find('.') == std::string::npos) {
+ return type.ToString() + "<" + block_id + "." + meta_type->ToString() +
+ ">";
+ }
+ }
+
+ if (type.GetKeyType() != nullptr) {
+ std::string key = type.ToString() + "<";
+ auto* key_type = type.GetKeyType();
+ if (key_type->IsEnumType() &&
+ key_type->ToString().find('.') == std::string::npos)
+ key += block_id + "." + key_type->ToString();
+ else
+ key += key_type->ToString();
+
+ if (type.GetValueType() != nullptr) {
+ auto* value_type = type.GetValueType();
+ if (value_type->IsEnumType() &&
+ value_type->ToString().find('.') == std::string::npos)
+ key += ", " + block_id + "." + value_type->ToString();
+ else
+ key += ", " + value_type->ToString();
+ }
+
+ key += ">";
+ return key;
+ }
+
+ return name;
+}
+
+void CppGeneratorBase::AddUnitType(std::string name, BaseType type,
+ const std::string& block_id) {
if (type.GetMetaType() != nullptr) {
auto meta_type = type.GetMetaType();
- AddUnitType(meta_type->GetFullName(false), BaseType(*meta_type));
+ if (!meta_type->IsEnumType()) {
+ AddUnitType(meta_type->GetFullName(false), BaseType(*meta_type),
+ block_id);
+ }
}
if (type.GetKeyType() != nullptr) {
auto key_type = type.GetKeyType();
- AddUnitType(key_type->GetFullName(false), BaseType(*key_type));
+ if (!key_type->IsEnumType())
+ AddUnitType(key_type->GetFullName(false), BaseType(*key_type), block_id);
if (type.GetValueType() != nullptr) {
auto value_type = type.GetValueType();
- AddUnitType(value_type->GetFullName(false), BaseType(*value_type));
+ if (!value_type->IsEnumType()) {
+ AddUnitType(value_type->GetFullName(false), BaseType(*value_type),
+ block_id);
+ }
}
}
+ name = GetUnitTypeKey(name, type, block_id);
unit_types_[std::move(name)] = std::move(type);
}
}
std::string CppGeneratorBase::GetFullNameFromType(const BaseType& type) {
+ if (type.IsEnumType())
+ return "int";
+
if (IsDelegateType(type) ||
type.ToString().find("::CallbackBase") != std::string::npos)
return "delegate";
std::string CppGeneratorBase::GenUnitImplSerializer(const BaseType& type) {
std::string code;
- if (type.IsUserDefinedType() || type.GetMetaType() != nullptr ||
+ if (type.IsEnumType()) {
+ code = CB_UNIT_IMPL_SERIALIZER_ENUM;
+ } else if (type.IsUserDefinedType() || type.GetMetaType() != nullptr ||
type.GetKeyType() != nullptr) {
if (IsDelegateType(type)) {
code = CB_UNIT_IMPL_SERIALIZER_DELEGATE;
std::string CppGeneratorBase::GenUnitImplDeserializer(const BaseType& type) {
std::string code;
- if (type.IsUserDefinedType() || type.GetMetaType() != nullptr ||
+ if (type.IsEnumType()) {
+ code = ReplaceAll(CB_UNIT_IMPL_DESERIALIZER_ENUM)
+ .Change("<TYPE>", ConvertTypeToString(type));
+ } else if (type.IsUserDefinedType() || type.GetMetaType() != nullptr ||
type.GetKeyType() != nullptr) {
if (IsDelegateType(type)) {
code = ReplaceAll(CB_UNIT_IMPL_DESERIALIZER_DELEGATE)
}
std::string CppGeneratorBase::GetParcelType(const BaseType& type) {
+ if (type.IsEnumType())
+ return "int32";
+
return parcel_type_map_[type.ToString()];
}
return code;
}
+std::string CppGeneratorBase::GetClassNameFromEnumType(
+ const std::string& type) {
+ for (auto& block : GetDocument().GetBlocks()) {
+ for (const auto& e : block->GetEnums()) {
+ if (e->GetID() == type)
+ return block->GetID();
+ }
+ }
+
+ return {};
+}
+
+std::string CppGeneratorBase::GetEnumTypeString(const std::string& type,
+ bool use_underbar) {
+ std::string concatenated_char = use_underbar ? "_" : "::";
+ auto pos = type.find('.');
+ if (pos == std::string::npos) {
+ std::string cls_name = GetClassNameFromEnumType(type);
+ if (!cls_name.empty())
+ return cls_name + concatenated_char + type;
+
+ return type;
+ }
+
+ std::string block_id = type.substr(0, pos);
+ std::string type_id = type.substr(pos + 1, type.size() - (pos + 1));
+ return block_id + concatenated_char + type_id;
+}
+
+std::string CppGeneratorBase::GenEnumerations(const Enums& enums) {
+ std::string code;
+ for (const auto& e : enums) {
+ std::string enum_fields;
+ for (const auto& f : e->GetFields()) {
+ if (f->GetValue().empty())
+ enum_fields += f->GetID() + "," + NLine(1);
+ else
+ enum_fields += f->GetID() + " = " + f->GetValue() + "," + NLine(1);
+ }
+
+ code += ReplaceAll(CB_ENUM_BASE)
+ .Change("<NAME>", e->GetID())
+ .Change("<ENUMS>", enum_fields);
+ code += NLine(1);
+ }
+
+ return RemoveLine(code);
+}
+
} // namespace version2
} // namespace tidl