summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2018-09-25 08:32:16 +0200
committerDaniel Friesel <derf@finalrewind.org>2018-09-25 08:32:16 +0200
commit4853a5593cc6147e1851e537b46a5128792939a5 (patch)
tree065b3cff26da895e98f5b2052c2883b2e6503587 /src
parentb682dc0cde9c7054174c083f44d6739f9648bd66 (diff)
Your Templates are Bad and You Should Feel Bad
Diffstat (limited to 'src')
-rw-r--r--src/app/prototest/main.cc10
-rw-r--r--src/os/object/xdrstream.cc33
2 files changed, 40 insertions, 3 deletions
diff --git a/src/app/prototest/main.cc b/src/app/prototest/main.cc
index 8613bea..c17b69d 100644
--- a/src/app/prototest/main.cc
+++ b/src/app/prototest/main.cc
@@ -6,7 +6,7 @@
#include "object/stdbuf.h"
#include "object/xdrstream.h"
-char buf[128];
+char buf[256];
void loop(void)
{
@@ -30,10 +30,16 @@ void loop(void)
BufferOutput<XDRStream> foostream(buf);
+ char test[] = "Obai World!";
+
foostream << 123 << 0 << 12345678;
+ foostream.setNextArrayLen(3);
+ foostream << fixed << "Hai";
+ foostream.setNextArrayLen(sizeof(test));
+ foostream << variable << (char const *)test;
kout << "foostream is " << hex;
- for (unsigned int i = 0; i < 32; i += 4) {
+ for (unsigned int i = 0; i < 64; i += 4) {
kout << (unsigned char)buf[i] << (unsigned char)buf[i+1];
kout << (unsigned char)buf[i+2] << (unsigned char)buf[i+3] << " ";
}
diff --git a/src/os/object/xdrstream.cc b/src/os/object/xdrstream.cc
index eb5ca87..d7a5ace 100644
--- a/src/os/object/xdrstream.cc
+++ b/src/os/object/xdrstream.cc
@@ -69,6 +69,9 @@ XDRStream & XDRStream::operator<<(int64_t number)
}
XDRStream & XDRStream::operator<<(char const *data){
+ if (!is_fixed_length) {
+ *this << next_array_len;
+ }
uint32_t i;
for (i = 0; i < next_array_len; i++) {
put(data[i]);
@@ -79,6 +82,21 @@ XDRStream & XDRStream::operator<<(char const *data){
return *this;
}
+template<int N>
+XDRStream & XDRStream::operator<<(char const (&data)[N]){
+ if (!is_fixed_length) {
+ *this << N;
+ }
+ uint32_t i;
+ for (i = 0; i < N; i++) {
+ put(data[i]);
+ }
+ while ((i++) % 4 != 0){
+ put('\0');
+ }
+ return *this;
+}
+
XDRStream & XDRStream::operator<<(XDRStream & (*fkt) (XDRStream &))
{
return fkt(*this);
@@ -99,8 +117,21 @@ XDRStream & term(XDRStream & os)
return os;
}
+template<int N>
XDRStream & opaque(XDRStream & os)
{
- os.setNextArrayLen(3);
+ os.setNextArrayLen(N);
+ return os;
+}
+
+XDRStream & fixed(XDRStream & os)
+{
+ os.setFixedLength();
+ return os;
+}
+
+XDRStream & variable(XDRStream & os)
+{
+ os.setVariableLength();
return os;
}