From f0510d2f90467de8e8f260b47d79a9baaf9bef17 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 17 Sep 2009 07:15:29 +0200 Subject: Start tracking XSD with git --- libxsd/xsd/cxx/tree/xdr-stream-extraction.hxx | 295 ++++++++++++++++++++++++++ 1 file changed, 295 insertions(+) create mode 100644 libxsd/xsd/cxx/tree/xdr-stream-extraction.hxx (limited to 'libxsd/xsd/cxx/tree/xdr-stream-extraction.hxx') diff --git a/libxsd/xsd/cxx/tree/xdr-stream-extraction.hxx b/libxsd/xsd/cxx/tree/xdr-stream-extraction.hxx new file mode 100644 index 0000000..f71a05e --- /dev/null +++ b/libxsd/xsd/cxx/tree/xdr-stream-extraction.hxx @@ -0,0 +1,295 @@ +// file : xsd/cxx/tree/xdr-stream-extraction.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2005-2009 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#ifndef XSD_CXX_TREE_XDR_STREAM_EXTRACTION_HXX +#define XSD_CXX_TREE_XDR_STREAM_EXTRACTION_HXX + +#include +#include + +#include + +#include +#include +#include + +namespace xsd +{ + namespace cxx + { + namespace tree + { + struct xdr_stream_extraction: xdr_stream_operation + { + virtual const char* + what () const throw () + { + return "XDR stream extraction operation failed"; + } + }; + + + // as_size + // +#ifdef XSD_CXX_TREE_USE_64_BIT_SIZE + template + inline istream& + operator>> (istream& s, istream::as_size& x) + { + uint64_t v; + + if (!xdr_uint64_t (&s.impl (), &v) || v > ~(T (0))) + throw xdr_stream_extraction (); + + x.x_ = static_cast (v); + + return s; + } +#else + template + inline istream& + operator>> (istream& s, istream::as_size& x) + { + uint32_t v; + + if (!xdr_uint32_t (&s.impl (), &v)) + throw xdr_stream_extraction (); + + x.x_ = static_cast (v); + + return s; + } +#endif + + + // 8-bit + // + template + inline istream& + operator>> (istream& s, istream::as_int8& x) + { + int8_t v; + + if (!xdr_int8_t (&s.impl (), &v)) + throw xdr_stream_extraction (); + + x.x_ = static_cast (v); + + return s; + } + + template + inline istream& + operator>> (istream& s, istream::as_uint8& x) + { + uint8_t v; + + if (!xdr_uint8_t (&s.impl (), &v)) + throw xdr_stream_extraction (); + + x.x_ = static_cast (v); + + return s; + } + + + // 16-bit + // + template + inline istream& + operator>> (istream& s, istream::as_int16& x) + { + int16_t v; + + if (!xdr_int16_t (&s.impl (), &v)) + throw xdr_stream_extraction (); + + x.x_ = static_cast (v); + + return s; + } + + template + inline istream& + operator>> (istream& s, istream::as_uint16& x) + { + uint16_t v; + + if (!xdr_uint16_t (&s.impl (), &v)) + throw xdr_stream_extraction (); + + x.x_ = static_cast (v); + + return s; + } + + + // 32-bit + // + template + inline istream& + operator>> (istream& s, istream::as_int32& x) + { + int32_t v; + + if (!xdr_int32_t (&s.impl (), &v)) + throw xdr_stream_extraction (); + + x.x_ = static_cast (v); + + return s; + } + + template + inline istream& + operator>> (istream& s, istream::as_uint32& x) + { + uint32_t v; + + if (!xdr_uint32_t (&s.impl (), &v)) + throw xdr_stream_extraction (); + + x.x_ = static_cast (v); + + return s; + } + + + // 64-bit + // + template + inline istream& + operator>> (istream& s, istream::as_int64& x) + { + int64_t v; + + if (!xdr_int64_t (&s.impl (), &v)) + throw xdr_stream_extraction (); + + x.x_ = static_cast (v); + + return s; + } + + template + inline istream& + operator>> (istream& s, istream::as_uint64& x) + { + uint64_t v; + + if (!xdr_uint64_t (&s.impl (), &v)) + throw xdr_stream_extraction (); + + x.x_ = static_cast (v); + + return s; + } + + + // Boolean + // + template + inline istream& + operator>> (istream& s, istream::as_bool& x) + { + bool_t v; + + if (!xdr_bool (&s.impl (), &v)) + throw xdr_stream_extraction (); + + x.x_ = static_cast (v); + + return s; + } + + + // Floating-point + // + template + inline istream& + operator>> (istream& s, istream::as_float32& x) + { + float v; + + if (!xdr_float (&s.impl (), &v)) + throw xdr_stream_extraction (); + + x.x_ = static_cast (v); + + return s; + } + + template + inline istream& + operator>> (istream& s, istream::as_float64& x) + { + double v; + + if (!xdr_double (&s.impl (), &v)) + throw xdr_stream_extraction (); + + x.x_ = static_cast (v); + + return s; + } + + // Extraction of std::basic_string. + // + + inline istream& + operator>> (istream& s, std::basic_string& x) + { + unsigned int n; + + if (!xdr_u_int (&s.impl (), &n)) + throw xdr_stream_extraction (); + + // Dangerous but fast. + // + x.clear (); + + if (n != 0) + { + x.resize (n); + char* p (const_cast (x.c_str ())); + + if (!xdr_opaque (&s.impl (), p, n)) + throw xdr_stream_extraction (); + } + + return s; + } + + // Wide strings are not supported by XDR. + // + // inline istream& + // operator>> (istream& s, std::basic_string& x) + // { + // } + + + // Extraction of a binary buffer. + // + template + istream& + operator>> (istream& s, buffer& x) + { + unsigned int n; + + if (!xdr_u_int (&s.impl (), &n)) + throw xdr_stream_extraction (); + + x.size (n); + + if (!xdr_opaque (&s.impl (), x.data (), n)) + throw xdr_stream_extraction (); + + return s; + } + } + } +} + +#endif // XSD_CXX_TREE_XDR_STREAM_EXTRACTION_HXX -- cgit v1.1