summaryrefslogtreecommitdiff
path: root/TAO/tao/AnyTypeCode/Any_Unknown_IDL_Type.cpp
blob: 23932fb77b5a21dfd9ef3fbda228700edc6da414 (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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// -*- C++ -*-
// $Id$

#include "tao/AnyTypeCode/Any_Unknown_IDL_Type.h"
#include "tao/AnyTypeCode/Marshal.h"
#include "tao/AnyTypeCode/TypeCode.h"
#include "tao/Valuetype_Adapter.h"
#include "tao/ORB_Core.h"
#include "tao/SystemException.h"
#include "tao/CDR.h"
#include "tao/debug.h"

#include "ace/Dynamic_Service.h"
#include "ace/OS_NS_string.h"

TAO_BEGIN_VERSIONED_NAMESPACE_DECL

TAO::Unknown_IDL_Type::LOCK const
TAO::Unknown_IDL_Type::lock_i (void)
{
  static LOCK base_lock_ (new ACE_Lock_Adapter<TAO_SYNCH_MUTEX>());
  return base_lock_;
}

TAO::Unknown_IDL_Type::Unknown_IDL_Type (CORBA::TypeCode_ptr tc,
                                         TAO_InputCDR &cdr)
  : TAO::Any_Impl (0, tc, true)
  , lock_ (lock_i ())
  , cdr_ (static_cast<ACE_Message_Block*>(0), lock_.get ())
{
  try
    {
      this->_tao_decode (cdr);
    }
  catch (::CORBA::Exception const &)
    {
      if (TAO_debug_level > 0)
        {
          TAOLIB_DEBUG ((LM_DEBUG,
            ACE_TEXT ("TAO (%P|%t) %N:%l ")
            ACE_TEXT ("silent marshaling exception ")
            ACE_TEXT ("in TAO::Unknown_IDL_Type::Unknown_IDL_Type\n")));
        }
    }
}

TAO::Unknown_IDL_Type::Unknown_IDL_Type (CORBA::TypeCode_ptr tc)
  : TAO::Any_Impl (0, tc, true)
  , lock_ (lock_i ())
  , cdr_ (static_cast<ACE_Message_Block*>(0), lock_.get ())
{
}

TAO::Unknown_IDL_Type::~Unknown_IDL_Type (void)
{
}

CORBA::Boolean
TAO::Unknown_IDL_Type::marshal_value (TAO_OutputCDR & cdr)
{
  try
    {
      // We don't want the rd_ptr to move, in case we are shared by
      // another Any, so we use this to copy the state, not the buffer.
      TAO_InputCDR for_reading (this->cdr_);

      TAO::traverse_status const status =
        TAO_Marshal_Object::perform_append (this->type_,
                                            &for_reading,
                                            &cdr);

      if (status != TAO::TRAVERSE_CONTINUE)
        {
          return false;
        }
    }
  catch (::CORBA::Exception const &)
    {
      return false;
    }

  return true;
}

const void *
TAO::Unknown_IDL_Type::value (void) const
{
  return this->cdr_.start ();
}

void
TAO::Unknown_IDL_Type::free_value (void)
{
  ::CORBA::release (this->type_);
}

TAO_InputCDR &
TAO::Unknown_IDL_Type::_tao_get_cdr (void)
{
  return this->cdr_;
}

int
TAO::Unknown_IDL_Type::_tao_byte_order (void) const
{
  return this->cdr_.byte_order ();
}
void
TAO::Unknown_IDL_Type::_tao_decode (TAO_InputCDR & cdr)
{
  // @@ (JP) The following code depends on the fact that
  //         TAO_InputCDR does not contain chained message blocks,
  //         otherwise <begin> and <end> could be part of
  //         different buffers!

  // This will be the start of a new message block.
  char const * const begin = cdr.rd_ptr ();

  // Skip over the next argument.
  TAO::traverse_status const status =
    TAO_Marshal_Object::perform_skip (this->type_, &cdr);

  if (status != TAO::TRAVERSE_CONTINUE)
    {
      throw ::CORBA::MARSHAL ();
    }

  // This will be the end of the new message block.
  char const * const end = cdr.rd_ptr ();

  // The ACE_CDR::mb_align() call can shift the rd_ptr by up to
  // ACE_CDR::MAX_ALIGNMENT - 1 bytes. Similarly, the offset adjustment
  // can move the rd_ptr by up to the same amount. We accommodate
  // this by including 2 * ACE_CDR::MAX_ALIGNMENT bytes of additional
  // space in the message block.
  size_t const size = end - begin;

  ACE_Message_Block new_mb (size + 2 * ACE_CDR::MAX_ALIGNMENT);

  ACE_CDR::mb_align (&new_mb);
  ptrdiff_t offset = ptrdiff_t (begin) % ACE_CDR::MAX_ALIGNMENT;

  if (offset < 0)
    {
      offset += ACE_CDR::MAX_ALIGNMENT;
    }

  new_mb.rd_ptr (offset);
  new_mb.wr_ptr (offset + size);

  ACE_OS::memcpy (new_mb.rd_ptr (), begin, size);

  this->cdr_.reset (&new_mb, cdr.byte_order ());
  this->cdr_.char_translator (cdr.char_translator ());
  this->cdr_.wchar_translator (cdr.wchar_translator ());

  this->cdr_.set_repo_id_map (cdr.get_repo_id_map ());
  this->cdr_.set_codebase_url_map (cdr.get_codebase_url_map ());
  this->cdr_.set_value_map (cdr.get_value_map ());

  // Take over the GIOP version, the input cdr can have a different
  // version then our current GIOP version.
  ACE_CDR::Octet major_version;
  ACE_CDR::Octet minor_version;
  cdr.get_version (major_version, minor_version);
  this->cdr_.set_version (major_version, minor_version);
}

CORBA::Boolean
TAO::Unknown_IDL_Type::to_object (CORBA::Object_ptr & obj) const
{
  try
    {
      CORBA::TCKind const kind = TAO::unaliased_kind (this->type_);

      if (kind != CORBA::tk_objref)
        {
          return false;
        }

      // We don't want the rd_ptr to move, in case we are shared by
      // another Any, so we use this to copy the state, not the buffer.
      TAO_InputCDR for_reading (this->cdr_);

      return for_reading >> obj;
    }
  catch (::CORBA::Exception const &)
    {
    }

  return false;
}

CORBA::Boolean
TAO::Unknown_IDL_Type::to_value (CORBA::ValueBase* & val) const
{
  try
    {
      CORBA::TCKind const kind = TAO::unaliased_kind (this->type_);

      if (kind != CORBA::tk_value)
        {
          return false;
        }

      TAO_ORB_Core *orb_core = this->cdr_.orb_core ();
      if (orb_core == 0)
        {
          orb_core = TAO_ORB_Core_instance ();

          if (TAO_debug_level > 0)
            {
              TAOLIB_DEBUG ((LM_WARNING,
                          ACE_TEXT ("TAO (%P|%t) WARNING: extracting ")
                          ACE_TEXT ("valuetype using default ORB_Core\n")));
            }
        }

      // We don't want the rd_ptr to move, in case we are shared by
      // another Any, so we use this to copy the state, not the buffer.
      TAO_InputCDR for_reading (this->cdr_);

      TAO_Valuetype_Adapter * const adapter =
        orb_core->valuetype_adapter ();

      return adapter->stream_to_value (for_reading, val);
    }
  catch (::CORBA::Exception const &)
    {
    }

  return false;
}

CORBA::Boolean
TAO::Unknown_IDL_Type::to_abstract_base (CORBA::AbstractBase_ptr & obj) const
{
  try
    {
      CORBA::TCKind const kind = TAO::unaliased_kind (this->type_);

      if (kind != CORBA::tk_abstract_interface)
        {
          return false;
        }

      TAO_ORB_Core *orb_core = this->cdr_.orb_core ();
      if (orb_core == 0)
        {
          orb_core = TAO_ORB_Core_instance ();

          if (TAO_debug_level > 0)
            {
              TAOLIB_DEBUG ((LM_WARNING,
                          ACE_TEXT ("TAO (%P|%t) WARNING: extracting ")
                          ACE_TEXT ("abstract base using default ORB_Core\n")));
            }
        }

      // We don't want the rd_ptr to move, in case we are shared by
      // another Any, so we use this to copy the state, not the buffer.
      TAO_InputCDR for_reading (this->cdr_);

      TAO_Valuetype_Adapter * const adapter =
        orb_core->valuetype_adapter ();
      CORBA::Boolean ret = adapter->stream_to_abstract_base (for_reading, obj);

      return ret;
    }
   catch (::CORBA::Exception const &)
    {
    }

  return false;
}

TAO_END_VERSIONED_NAMESPACE_DECL