pass all scalar values through a single method v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Tue, 01 Jun 2021 19:25:12 +0200
branchv_0
changeset 31 273faff8b848
parent 30 f5ac2d29eeb4
child 32 82b7f6e41a65
pass all scalar values through a single method
src/XMLDocumentConstructor.h
--- a/src/XMLDocumentConstructor.h	Sun May 30 20:12:05 2021 +0200
+++ b/src/XMLDocumentConstructor.h	Tue Jun 01 19:25:12 2021 +0200
@@ -38,8 +38,16 @@
 	xmlpp::DomParser* parser = nullptr;
 	XMLNameCodec nameCodec;
 
+	enum class Mode {
+		ROOT,
+		SEQUENCE,
+		MAPPING,
+		MAP_KEY
+	};
+
 	std::string rootName = "cbor";
 	xmlpp::Element* current;
+	std::vector<Mode> mode;
 
 	cbor_callbacks callbacks = cbor_empty_callbacks;
 
@@ -58,6 +66,13 @@
 		return current->get_parent() == nullptr ? current : current->get_parent();
 	}
 
+	void appendScalarValue(Glib::ustring value, Glib::ustring cborType, bool isNull = false) {
+		// TODO: different behavior depending on mode
+		xmlpp::Element* element = current->add_child("scalar");
+		element->add_child_text(value);
+		element->set_attribute("type", cborType);
+	}
+
 public:
 
 	XMLDocumentConstructor(std::istream* input, xmlpp::DomParser* parser) : input(input), parser(parser) {
@@ -67,75 +82,76 @@
 
 		callbacks.array_start = [](void* context, size_t size) {
 			CBOR_CALLBACK_START
+			// TODO: implement
 			xmlpp::Element* element = instance->current->add_child("array-start");
+			element->set_attribute("size", std::to_string(size));
 			CBOR_CALLBACK_END
 		};
 
 		callbacks.boolean = [](void* context, bool value) {
 			CBOR_CALLBACK_START
-			xmlpp::Element* element = instance->current->add_child("boolean");
-			element->add_child_text(value ? "true" : "false");
+			instance->appendScalarValue(value ? "true" : "false", "boolean");
 			CBOR_CALLBACK_END
 		};
 
 		callbacks.byte_string = [](void* context, cbor_data value, uint64_t size) {
 			CBOR_CALLBACK_START
-			xmlpp::Element* element = instance->current->add_child("byte-string");
-			element->set_attribute("size", std::to_string(size));
 			std::stringstream hex;
 			hex << std::hex << std::setfill('0') << std::setw(2);
 			for (uint64_t i = 0; i < size; i++) hex << (int) value[i];
-			element->add_child_text(hex.str());
+			instance->appendScalarValue(hex.str(), "byte-string");
 			CBOR_CALLBACK_END
 		};
 
 		callbacks.byte_string_start = [](void* context) {
 			CBOR_CALLBACK_START
+			// TODO: implement
 			xmlpp::Element* element = instance->current->add_child("byte-string-start");
 			CBOR_CALLBACK_END
 		};
 
 		callbacks.float2 = [](void* context, float value) {
 			CBOR_CALLBACK_START
-			xmlpp::Element* element = instance->current->add_child("float2");
-			element->add_child_text(std::to_string(value));
+			instance->appendScalarValue(std::to_string(value), "float2");
 			CBOR_CALLBACK_END
 		};
 
 		callbacks.float4 = [](void* context, float value) {
 			CBOR_CALLBACK_START
-			xmlpp::Element* element = instance->current->add_child("float4");
-			element->add_child_text(std::to_string(value));
+			instance->appendScalarValue(std::to_string(value), "float4");
 			CBOR_CALLBACK_END
 		};
 
 		callbacks.float8 = [](void* context, double value) {
 			CBOR_CALLBACK_START
-			xmlpp::Element* element = instance->current->add_child("float8");
-			element->add_child_text(std::to_string(value));
+			instance->appendScalarValue(std::to_string(value), "float8");
 			CBOR_CALLBACK_END
 		};
 
 		callbacks.indef_array_start = [](void* context) {
 			CBOR_CALLBACK_START
+			// TODO: implement
 			xmlpp::Element* element = instance->current->add_child("array-start");
 			CBOR_CALLBACK_END
 		};
 
 		callbacks.indef_map_start = [](void* context) {
 			CBOR_CALLBACK_START
+			// TODO: implement
 			xmlpp::Element* element = instance->current->add_child("map-start");
 			CBOR_CALLBACK_END
 		};
 
 		callbacks.indef_break = [](void* context) {
 			CBOR_CALLBACK_START
+			// TODO: implement
 			xmlpp::Element* element = instance->current->add_child("indef-break");
 			CBOR_CALLBACK_END
 		};
 
 		callbacks.map_start = [](void* context, size_t size) {
 			CBOR_CALLBACK_START
+			// TODO: implement
 			xmlpp::Element* element = instance->current->add_child("map-start");
 			element->set_attribute("size", std::to_string(size));
 			CBOR_CALLBACK_END
@@ -143,54 +159,50 @@
 
 		callbacks.negint8 = [](void* context, uint8_t value) {
 			CBOR_CALLBACK_START
-			xmlpp::Element* element = instance->current->add_child("negative-int8");
-			element->add_child_text(std::to_string(value));
+			instance->appendScalarValue(std::to_string(value), "negative-int8");
 			CBOR_CALLBACK_END
 		};
 
 		callbacks.negint16 = [](void* context, uint16_t value) {
 			CBOR_CALLBACK_START
-			xmlpp::Element* element = instance->current->add_child("negative-int16");
-			element->add_child_text(std::to_string(value));
+			instance->appendScalarValue(std::to_string(value), "negative-int16");
 			CBOR_CALLBACK_END
 		};
 
 		callbacks.negint32 = [](void* context, uint32_t value) {
 			CBOR_CALLBACK_START
-			xmlpp::Element* element = instance->current->add_child("negative-int32");
-			element->add_child_text(std::to_string(value));
+			instance->appendScalarValue(std::to_string(value), "negative-int32");
 			CBOR_CALLBACK_END
 		};
 
 		callbacks.negint64 = [](void* context, uint64_t value) {
 			CBOR_CALLBACK_START
-			xmlpp::Element* element = instance->current->add_child("negative-int64");
-			element->add_child_text(std::to_string(value));
+			instance->appendScalarValue(std::to_string(value), "negative-int64");
 			CBOR_CALLBACK_END
 		};
 
 		callbacks.null = [](void* context) {
 			CBOR_CALLBACK_START
-			xmlpp::Element* element = instance->current->add_child("null");
+			instance->appendScalarValue("", "null", true);
 			CBOR_CALLBACK_END
 		};
 
 		callbacks.string = [](void* context, cbor_data value, uint64_t size) {
 			CBOR_CALLBACK_START
-			xmlpp::Element* element = instance->current->add_child("string");
-			element->set_attribute("size", std::to_string(size));
-			element->add_child_text(c2x(value, size));
+			instance->appendScalarValue(c2x(value, size), "string");
 			CBOR_CALLBACK_END
 		};
 
 		callbacks.string_start = [](void* context) {
 			CBOR_CALLBACK_START
+			// TODO: implement
 			xmlpp::Element* element = instance->current->add_child("string-start");
 			CBOR_CALLBACK_END
 		};
 
 		callbacks.tag = [](void* context, uint64_t value) {
 			CBOR_CALLBACK_START
+			// TODO: implement
 			xmlpp::Element* element = instance->current->add_child("tag");
 			element->add_child_text(std::to_string(value));
 			CBOR_CALLBACK_END
@@ -198,35 +210,31 @@
 
 		callbacks.uint8 = [](void* context, uint8_t value) {
 			CBOR_CALLBACK_START
-			xmlpp::Element* element = instance->current->add_child("uint8");
-			element->add_child_text(std::to_string(value));
+			instance->appendScalarValue(std::to_string(value), "uint8");
 			CBOR_CALLBACK_END
 		};
 
 		callbacks.uint16 = [](void* context, uint16_t value) {
 			CBOR_CALLBACK_START
-			xmlpp::Element* element = instance->current->add_child("uint16");
-			element->add_child_text(std::to_string(value));
+			instance->appendScalarValue(std::to_string(value), "uint16");
 			CBOR_CALLBACK_END
 		};
 
 		callbacks.uint32 = [](void* context, uint32_t value) {
 			CBOR_CALLBACK_START
-			xmlpp::Element* element = instance->current->add_child("uint32");
-			element->add_child_text(std::to_string(value));
+			instance->appendScalarValue(std::to_string(value), "uint32");
 			CBOR_CALLBACK_END
 		};
 
 		callbacks.uint64 = [](void* context, uint64_t value) {
 			CBOR_CALLBACK_START
-			xmlpp::Element* element = instance->current->add_child("uint64");
-			element->add_child_text(std::to_string(value));
+			instance->appendScalarValue(std::to_string(value), "uint64");
 			CBOR_CALLBACK_END
 		};
 
 		callbacks.undefined = [](void* context) {
 			CBOR_CALLBACK_START
-			xmlpp::Element* element = instance->current->add_child("undefined");
+			instance->appendScalarValue("undefined", "undefined", true); // FIXME: throw exception?
 			CBOR_CALLBACK_END
 		};