avro-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From th...@apache.org
Subject svn commit: r1185027 [4/4] - in /avro/trunk: ./ lang/c++/ lang/c++/api/ lang/c++/api/buffer/ lang/c++/api/buffer/detail/ lang/c++/examples/
Date Mon, 17 Oct 2011 07:14:14 GMT
Added: avro/trunk/lang/c++/examples/resolving.cc
URL: http://svn.apache.org/viewvc/avro/trunk/lang/c%2B%2B/examples/resolving.cc?rev=1185027&view=auto
==============================================================================
--- avro/trunk/lang/c++/examples/resolving.cc (added)
+++ avro/trunk/lang/c++/examples/resolving.cc Mon Oct 17 07:14:12 2011
@@ -0,0 +1,45 @@
+#include <fstream>
+
+#include "cpx.hh"
+#include "imaginary.hh"
+
+#include "avro/Compiler.hh"
+#include "avro/Encoder.hh"
+#include "avro/Decoder.hh"
+#include "avro/Specific.hh"
+#include "avro/Generic.hh"
+
+
+
+avro::ValidSchema load(const char* filename)
+{
+    std::ifstream ifs(filename);
+    avro::ValidSchema result;
+    avro::compileJsonSchema(ifs, result);
+    return result;
+}
+
+int
+main()
+{
+    avro::ValidSchema cpxSchema = load("cpx.json");
+    avro::ValidSchema imaginarySchema = load("imaginary.json");
+
+    std::auto_ptr<avro::OutputStream> out = avro::memoryOutputStream();
+    avro::EncoderPtr e = avro::binaryEncoder();
+    e->init(*out);
+    c::cpx c1;
+    c1.re = 100.23;
+    c1.im = 105.77;
+    avro::encode(*e, c1);
+
+    std::auto_ptr<avro::InputStream> in = avro::memoryInputStream(*out);
+    avro::DecoderPtr d = avro::resolvingDecoder(cpxSchema, imaginarySchema,
+        avro::binaryDecoder());
+    d->init(*in);
+
+    i::cpx c2;
+    avro::decode(*d, c2);
+    std::cout << "Imaginary: " << c2.im << std::endl;
+
+}

Added: avro/trunk/lang/c++/examples/schemaload.cc
URL: http://svn.apache.org/viewvc/avro/trunk/lang/c%2B%2B/examples/schemaload.cc?rev=1185027&view=auto
==============================================================================
--- avro/trunk/lang/c++/examples/schemaload.cc (added)
+++ avro/trunk/lang/c++/examples/schemaload.cc Mon Oct 17 07:14:12 2011
@@ -0,0 +1,14 @@
+#include <fstream>
+
+#include "avro/ValidSchema.hh"
+#include "avro/Compiler.hh"
+
+
+int
+main()
+{
+    std::ifstream in("cpx.json");
+
+    avro::ValidSchema cpxSchema;
+    avro::compileJsonSchema(in, cpxSchema);
+}

Added: avro/trunk/lang/c++/examples/validating.cc
URL: http://svn.apache.org/viewvc/avro/trunk/lang/c%2B%2B/examples/validating.cc?rev=1185027&view=auto
==============================================================================
--- avro/trunk/lang/c++/examples/validating.cc (added)
+++ avro/trunk/lang/c++/examples/validating.cc Mon Oct 17 07:14:12 2011
@@ -0,0 +1,50 @@
+#include <fstream>
+#include <complex>
+
+#include "avro/Compiler.hh"
+#include "avro/Encoder.hh"
+#include "avro/Decoder.hh"
+#include "avro/Specific.hh"
+
+namespace avro {
+template<typename T>
+struct codec_traits<std::complex<T> > {
+    static void encode(Encoder& e, const std::complex<T>& c) {
+        avro::encode(e, std::real(c));
+        avro::encode(e, std::imag(c));
+    }
+
+    static void decode(Decoder& d, std::complex<T>& c) {
+        T re, im;
+        avro::decode(d, re);
+        avro::decode(d, im);
+        c = std::complex<T>(re, im);
+    }
+};
+
+}
+int
+main()
+{
+    std::ifstream ifs("cpx.json");
+
+    avro::ValidSchema cpxSchema;
+    avro::compileJsonSchema(ifs, cpxSchema);
+
+    std::auto_ptr<avro::OutputStream> out = avro::memoryOutputStream();
+    avro::EncoderPtr e = avro::validatingEncoder(cpxSchema,
+        avro::binaryEncoder());
+    e->init(*out);
+    std::complex<double> c1(1.0, 2.0);
+    avro::encode(*e, c1);
+
+    std::auto_ptr<avro::InputStream> in = avro::memoryInputStream(*out);
+    avro::DecoderPtr d = avro::validatingDecoder(cpxSchema,
+        avro::binaryDecoder());
+    d->init(*in);
+
+    std::complex<double> c2;
+    avro::decode(*d, c2);
+    std::cout << '(' << std::real(c2) << ", " << std::imag(c2) <<
')' << std::endl;
+    return 0;
+}



Mime
View raw message