add NUMERIC_CALLBACK macro v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 05 Jun 2021 21:06:34 +0200
branchv_0
changeset 34 0b0d214c3cb2
parent 33 57cb51fb5212
child 35 5bfd1d389d5f
add NUMERIC_CALLBACK macro
src/XMLDocumentConstructor.h
--- a/src/XMLDocumentConstructor.h	Sat Jun 05 21:01:50 2021 +0200
+++ b/src/XMLDocumentConstructor.h	Sat Jun 05 21:06:34 2021 +0200
@@ -152,6 +152,7 @@
 
 #define CBOR_CALLBACK_START if (XMLDocumentConstructor* instance = static_cast<XMLDocumentConstructor*> (context)) {
 #define CBOR_CALLBACK_END } else throw relpipe::writer::RelpipeWriterException(L"Invalid context in the CBOR callback.");
+#define NUMERIC_CALLBACK(type, name) [](void* context, type value) { CBOR_CALLBACK_START instance->appendScalarValue(std::to_string(value), name); CBOR_CALLBACK_END }
 
 		callbacks.array_start = [](void* context, size_t size) {
 			CBOR_CALLBACK_START
@@ -183,23 +184,9 @@
 			CBOR_CALLBACK_END
 		};
 
-		callbacks.float2 = [](void* context, float value) {
-			CBOR_CALLBACK_START
-			instance->appendScalarValue(std::to_string(value), "float2");
-			CBOR_CALLBACK_END
-		};
-
-		callbacks.float4 = [](void* context, float value) {
-			CBOR_CALLBACK_START
-			instance->appendScalarValue(std::to_string(value), "float4");
-			CBOR_CALLBACK_END
-		};
-
-		callbacks.float8 = [](void* context, double value) {
-			CBOR_CALLBACK_START
-			instance->appendScalarValue(std::to_string(value), "float8");
-			CBOR_CALLBACK_END
-		};
+		callbacks.float2 = NUMERIC_CALLBACK(float, "float2");
+		callbacks.float4 = NUMERIC_CALLBACK(float, "float4");
+		callbacks.float8 = NUMERIC_CALLBACK(double, "float8");
 
 		callbacks.indef_array_start = [](void* context) {
 			CBOR_CALLBACK_START
@@ -225,29 +212,10 @@
 			CBOR_CALLBACK_END
 		};
 
-		callbacks.negint8 = [](void* context, uint8_t value) {
-			CBOR_CALLBACK_START
-			instance->appendScalarValue(std::to_string(value), "negative-int8");
-			CBOR_CALLBACK_END
-		};
-
-		callbacks.negint16 = [](void* context, uint16_t value) {
-			CBOR_CALLBACK_START
-			instance->appendScalarValue(std::to_string(value), "negative-int16");
-			CBOR_CALLBACK_END
-		};
-
-		callbacks.negint32 = [](void* context, uint32_t value) {
-			CBOR_CALLBACK_START
-			instance->appendScalarValue(std::to_string(value), "negative-int32");
-			CBOR_CALLBACK_END
-		};
-
-		callbacks.negint64 = [](void* context, uint64_t value) {
-			CBOR_CALLBACK_START
-			instance->appendScalarValue(std::to_string(value), "negative-int64");
-			CBOR_CALLBACK_END
-		};
+		callbacks.negint8 = NUMERIC_CALLBACK(uint8_t, "negative-int8");
+		callbacks.negint16 = NUMERIC_CALLBACK(uint16_t, "negative-int16");
+		callbacks.negint32 = NUMERIC_CALLBACK(uint32_t, "negative-int32");
+		callbacks.negint64 = NUMERIC_CALLBACK(uint64_t, "negative-int64");
 
 		callbacks.null = [](void* context) {
 			CBOR_CALLBACK_START
@@ -278,29 +246,10 @@
 			CBOR_CALLBACK_END
 		};
 
-		callbacks.uint8 = [](void* context, uint8_t value) {
-			CBOR_CALLBACK_START
-			instance->appendScalarValue(std::to_string(value), "uint8");
-			CBOR_CALLBACK_END
-		};
-
-		callbacks.uint16 = [](void* context, uint16_t value) {
-			CBOR_CALLBACK_START
-			instance->appendScalarValue(std::to_string(value), "uint16");
-			CBOR_CALLBACK_END
-		};
-
-		callbacks.uint32 = [](void* context, uint32_t value) {
-			CBOR_CALLBACK_START
-			instance->appendScalarValue(std::to_string(value), "uint32");
-			CBOR_CALLBACK_END
-		};
-
-		callbacks.uint64 = [](void* context, uint64_t value) {
-			CBOR_CALLBACK_START
-			instance->appendScalarValue(std::to_string(value), "uint64");
-			CBOR_CALLBACK_END
-		};
+		callbacks.uint8 = NUMERIC_CALLBACK(uint8_t, "uint8");
+		callbacks.uint16 = NUMERIC_CALLBACK(uint16_t, "uint16");
+		callbacks.uint32 = NUMERIC_CALLBACK(uint32_t, "uint32");
+		callbacks.uint64 = NUMERIC_CALLBACK(uint64_t, "uint64");
 
 		callbacks.undefined = [](void* context) {
 			CBOR_CALLBACK_START