{"long", "long"}, {"string", "string"}, {"bool", "bool"},
{"list", "LinkedList"}, {"array", "List"}, {"float", "float"},
{"double", "double"}, {"bundle", "Bundle"}, {"void", "void"},
- {"file", "string"}};
+ {"file", "string"} , {"set", "HashSet"}, {"map", "Dictionary"}};
parcel_type_map_ = {{"char", "Byte"}, {"int", "Int"},
{"short", "Short"}, {"long", "Long"},
CB_PROPERTY, stream,
[&]() -> std::string { return ConvertTypeToString(i->GetType()); },
[&]() -> std::string { return i->GetID(); });
- if (i->GetType().ToString() == "bundle") {
- v.push_back(i->GetID() + " = " + "new Bundle()");
+ if (i->GetType().ToString() == "bundle" ||
+ i->GetType().ToString() == "map" ||
+ i->GetType().ToString() == "set") {
+ v.push_back(i->GetID() + " = " + "new " +
+ ConvertTypeToString(i->GetType()) + "()");
}
}
}
std::string CsGeneratorBase::GetSerializer(const Structure& st) {
+
std::string str(ReplaceAll(CB_SERIALIZER)
.Change("<TYPE>", st.GetID())
.Change("<BODY>", [&]() {
serializer_list_[ConvertTypeToString(type, st_id)] = type;
AddSerializerList(*type.GetMetaType(), st_id);
+ } else if (type.GetKeyType() != nullptr && type.GetValueType() != nullptr) {
+ if (type.GetValueType()->IsEnumType() && !st_id.empty()) {
+ BaseType new_type(type.ToString(), "", type.GetUserDefinedType());
+ BaseType* new_key = new BaseType(*(type.GetKeyType()));
+ BaseType* new_value = new BaseType(st_id + "." +
+ type.GetValueType()->ToString(), "", BaseType::UserType::ENUM);
+
+ new_type.SetKeyType(new_key);
+ new_type.SetValueType(new_value);
+ serializer_list_[ConvertTypeToString(type, st_id)] = new_type;
+ return;
+ }
+ serializer_list_[ConvertTypeToString(type, st_id)] = type;
+ }
+}
+
+std::string CsGeneratorBase::GenListWrite(const BaseType& type,
+ const std::string& type_str) {
+ std::string ret;
+ std::string type_string;
+ if (type.ToString() == "map") {
+ if (type.GetValueType()->IsEnumType())
+ type_string = "int";
+ else
+ type_string = ConvertTypeToString(*type.GetValueType());
+
+ ret = ReplaceAll(CB_LIST_MAP_WRITE)
+ .Change("<KEY_TYPE>", ConvertTypeToString(*type.GetKeyType()))
+ .Change("<VALUE_TYPE>", type_string);
+ } else if (type.ToString() == "set") {
+ ret = ReplaceAll(CB_LIST_SET_WRITE)
+ .Change("<VALUE_TYPE>", ConvertTypeToString(*type.GetMetaType()));
+ } else if (type.GetMetaType()->IsEnumType()) {
+ ret = ReplaceAll(CB_LIST_WRITE)
+ .Change("<VALUE_TYPE>", std::string("int"));
+ } else {
+ ret = ReplaceAll(CB_LIST_WRITE)
+ .Change("<VALUE_TYPE>", ConvertTypeToString(*type.GetMetaType()));
}
+
+ return ret;
}
+std::string CsGeneratorBase::GenListRead(const BaseType& type,
+ const std::string& type_str) {
+ std::string ret;
+
+ if (type.ToString() == "map") {
+ if (type.GetValueType()->IsEnumType()) {
+ ret = ReplaceAll(CB_LIST_MAP_READ)
+ .Change("<KEY_TYPE>", ConvertTypeToString(*type.GetKeyType()))
+ .Change("<TYPE_STRING>", std::string("int"))
+ .Change("<VALUE_TYPE>", type.GetValueType()->ToString());
+ } else {
+ ret = ReplaceAll(CB_LIST_MAP_READ)
+ .Change("<KEY_TYPE>", ConvertTypeToString(*type.GetKeyType()))
+ .Change("<TYPE_STRING>", ConvertTypeToString(*type.GetKeyType()))
+ .Change("<VALUE_TYPE>", ConvertTypeToString(*type.GetValueType()));
+ }
+ } else if (type.ToString() == "set") {
+ ret = ReplaceAll(CB_LIST_SET_READ)
+ .Change("<VALUE_TYPE>", ConvertTypeToString(*type.GetMetaType()));
+ } else if (type.GetMetaType()->IsEnumType()) {
+ std::string list_method;
+ if (type.ToString() == "list")
+ list_method = "Last";
+
+ ret = ReplaceAll(CB_LIST_READ)
+ .Change("<TYPE_STRING>", std::string("int"))
+ .Change("<LIST_METHOD>", list_method)
+ .Change("<VALUE_TYPE>", type.GetMetaType()->ToString());
+ } else {
+ std::string list_method;
+ if (type.ToString() == "list")
+ list_method = "Last";
+
+ ret = ReplaceAll(CB_LIST_READ)
+ .Change("<TYPE_STRING>", ConvertTypeToString(*type.GetMetaType()))
+ .Change("<LIST_METHOD>", list_method)
+ .Change("<VALUE_TYPE>", ConvertTypeToString(*type.GetMetaType()));
+ }
+
+ return ret;
+}
+
+std::string CsGeneratorBase::GetLengthName(const BaseType& type) {
+ if (type.ToString() == "list") {
+ return "length";
+ } else {
+ return "size";
+ }
+}
std::string CsGeneratorBase::GetListSerializer(const BaseType& type,
const std::string& type_str) {
- std::string str(ReplaceAll(CB_LIST_SERIALIZER)
+ std::string code;
+ code = ReplaceAll(CB_LIST_SERIALIZER)
.Change("<TYPE>", type_str)
- .Change("<BODY>", [&]() {
- std::string ret;
- auto* ptr = type.GetMetaType();
- if (ptr == nullptr) return ret;
-
- auto& mt = *ptr;
- std::string param_str;
- if (mt.GetUserDefinedType() == BaseType::UserType::ENUM)
- param_str = "int";
- else
- param_str = ConvertTypeToString(mt);
-
- ret += Tab(5) + "map.Write(index.ToString(), \"" +
- param_str + "\", i);" + NLine(1);
- return ret;
- }));
+ .Change("<LENGTH_NAME>", GetLengthName(type))
+ .Change("<WRITE_LIST>", GenListWrite(type, type_str));
- str += ReplaceAll(CB_LIST_DESERIALIZER)
- .Change("<TYPE>", type_str)
- .Change("<BODY>", [&]() {
- std::string ret;
- auto* ptr = type.GetMetaType();
- if (ptr == nullptr) return ret;
-
- auto& mt = *ptr;
- if (mt.GetUserDefinedType() == BaseType::UserType::ENUM) {
- ret += Tab(5) + "map.Read(index.ToString(), \"int\", out " +
- mt.ToString() + " p);" + NLine(1);
- } else {
- ret += Tab(5) + "map.Read(index.ToString(), \"" +
- ConvertTypeToString(mt) +
- "\", out "+ ConvertTypeToString(mt)
- + " p);" + NLine(1);
- }
- if (type.ToString() == "list")
- ret += Tab(5) + "param.AddLast(p);" + NLine(1);
- else
- ret += Tab(5) + "param.Add(p);" + NLine(1);
- return ret;
- });
+ code += ReplaceAll(CB_LIST_DESERIALIZER)
+ .Change("<TYPE>", type_str)
+ .Change("<LENGTH_NAME>", GetLengthName(type))
+ .Change("<READ_LIST>", GenListRead(type, type_str));
- return str;
+ return code;
}
void CsGeneratorBase::GenListSerializer(std::ofstream& stream) {
return type_map_[type.ToString()] + "<" +
ConvertTypeToString(*(type.GetMetaType()), st_id) + ">";
+ if (type.GetKeyType() != nullptr && type.GetValueType() != nullptr) {
+ return type_map_[type.ToString()] + "<" +
+ ConvertTypeToString(*(type.GetKeyType()), st_id) + ", " +
+ ConvertTypeToString(*(type.GetValueType()), st_id) + ">";
+ }
+
return type_map_[type.ToString()];
}
const BaseType& base_type) {
std::string ret;
- if (!base_type.IsUserDefinedType() && base_type.GetMetaType() == nullptr) {
+ if (!base_type.IsUserDefinedType() && base_type.GetMetaType() == nullptr
+ && base_type.GetKeyType() == nullptr) {
ret = "unit.parcel.Write" +
ConvertTypeToParcelType(base_type.ToString()) + "(value);";
} else if (base_type.GetUserDefinedType() == BaseType::UserType::ENUM) {
const std::string& type_str) {
std::string ret;
- if (!base_type.IsUserDefinedType() && base_type.GetMetaType() == nullptr) {
+ if (!base_type.IsUserDefinedType() && base_type.GetMetaType() == nullptr
+ && base_type.GetKeyType() == nullptr) {
ret = "value = unit.parcel.Read" +
ConvertTypeToParcelType(base_type.ToString()) + "();";
} else if (base_type.GetUserDefinedType() == BaseType::UserType::ENUM) {
}
}
}
+
std::string CsGeneratorBase::GenUnitMapReadWrite(const Interface& iface) {
std::unordered_map<std::string, BaseType> types;
BaseType bI("int", "int");
}
)__cs_cb";
+constexpr const char CB_LIST_MAP_WRITE[] =
+R"__cs_cb( map.Write("key-" + index.ToString(), "<KEY_TYPE>", i.Key);
+ map.Write("value-" + index.ToString(), "<VALUE_TYPE>", i.Value);)__cs_cb";
+
+constexpr const char CB_LIST_SET_WRITE[] =
+R"__cs_cb( map.Write("key-" + index.ToString(), "<VALUE_TYPE>", i);)__cs_cb";
+
+constexpr const char CB_LIST_WRITE[] =
+R"__cs_cb( map.Write(index.ToString(), "<VALUE_TYPE>", i);)__cs_cb";
+
+constexpr const char CB_LIST_MAP_READ[] =
+R"__cs_cb( map.Read("key-" + index.ToString(), "<KEY_TYPE>", out <KEY_TYPE> key);
+ map.Read("value-" + index.ToString(), "<TYPE_STRING>", out <VALUE_TYPE> value);
+ param[key] = value;)__cs_cb";
+
+constexpr const char CB_LIST_SET_READ[] =
+R"__cs_cb( map.Read("key-" + index.ToString(), "<VALUE_TYPE>", out <VALUE_TYPE> value);
+ param.Add(value);)__cs_cb";
+
+constexpr const char CB_LIST_READ[] =
+R"__cs_cb( map.Read("key-" + index.ToString(), "<TYPE_STRING>", out <VALUE_TYPE> value);
+ param.Add<LIST_METHOD>(value);)__cs_cb";
+
const char CB_LIST_SERIALIZER[] =
R"__cs_cb(
private static void Serialize(Parcel h, <TYPE> param)
{
UnitMap map = new UnitMap();
-
- map.Write("length", "int", param.Count & int.MaxValue);
+ map.Write("<LENGTH_NAME>", "int", param.Count & int.MaxValue);
int index = 0;
foreach (var i in param)
{
-<BODY>
+<WRITE_LIST>
index++;
}
map.Serialize(h);
{
UnitMap map = new UnitMap();
map.Deserialize(h);
- map.Read("length", "int", out int index);
- for (int i = 0; index < i; i++)
+ map.Read("<LENGTH_NAME>", "int", out int length);
+ for (int index = 0; index < length; index++)
{
-<BODY>
+<READ_LIST>
}
}
)__cs_cb";