aboutsummaryrefslogtreecommitdiff
path: root/libxsde/xsde/cxx/hybrid/xdr/ostream.cxx
blob: 6ef0c52f7ca52419f62fdaca42103dacf30e005d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// file      : xsde/cxx/hybrid/xdr/ostream.cxx
// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

#include <xsde/cxx/config.hxx>

#ifndef XSDE_STL
#  include <string.h>
#endif

#include <xsde/cxx/hybrid/xdr/ostream.hxx>

namespace xsde
{
  namespace cxx
  {
    namespace hybrid
    {
#ifdef XSDE_EXCEPTIONS

      // XDR strings always use 32-bit length.
      //
#ifdef XSDE_STL
      void oxdrstream::
      operator<< (const std::string& x)
      {
        char* p = const_cast<char*> (x.c_str ());
        unsigned int n = static_cast<unsigned int> (x.length ());

        if (!xdr_u_int (&xdr_, &n) || !xdr_opaque (&xdr_, p, n))
          throw xdr_exception ();
      }
#else
      void oxdrstream::
      operator<< (const char* x)
      {
        unsigned int n = static_cast<unsigned int> (strlen (x));

        if (!xdr_u_int (&xdr_, &n) ||
            !xdr_opaque (&xdr_, const_cast<char*> (x), n))
          throw xdr_exception ();
      }
#endif

      void oxdrstream::
      operator<< (const buffer& x)
      {
        // XDR arrays are limited to 32-bit size.
        //
        unsigned int n = static_cast<unsigned int> (x.size ());

        if (!xdr_u_int (&xdr_, &n) ||
            !xdr_opaque (&xdr_, const_cast<char*> (x.data ()), n))
          throw xdr_exception ();
      }

#else // XSDE_EXCEPTIONS

#ifdef XSDE_STL
      bool oxdrstream::
      operator<< (const std::string& x)
      {
        char* p = const_cast<char*> (x.c_str ());
        unsigned int n = static_cast<unsigned int> (x.length ());
        return xdr_u_int (&xdr_, &n) && xdr_opaque (&xdr_, p, n);
      }
#else
      bool oxdrstream::
      operator<< (const char* x)
      {
        unsigned int n = static_cast<unsigned int> (strlen (x));
        return xdr_u_int (&xdr_, &n) &&
          xdr_opaque (&xdr_, const_cast<char*> (x), n);
      }
#endif

      bool oxdrstream::
      operator<< (const buffer& x)
      {
        // XDR arrays are limited to 32-bit size.
        //
        unsigned int n = static_cast<unsigned int> (x.size ());
        return xdr_u_int (&xdr_, &n) &&
          xdr_opaque (&xdr_, const_cast<char*> (x.data ()), n);
      }

#endif // XSDE_EXCEPTIONS

    }
  }
}