Revert "Minor modernization of DynamicAny code"
[ACE_TAO.git] / TAO / tao / Vector_CDR_T.h
blob14c6f4509c1b5dfdd9eeefc89726d16c9602def0
1 #ifndef guard_vector_cdr
2 #define guard_vector_cdr
3 /**
4 * @file
6 * @brief CDR (de)marshaling for std::vector
8 * @author Jeff Parsons
9 */
11 #include <vector>
13 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
15 namespace TAO
17 template<typename T>
18 bool
19 marshal_value_vector (
20 TAO_OutputCDR &strm,
21 const std::vector<T> &source)
23 ::CORBA::ULong const length = source.size ();
25 if (! (strm << length))
27 return false;
30 for (typename std::vector<T>::const_iterator iter =
31 source.begin ();
32 iter != source.end ();
33 ++iter)
35 if (! (strm << *iter))
37 return false;
41 return true;
44 template<typename T>
45 bool
46 demarshal_value_vector (
47 TAO_InputCDR &strm,
48 std::vector<T> &target)
50 ::CORBA::ULong new_length = 0;
52 if (! (strm >> new_length))
54 return false;
57 if (new_length > strm.length ())
59 return false;
62 std::vector<T> tmp;
63 tmp.reserve (new_length);
64 T tmp_elem;
66 for ( ::CORBA::ULong i = 0; i < new_length; ++i)
68 if (! (strm >> tmp_elem))
70 return false;
73 tmp[i] = tmp_elem;
76 tmp.swap(target);
77 return true;
80 template<typename T>
81 bool
82 marshal_objref_vector (
83 TAO_OutputCDR &strm,
84 const std::vector<typename T::_ptr_type> &source)
86 ::CORBA::ULong const length = source.size ();
88 if (! (strm << length))
90 return false;
93 for (typename std::vector<typename T::_ptr_type>::const_iterator i =
94 source.begin ();
95 i != source.end ();
96 ++i)
98 if (! (TAO::Objref_Traits<T>::marshal (*i, strm)))
100 return false;
104 return true;
107 template<typename T>
108 bool
109 demarshal_objref_vector (
110 TAO_InputCDR &strm,
111 std::vector<typename T::_ptr_type> &target)
113 ::CORBA::ULong new_length = 0;
115 if (! (strm >> new_length))
117 return false;
120 if (new_length > strm.length ())
122 return false;
125 std::vector<typename T::_ptr_type> tmp;
126 tmp.reserve (new_length);
127 typename T::_ptr_type tmp_elem = T::_nil ();
129 for ( ::CORBA::ULong i = 0; i < new_length; ++i)
131 if (! (strm >> tmp_elem))
133 return false;
136 tmp[i] = tmp_elem;
139 tmp.swap (target);
140 return true;
143 template<typename T_forany>
144 bool
145 marshal_array_vector (
146 TAO_OutputCDR &strm,
147 const std::vector<typename T_forany::_slice_type *> &source)
149 typedef TAO_FixedArray_Var_T <typename T_forany::_array_type,
150 typename T_forany::_slice_type,
151 typename T_forany::_tag_type> var_type;
152 ::CORBA::ULong const length = source.size ();
154 if (! (strm << length))
156 return false;
159 for (std::vector<typename T_forany::_slice_type *> i =
160 source.begin ();
161 i != source.end ();
162 ++i)
164 var_type tmp_array =
165 TAO::Array_Traits<T_forany>::dup (*i);
166 T_forany const tmp (tmp_array.inout ());
168 if (! (strm << tmp))
170 return false;
174 return true;
177 template<typename T_forany>
178 bool
179 demarshal_array_vector (
180 TAO_InputCDR &strm,
181 const std::vector<typename T_forany::_slice_type *> &source)
183 typedef TAO::Array_Traits<T_forany> array_traits;
184 ::CORBA::ULong new_length = 0;
186 if (! (strm >> new_length))
188 return false;
191 if (new_length > strm.length ())
193 return false;
196 std::vector<typename T_forany::_slice_type *> tmp_vec;
197 tmp_vec.reserve (new_length);
199 for ( ::CORBA::ULong i = 0; i < new_length; ++i)
201 T_forany tmp_array (array_traits::alloc ());
202 bool const _tao_marshal_flag = (strm >> tmp_array);
204 if (_tao_marshal_flag)
206 array_traits::copy (tmp_vec[i], tmp_array.in ());
209 array_traits::free (tmp_array.inout ());
211 if (!_tao_marshal_flag)
213 return false;
217 tmp_vec.swap (source);
218 return true;
222 TAO_END_VERSIONED_NAMESPACE_DECL
224 #endif // guard_vector_cdr