aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSundong Ahn <sundongahn@google.com>2019-02-25 23:15:39 -0800
committerandroid-build-merger <android-build-merger@google.com>2019-02-25 23:15:39 -0800
commitb96b0a28d2e2b5ab31e226b284e01cfb24460699 (patch)
treed83556619daa7f56352ebd36791485d7dbd87010
parentff3868fb181a7bcbb0aeefeea1e0558d8c3d3d47 (diff)
parentd177c0803233ebe8b973ee0bd8cdf359908a0c78 (diff)
downloadxsdc-b96b0a28d2e2b5ab31e226b284e01cfb24460699.tar.gz
Merge "Allow to get first element" am: ee5cef6610 am: ce4e893ccc
am: d177c08032 Change-Id: Ie5c07a9551f99b096da701ff98bd4f278891731d
-rw-r--r--src/com/android/xsdc/cpp/CppCodeGenerator.java64
-rw-r--r--src/com/android/xsdc/cpp/CppSimpleType.java10
-rw-r--r--tests/main.cpp34
3 files changed, 70 insertions, 38 deletions
diff --git a/src/com/android/xsdc/cpp/CppCodeGenerator.java b/src/com/android/xsdc/cpp/CppCodeGenerator.java
index 121ac48..917f370 100644
--- a/src/com/android/xsdc/cpp/CppCodeGenerator.java
+++ b/src/com/android/xsdc/cpp/CppCodeGenerator.java
@@ -99,9 +99,10 @@ public class CppCodeGenerator {
headerFile.printf("#define %s_H\n\n", fileName.toUpperCase());
headerFile.printf("#include <libxml/parser.h>\n");
headerFile.printf("#include <libxml/xinclude.h>\n\n");
+ headerFile.printf("#include <map>\n");
+ headerFile.printf("#include <optional>\n");
headerFile.printf("#include <string>\n");
headerFile.printf("#include <vector>\n\n");
- headerFile.printf("#include <map>\n\n");
cppFile.printf("#define LOG_TAG \"%s\"\n\n", fileName);
cppFile.printf("#include <android/log.h>\n");
@@ -238,7 +239,6 @@ public class CppCodeGenerator {
CppType type = elementTypes.get(i);
XsdElement element = complexType.getElements().get(i);
XsdElement elementValue = resolveElement(element);
- //String typeName = String.format("std::vector<%s>", type.getName());
String typeName = element.isMultiple() || type instanceof CppComplexType ?
String.format("std::vector<%s>", type.getName()) : type.getName();
headerFile.printf("%s %s;\n", typeName,
@@ -263,16 +263,17 @@ public class CppCodeGenerator {
XsdElement elementValue = resolveElement(element);
printGetterAndSetter(nameScope + name, type,
Utils.toVariableName(getElementName(elementValue)),
- type instanceof CppComplexType ? true : element.isMultiple());
+ type instanceof CppComplexType ? true : element.isMultiple(),
+ type instanceof CppComplexType ? false : ((CppSimpleType)type).isList());
}
for (int i = 0; i < attributeTypes.size(); ++i) {
CppType type = attributeTypes.get(i);
XsdAttribute attribute = resolveAttribute(complexType.getAttributes().get(i));
printGetterAndSetter(nameScope + name, type,
- Utils.toVariableName(attribute.getName()), false);
+ Utils.toVariableName(attribute.getName()), false, false);
}
if (valueType != null) {
- printGetterAndSetter(nameScope + name, valueType, "value", false);
+ printGetterAndSetter(nameScope + name, valueType, "value", false, false);
}
printParser(name, nameScope, complexType);
@@ -360,7 +361,7 @@ public class CppCodeGenerator {
}
private void printGetterAndSetter(String name, CppType type, String variableName,
- boolean isMultiple) {
+ boolean isMultiple, boolean isMultipleType) {
String typeName = isMultiple ? String.format("std::vector<%s>", type.getName())
: type.getName();
@@ -371,6 +372,36 @@ public class CppCodeGenerator {
+ "return %s;\n}\n",
typeName, name, Utils.capitalize(variableName), variableName);
+ if (isMultiple || isMultipleType) {
+ String elementTypeName = type instanceof CppComplexType ? type.getName() :
+ ((CppSimpleType)type).getTypeName();
+ if (elementTypeName.equals("bool")) {
+ headerFile.printf("%s getFirst%s();\n",
+ elementTypeName, Utils.capitalize(variableName));
+ cppFile.println();
+ cppFile.printf("%s %s::getFirst%s() {\n"
+ + "if (%s.empty()) {\n"
+ + "return false;\n"
+ + "}\n"
+ + "return %s[0];\n"
+ + "}\n",
+ elementTypeName, name, Utils.capitalize(variableName), variableName,
+ variableName);
+ } else {
+ headerFile.printf("%s* getFirst%s();\n",
+ elementTypeName, Utils.capitalize(variableName));
+ cppFile.println();
+ cppFile.printf("%s* %s::getFirst%s() {\n"
+ + "if (%s.empty()) {\n"
+ + "return nullptr;\n"
+ + "}\n"
+ + "return &%s[0];\n"
+ + "}\n",
+ elementTypeName, name, Utils.capitalize(variableName), variableName,
+ variableName);
+ }
+ }
+
if (isMultiple) return;
headerFile.printf("void set%s(%s);\n", Utils.capitalize(variableName), typeName);
cppFile.println();
@@ -414,21 +445,19 @@ public class CppCodeGenerator {
String typeName = cppType instanceof CppSimpleType ? cppType.getName() :
Utils.toClassName(cppType.getName());
- headerFile.printf("std::vector<%s> read(const char* configFile);\n\n",
- typeName);
- cppFile.printf("std::vector<%s> read(const char* configFile) {\n", typeName);
- cppFile.printf("std::vector<%s> config;\n"
- + "auto doc = make_xmlUnique(xmlParseFile(configFile));\n"
+ headerFile.printf("std::optional<%s> read(const char* configFile);\n\n", typeName);
+ cppFile.printf("std::optional<%s> read(const char* configFile) {\n", typeName);
+ cppFile.printf("auto doc = make_xmlUnique(xmlParseFile(configFile));\n"
+ "if (doc == nullptr) {\n"
- + "return config;\n"
+ + "return std::nullopt;\n"
+ "}\n"
+ "xmlNodePtr child = xmlDocGetRootElement(doc.get());\n"
+ "if (child == NULL) {\n"
- + "return config;\n"
+ + "return std::nullopt;\n"
+ "}\n\n"
+ "if (!xmlStrcmp(child->name, reinterpret_cast<const xmlChar*>"
+ "(\"%s\"))) {\n",
- typeName, elementName);
+ elementName);
if (cppType instanceof CppSimpleType) {
cppFile.printf("%s value = getXmlAttribute(child, \"%s\");\n",
@@ -436,12 +465,9 @@ public class CppCodeGenerator {
} else {
cppFile.printf(cppType.getParsingExpression());
}
-
- cppFile.printf("config.push_back(std::move(value));\n"
- + "}\n",
- Utils.capitalize(VariableName));
+ cppFile.printf("return value;\n}\n");
}
- cppFile.printf("return config;\n");
+ cppFile.printf("return std::nullopt;\n");
cppFile.printf("}\n\n");
}
diff --git a/src/com/android/xsdc/cpp/CppSimpleType.java b/src/com/android/xsdc/cpp/CppSimpleType.java
index 2fc4f30..f59aec5 100644
--- a/src/com/android/xsdc/cpp/CppSimpleType.java
+++ b/src/com/android/xsdc/cpp/CppSimpleType.java
@@ -18,13 +18,15 @@ package com.android.xsdc.cpp;
class CppSimpleType implements CppType {
final private String name;
+ final private String fullName;
final private String rawParsingExpression;
final private boolean list;
CppSimpleType(String name, String rawParsingExpression, boolean list) {
this.rawParsingExpression = rawParsingExpression;
this.list = list;
- this.name = list ? String.format("std::vector<%s>", name) : name;
+ this.name = name;
+ this.fullName = list ? String.format("std::vector<%s>", name) : name;
}
boolean isList() {
@@ -36,9 +38,13 @@ class CppSimpleType implements CppType {
return new CppSimpleType(name, rawParsingExpression, true);
}
+ public String getTypeName() {
+ return name;
+ }
+
@Override
public String getName() {
- return name;
+ return fullName;
}
@Override
diff --git a/tests/main.cpp b/tests/main.cpp
index 75e1d7a..a3c6357 100644
--- a/tests/main.cpp
+++ b/tests/main.cpp
@@ -38,24 +38,24 @@ public:
TEST_F(XmlTest, Simpletype) {
using namespace simple::type;
string file_name = "resources/simple_type.xml";
- SimpleTypes simple = read(file_name.c_str())[0];
+ SimpleTypes simple = *read(file_name.c_str());
for (int i = 0; i < simple.getListInt().size(); ++i) {
EXPECT_EQ(simple.getListInt()[i], i + 1);
}
- EXPECT_EQ(simple.getUnionTest()[0], "100");
+ EXPECT_EQ(*simple.getFirstUnionTest(), "100");
EXPECT_EQ(simple.getYesOrNo(), EnumType::YES);
}
TEST_F(XmlTest, Predefinedtypes) {
using namespace predefined::types;
- Types type = read("resources/predefined_types.xml")[0];
+ Types type = *read("resources/predefined_types.xml");
- StringTypes stringTypes = type.getStringTypes()[0];
- DateTypes dateTypes = type.getDateTypes()[0];
- NumericTypes numericTypes = type.getNumericTypes()[0];
- MiscTypes miscTypes = type.getMiscTypes()[0];
- ListPrimitiveTypes listPrimitiveTypes = type.getListPrimitiveTypes()[0];
+ StringTypes stringTypes = *type.getFirstStringTypes();
+ DateTypes dateTypes = *type.getFirstDateTypes();
+ NumericTypes numericTypes = *type.getFirstNumericTypes();
+ MiscTypes miscTypes = *type.getFirstMiscTypes();
+ ListPrimitiveTypes listPrimitiveTypes = *type.getFirstListPrimitiveTypes();
EXPECT_EQ(stringTypes.getString(), "abcd");
EXPECT_EQ(stringTypes.getToken(), "abcd");
@@ -127,10 +127,10 @@ TEST_F(XmlTest, Predefinedtypes) {
TEST_F(XmlTest, Nestedtype) {
using namespace nested::type;
- Employee employee = read("resources/nested_type.xml")[0];
+ Employee employee = *read("resources/nested_type.xml");
- Employee::Address address = employee.getAddress()[0];
- Employee::Address::Extra extra = address.getExtra()[0];
+ Employee::Address address = *employee.getFirstAddress();
+ Employee::Address::Extra extra = *address.getFirstExtra();
EXPECT_EQ((int)employee.getId(), 1);
EXPECT_EQ(employee.getName(), "Peter");
@@ -143,7 +143,7 @@ TEST_F(XmlTest, Nestedtype) {
TEST_F(XmlTest, Purchasesimple) {
using namespace purchase::simple;
- PurchaseOrderType orderType = read("resources/purchase_simple.xml")[0];
+ PurchaseOrderType orderType = *read("resources/purchase_simple.xml");
EXPECT_EQ(orderType.getOrderDate(), "1900-01-01");
@@ -170,7 +170,7 @@ TEST_F(XmlTest, Purchasesimple) {
TEST_F(XmlTest, Reference) {
using namespace reference;
- Class _class = read("resources/reference.xml")[0];
+ Class _class = *read("resources/reference.xml");
EXPECT_EQ(_class.getStudent()[0], "Sam");
EXPECT_EQ(_class.getStudent()[1], "Paul");
@@ -179,10 +179,10 @@ TEST_F(XmlTest, Reference) {
TEST_F(XmlTest, Simplecomplexcontent) {
using namespace simple::complex::content;
- Person person = read("resources/simple_complex_content.xml")[0];
- USAddressP uSAddressP = person.getUSAddressP()[0];
- KRAddress kRAddress = person.getKRAddress()[0];
- SubAddress subAddress = person.getSubAddress()[0];
+ Person person = *read("resources/simple_complex_content.xml");
+ USAddressP uSAddressP = *person.getFirstUSAddressP();
+ KRAddress kRAddress = *person.getFirstKRAddress();
+ SubAddress subAddress = *person.getFirstSubAddress();
EXPECT_EQ(person.getName(), "Petr");