summaryrefslogtreecommitdiff
path: root/TAO/tao/Vector_CDR_T.h
blob: 14c6f4509c1b5dfdd9eeefc89726d16c9602def0 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#ifndef guard_vector_cdr
#define guard_vector_cdr
/**
 * @file
 *
 * @brief CDR (de)marshaling for std::vector
 *
 * @author Jeff Parsons
 */

#include <vector>

TAO_BEGIN_VERSIONED_NAMESPACE_DECL

namespace TAO
{
  template<typename T>
  bool
  marshal_value_vector (
    TAO_OutputCDR &strm,
    const std::vector<T> &source)
  {
    ::CORBA::ULong const length = source.size ();

    if (! (strm << length))
      {
        return false;
      }

    for (typename std::vector<T>::const_iterator iter =
           source.begin ();
         iter != source.end ();
         ++iter)
      {
        if (! (strm << *iter))
          {
            return false;
          }
      }

    return true;
  }

  template<typename T>
  bool
  demarshal_value_vector (
    TAO_InputCDR &strm,
    std::vector<T> &target)
  {
    ::CORBA::ULong new_length = 0;

    if (! (strm >> new_length))
      {
        return false;
      }

    if (new_length > strm.length ())
      {
        return false;
      }

    std::vector<T> tmp;
    tmp.reserve (new_length);
    T tmp_elem;

    for ( ::CORBA::ULong i = 0; i < new_length; ++i)
      {
        if (! (strm >> tmp_elem))
          {
            return false;
          }

        tmp[i] = tmp_elem;
      }

    tmp.swap(target);
    return true;
  }

  template<typename T>
  bool
  marshal_objref_vector (
    TAO_OutputCDR &strm,
    const std::vector<typename T::_ptr_type> &source)
  {
    ::CORBA::ULong const length = source.size ();

    if (! (strm << length))
      {
        return false;
      }

    for (typename std::vector<typename T::_ptr_type>::const_iterator i =
           source.begin ();
         i != source.end ();
         ++i)
      {
        if (! (TAO::Objref_Traits<T>::marshal (*i, strm)))
          {
            return false;
          }
      }

    return true;
  }

  template<typename T>
  bool
  demarshal_objref_vector (
    TAO_InputCDR &strm,
    std::vector<typename T::_ptr_type> &target)
  {
    ::CORBA::ULong new_length = 0;

    if (! (strm >> new_length))
      {
        return false;
      }

    if (new_length > strm.length ())
      {
        return false;
      }

    std::vector<typename T::_ptr_type> tmp;
    tmp.reserve (new_length);
    typename T::_ptr_type tmp_elem = T::_nil ();

    for ( ::CORBA::ULong i = 0; i < new_length; ++i)
      {
        if (! (strm >> tmp_elem))
          {
            return false;
          }

        tmp[i] = tmp_elem;
      }

    tmp.swap (target);
    return true;
  }

  template<typename T_forany>
  bool
  marshal_array_vector (
    TAO_OutputCDR &strm,
    const std::vector<typename T_forany::_slice_type *> &source)
  {
    typedef TAO_FixedArray_Var_T <typename T_forany::_array_type,
                                  typename T_forany::_slice_type,
                                  typename T_forany::_tag_type> var_type;
    ::CORBA::ULong const length = source.size ();

    if (! (strm << length))
      {
        return false;
      }

    for (std::vector<typename T_forany::_slice_type *> i =
           source.begin ();
         i != source.end ();
         ++i)
      {
        var_type tmp_array =
          TAO::Array_Traits<T_forany>::dup (*i);
        T_forany const tmp (tmp_array.inout ());

        if (! (strm << tmp))
          {
            return false;
          }
      }

    return true;
  }

  template<typename T_forany>
  bool
  demarshal_array_vector (
    TAO_InputCDR &strm,
    const std::vector<typename T_forany::_slice_type *> &source)
  {
    typedef TAO::Array_Traits<T_forany> array_traits;
    ::CORBA::ULong new_length = 0;

    if (! (strm >> new_length))
      {
        return false;
      }

    if (new_length > strm.length ())
      {
        return false;
      }

    std::vector<typename T_forany::_slice_type *> tmp_vec;
    tmp_vec.reserve (new_length);

    for ( ::CORBA::ULong i = 0; i < new_length; ++i)
      {
        T_forany tmp_array (array_traits::alloc ());
        bool const _tao_marshal_flag = (strm >> tmp_array);

        if (_tao_marshal_flag)
          {
            array_traits::copy (tmp_vec[i], tmp_array.in ());
          }

        array_traits::free (tmp_array.inout ());

        if (!_tao_marshal_flag)
          {
            return false;
          }
      }

    tmp_vec.swap (source);
    return true;
  }
}

TAO_END_VERSIONED_NAMESPACE_DECL

#endif // guard_vector_cdr