From 2615896faa646e5830abf2c269150e1165c66515 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Fri, 18 Dec 2020 18:48:46 +0300 Subject: Switch to build2 --- libxsd/libxsd/cxx/tree/xdr-stream-insertion.hxx | 270 ++++++++++++++++++++++++ 1 file changed, 270 insertions(+) create mode 100644 libxsd/libxsd/cxx/tree/xdr-stream-insertion.hxx (limited to 'libxsd/libxsd/cxx/tree/xdr-stream-insertion.hxx') diff --git a/libxsd/libxsd/cxx/tree/xdr-stream-insertion.hxx b/libxsd/libxsd/cxx/tree/xdr-stream-insertion.hxx new file mode 100644 index 0000000..882f765 --- /dev/null +++ b/libxsd/libxsd/cxx/tree/xdr-stream-insertion.hxx @@ -0,0 +1,270 @@ +// file : libxsd/cxx/tree/xdr-stream-insertion.hxx +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#ifndef LIBXSD_CXX_TREE_XDR_STREAM_INSERTION_HXX +#define LIBXSD_CXX_TREE_XDR_STREAM_INSERTION_HXX + +#include +#include + +// Of course BSD has to be different and name its functions u_intXX +// instead of uintXX. Plus it does not have XX == 8. +// +#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__NetBSD__) +# if !defined(XSD_CXX_TREE_ASSUME_SUN_XDR) && !defined(xdr_int8_t) +# define xdr_int8_t(x, y) xdr_char(x, reinterpret_cast (y)) +# define xdr_uint8_t(x, y) xdr_u_char(x, reinterpret_cast (y)) +# define xdr_uint16_t xdr_u_int16_t +# define xdr_uint32_t xdr_u_int32_t +# define xdr_uint64_t xdr_u_int64_t +# endif +#endif + +#include + +#include +#include +#include + +namespace xsd +{ + namespace cxx + { + namespace tree + { + struct xdr_stream_insertion: xdr_stream_operation + { + virtual const char* + what () const throw () + { + return "XDR stream insertion operation failed"; + } + }; + + // as_size + // +#ifdef XSD_CXX_TREE_USE_64_BIT_SIZE + template + inline ostream& + operator<< (ostream& s, ostream::as_size x) + { + uint64_t v (static_cast (x.x_)); + + if (!xdr_uint64_t (&s.impl (), &v)) + throw xdr_stream_insertion (); + + return s; + } +#else + template + inline ostream& + operator<< (ostream& s, ostream::as_size x) + { + uint32_t v (static_cast (x.x_)); + + if (x.x_ > ~(uint32_t (0)) || !xdr_uint32_t (&s.impl (), &v)) + throw xdr_stream_insertion (); + + return s; + } +#endif + + + // 8-bit + // + template + inline ostream& + operator<< (ostream& s, ostream::as_int8 x) + { + int8_t v (static_cast (x.x_)); + + if (!xdr_int8_t (&s.impl (), &v)) + throw xdr_stream_insertion (); + + return s; + } + + template + inline ostream& + operator<< (ostream& s, ostream::as_uint8 x) + { + uint8_t v (static_cast (x.x_)); + + if (!xdr_uint8_t (&s.impl (), &v)) + throw xdr_stream_insertion (); + + return s; + } + + + // 16-bit + // + template + inline ostream& + operator<< (ostream& s, ostream::as_int16 x) + { + int16_t v (static_cast (x.x_)); + + if (!xdr_int16_t (&s.impl (), &v)) + throw xdr_stream_insertion (); + + return s; + } + + template + inline ostream& + operator<< (ostream& s, ostream::as_uint16 x) + { + uint16_t v (static_cast (x.x_)); + + if (!xdr_uint16_t (&s.impl (), &v)) + throw xdr_stream_insertion (); + + return s; + } + + + // 32-bit + // + template + inline ostream& + operator<< (ostream& s, ostream::as_int32 x) + { + int32_t v (static_cast (x.x_)); + + if (!xdr_int32_t (&s.impl (), &v)) + throw xdr_stream_insertion (); + + return s; + } + + template + inline ostream& + operator<< (ostream& s, ostream::as_uint32 x) + { + uint32_t v (static_cast (x.x_)); + + if (!xdr_uint32_t (&s.impl (), &v)) + throw xdr_stream_insertion (); + + return s; + } + + + // 64-bit + // + template + inline ostream& + operator<< (ostream& s, ostream::as_int64 x) + { + int64_t v (static_cast (x.x_)); + + if (!xdr_int64_t (&s.impl (), &v)) + throw xdr_stream_insertion (); + + return s; + } + + template + inline ostream& + operator<< (ostream& s, ostream::as_uint64 x) + { + uint64_t v (static_cast (x.x_)); + + if (!xdr_uint64_t (&s.impl (), &v)) + throw xdr_stream_insertion (); + + return s; + } + + + // Boolean + // + template + inline ostream& + operator<< (ostream& s, ostream::as_bool x) + { + bool_t v (static_cast (x.x_)); + + if (!xdr_bool (&s.impl (), &v)) + throw xdr_stream_insertion (); + + return s; + } + + + // Floating-point + // + template + inline ostream& + operator<< (ostream& s, ostream::as_float32 x) + { + float v (static_cast (x.x_)); + + if (!xdr_float (&s.impl (), &v)) + throw xdr_stream_insertion (); + + return s; + } + + template + inline ostream& + operator<< (ostream& s, ostream::as_float64 x) + { + double v (static_cast (x.x_)); + + if (!xdr_double (&s.impl (), &v)) + throw xdr_stream_insertion (); + + return s; + } + + // Insertion of std::basic_string. + // + + inline ostream& + operator<< (ostream& s, const std::basic_string& x) + { + // XDR strings are hard-wired with a 32 bit (unsigned int) length. + // + char* p (const_cast (x.c_str ())); + unsigned int n (static_cast (x.length ())); + + if (x.length () > ~((unsigned int) 0) || + !xdr_u_int (&s.impl (), &n) || + !xdr_opaque (&s.impl (), p, n)) + throw xdr_stream_insertion (); + + return s; + } + + // Wide strings are not supported by XDR. + // + // inline ostream& + // operator<< (ostream& s, const std::basic_string& x) + // { + // } + + + // Insertion of a binary buffer. + // + template + ostream& + operator<< (ostream& s, const buffer& x) + { + // It is not possible to write an array with a 64-bit size. + // + unsigned int n (static_cast (x.size ())); + + if (x.size () > ~((unsigned int) 0) || + !xdr_u_int (&s.impl (), &n) || + !xdr_opaque (&s.impl (), const_cast (x.data ()), n)) + throw xdr_stream_insertion (); + + return s; + } + } + } +} + +#endif // LIBXSD_CXX_TREE_XDR_STREAM_INSERTION_HXX -- cgit v1.1