summaryrefslogtreecommitdiff
path: root/TAO/IIOP/lib/any.cpp
blob: fa2a88eba5f985c6a5bb9a3341091da3fbaccb2b (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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
// @(#) $Id$
//
// Copyright 1994-1995 by Sun Microsystems Inc.
// All Rights Reserved
//
// ORB:         Implementation of CORBA_Any
//
// This includes three constructors, a destructor, and a "replace"
// method for the "Any" data type.  "Any" values pair a pointer to a
// data structure in the native binary representation (e.g. C struct)
// with a TypeCode that describes that data structure.
//
// The copy constructor and the destructor each use the TypeCode
// interpreter with specialized "visit" callback routines.  The
// "visit" routines are used respectively to make "deep copies" and
// perform "deep frees" of the aritrary values as described by the
// "Any" value's typecode.
//
// Note that these "visit" routines are called directly, and they
// choose whether or not to use the TypeCode interpreter to examine
// constituents.  In the simple cases, the "visit" routines can do
// their work without any further calls; only for constructed types is
// the interpreter's knowledge really required.
//
// THREADING NOTE: "Any" is a data structure which must be protected
// by external critical sections.  Like simpler numeric types, "Any"
// instances are accessed and modified atomically.  This
// implementation is reentrant, so that independent "Any" values may
// be manipulated concurrently when the underlying programming
// environment is itself reentrant.
//
// COM NOTE: Yes, this is a utility data type whose implementation is
// fully exposed.  Factories for these are not normally used in C++.

#include <assert.h>
#include <limits.h>
#include <string.h>
#include <orb.h>

#include "debug.h"
#include "thread.h"

#include <initguid.h>

#if !defined (__ACE_INLINE__)
#  include "any.i"
#endif

CORBA_TypeCode_ptr
CORBA_Any::type (void) const
{
  return _type;
}

void *
CORBA_Any::value (void) const
{
  return _value;
}

// Default "Any" constructor -- initializes to nulls per the
// OMG C++ mapping.
//
// NOTE:  null (zero) typecode pointers are also treated as
// the null typecode ...

CORBA_Any::CORBA_Any (void) 
{
  _type = _tc_CORBA_Null;
  _value = 0;
  _orb_owns_data = CORBA_B_FALSE;
  _refcnt = 1;
}

// The more common "Any" constructor has its own copy of a
// typecode, and either holds or "consumes" an arbitrary data
// value satisfying the normal binary interface rules.

CORBA_Any::CORBA_Any (CORBA_TypeCode_ptr tc,
                      void *value,
                      CORBA_Boolean orb_owns_data) 
                      
  : _value (value) ,
    _orb_owns_data (orb_owns_data) 
{
  _type = tc;
  tc->AddRef ();
  _refcnt = 1;
}

// Helper routine for "Any" copy constructor ...
//
// "Deep Copy" from source to dest.  Memory is always there to be
// copied to ... if this calls itself recursively, it ensures that
// this remains true (only really an issue for sequences) .
//
// This shows the main reason to pass two values to the "visit"
// function used by the TypeCode interpreter: it allows the copy to be
// made without using any additional temporary memory.  Most other
// such "visit" routines use only a single value.  This is also
// slightly atypical in that it doesn't use the "context".

static CORBA_TypeCode::traverse_status
deep_copy (CORBA_TypeCode_ptr tc,
           const void *source,
           const void *dest,
           void *,              // no context
           CORBA_Environment &env) 
{
  CORBA_TypeCode::traverse_status retval;
  CORBA_TCKind my_kind;

  if (!tc) 
    {
      env.exception (new CORBA_BAD_TYPECODE (COMPLETED_NO) );
      return CORBA_TypeCode::TRAVERSE_STOP;
    }

  my_kind = tc->kind (env);

  if (env.exception_type () != NO_EXCEPTION) 
    return CORBA_TypeCode::TRAVERSE_STOP;

  // Deep copy from "source" to "dest" ... this code "knows" a bit
  // about representations, verify it when porting to oddball
  // platforms with non-IEEE floating point values or atypical byte
  // and word sizes.
  //
  // See the TypeCode interpreter code for more details about the
  // representational assumptions here.

  retval = CORBA_TypeCode::TRAVERSE_CONTINUE;

  switch (my_kind) 
    {
    case tk_null:
    case tk_void:
      break;
      
    case tk_char:
    case tk_octet:
      *(CORBA_Octet *) dest = *(CORBA_Octet *) source;
      break;

    case tk_short:
    case tk_ushort:
      *(CORBA_Short *) dest = *(CORBA_Short *) source;
      break;

    case tk_wchar:
      *(CORBA_WChar *) dest = *(CORBA_WChar *) source;
      break;

    case tk_long:
    case tk_ulong:
    case tk_float:
      *(CORBA_Long *) dest = *(CORBA_Long *) source;
      break;

    case tk_longlong:
    case tk_ulonglong:
    case tk_double:
      *(CORBA_LongLong *) dest = *(CORBA_LongLong *) source;
      break;

    case tk_longdouble:
      *(CORBA_LongDouble *) dest = *(CORBA_LongDouble *) source;
      break;

    case tk_boolean:
      *(CORBA_Boolean *) dest = *(CORBA_Boolean *) source;
      break;

    case tk_any:
      (void) new (dest) CORBA_Any (*(CORBA_Any*) source);
      break;

    case tk_TypeCode:
      if ((*(CORBA_TypeCode_ptr *) source) != 0) 
        dest = source;
      else
        dest = _tc_CORBA_Null;
      ((CORBA_TypeCode_ptr) dest)->AddRef ();
      break;

    case tk_Principal:
      {
        CORBA_Principal_ptr     src, dst;

        src = *(CORBA_Principal_ptr *) source;
        dst = *(CORBA_Principal_ptr *) dest = new CORBA_Principal;

        // Principals are just opaque IDs ... copy them

        assert (src->id.length <= UINT_MAX);
        dst->id.length = dst->id.maximum = src->id.length;

        if (dst->id.length > 0) 
	  {
	    dst->id.buffer = new CORBA_Octet [ (unsigned) dst->id.length];
	    ACE_OS::memcpy (dst->id.buffer, src->id.buffer,
			    (size_t) dst->id.length);
	  } 
	else 
          dst->id.buffer = 0;
      }
    break;

    case tk_objref:
      *(CORBA_Object_ptr *) dest = CORBA_Object::
        _duplicate (*(CORBA_Object_ptr *) source);
      break;

    case tk_sequence:
      {
        CORBA_OctetSeq          *src, *dst;
        CORBA_TypeCode_ptr      tcp;
        size_t                  size;

        // Rely on binary format of sequences -- all are the same
        // except for the type pointed to by "buffer"

        src = (CORBA_OctetSeq *) source;
        dst = (CORBA_OctetSeq *) dest;

        assert (src->length <= UINT_MAX);
        dst->length = dst->maximum = src->length;

        // Get the size of each "buffer" element

        tcp = tc->typecode_param (0, env);

        if (env.exception () != 0) 
	  {
	    retval = CORBA_TypeCode::TRAVERSE_STOP;
	    break;
	  }

        size = tcp->size (env);

        if (env.exception () != 0) 
	  {
	    retval = CORBA_TypeCode::TRAVERSE_STOP;
	    break;
	  }
        tcp->Release ();

        // Now allocate a new (uninitialized) buffer of the right size
        // to hold that many elements ... fall through and let a
        // general traverse fill in those buffer elements.

        size *= (size_t) src->length;
        dst->buffer = new CORBA_Octet [size];
      }
    // FALLTHROUGH

    case tk_struct:
    case tk_union:
    case tk_array:
    case tk_alias:
      return tc->traverse (source, 
			   dest,
                           (CORBA_TypeCode::VisitRoutine) deep_copy, 
			   0, 
			   env);

    case tk_except:
      // Exceptions in memory have a "hidden" typecode up front, used
      // to ensure that memory is appropriately freed and to hold the
      // exception ID.  We just copy that typecode, the traverse code
      // ignores it completely.

      *(CORBA_TypeCode_ptr *) dest = *(CORBA_TypeCode_ptr *) source;
      (void) (*(CORBA_TypeCode_ptr *) dest)->AddRef ();

      return tc->traverse (source, 
			   dest, 
			   (CORBA_TypeCode::VisitRoutine) deep_copy, 
			   0, 
			   env);

    case tk_enum:
      *(int *) dest = *(int *) source;
      break;

    case tk_string:
      *(CORBA_String *) dest =
	CORBA_string_copy (*(CORBA_String *) source);
      break;

    case tk_wstring:
      *(CORBA_WString *) dest =
	CORBA_wstring_copy (*(CORBA_WString *) source);
      break;

    default:
      dmsg ("deep copy default case ?");
      env.exception (new CORBA_BAD_TYPECODE (COMPLETED_NO) );
      retval = CORBA_TypeCode::TRAVERSE_STOP;
      break;
    }
  return retval;
}

// Copy constructor for "Any".

CORBA_Any::CORBA_Any (const CORBA_Any &src) 
{
  CORBA_Environment env;
  size_t size;

  if (src._type != 0) 
    _type = src._type;
  else
    _type = _tc_CORBA_Null;

  _type->AddRef ();
  _orb_owns_data = CORBA_B_TRUE;

  size = _type->size (env);           // XXX check error status
  _value = (char *) calloc (1, size);

  (void) _type->traverse (src._value, 
			  _value,
			  (CORBA_TypeCode::VisitRoutine) deep_copy, 
			  0, 
			  env);
}

// Helper routine for "Any" destructor.
//
// This frees all the memory pointed to by any given value held inside
// of an "Any".  For most data types it does nothing, since most data
// types don't hold any memory.  For a few, it recurses.
//
// This is one of the simplest typecode interpreter callbacks, since
// in most cases it does nothing.  Also, it uses neither the second
// value nor the context parameter.

static CORBA_TypeCode::traverse_status
deep_free (CORBA_TypeCode_ptr tc,
	   const void *value,
	   const void *,      // value2 unused
	   void *,      // context unused
	   CORBA_Environment &env) 
{
  // Don't do anything if the value is a null pointer.

  if (!value) 
    return CORBA_TypeCode::TRAVERSE_CONTINUE;

  CORBA_TypeCode::traverse_status retval;
  CORBA_TCKind my_kind;

  if (!tc) 
    {
      env.exception (new CORBA_BAD_TYPECODE (COMPLETED_NO) );
      return CORBA_TypeCode::TRAVERSE_STOP;
    }

  my_kind = tc->kind (env);

  if (env.exception_type () != NO_EXCEPTION) 
    return CORBA_TypeCode::TRAVERSE_STOP;

  // Free only embedded pointers ... which don't exist in most
  // primitive types.

  retval = CORBA_TypeCode::TRAVERSE_CONTINUE;
  switch (my_kind) 
    {
    case tk_struct:
    case tk_union:
    case tk_array:
    case tk_alias:
      return tc->traverse (value, 
			   0,
			   (CORBA_TypeCode::VisitRoutine) deep_free, 
			   0, 
			   env);

      // XXX: Exceptions are currently leaked because of bugs lurking
      // in this area.  Keep in mind that there are two things to
      // free: (a) the typecode in the exception base class; (b) any
      // pointers held by a user-defined exception, such as an objref
      // or string.
      //
      // Since this code does nothing, it should leak BOTH of those
      // kinds of memory.  Since it's not supposed to be called except
      // when the exception really is being freed, it should only be
      // called when the reference count in the exception base class
      // is zero.
      //
      // It's not clear which of those assertions actually hold.
      //
      // The code SHOULD be just like the traverse () call for a
      // structure, with (a) a precondition that the reference count
      // is zero, (b) an assertion that the typecode in the exception
      // and "tc" are equivalent, (c) releasing that typecode found
      // within the exception.
      //
    case tk_except:
      return retval;

    case tk_sequence:
      retval = tc->traverse (value, 
			     0,
			     (CORBA_TypeCode::VisitRoutine) deep_free,
			     0, 
			     env);
      delete ((CORBA_OctetSeq *) value)->buffer;
      break;

    case tk_TypeCode:
      if ((*(CORBA_TypeCode_ptr *) value) != 0) 
	(*(CORBA_TypeCode_ptr *) value)->Release ();
      break;

    case tk_Principal:
      CORBA_release (*(CORBA_Principal_ptr *) value);
      break;

    case tk_objref:
      CORBA_release (*(CORBA_Object_ptr *) value);
      break;

    case tk_string:
      CORBA_string_free (*(CORBA_String *) value);
      break;

    case tk_wstring:
      CORBA_wstring_free (*(CORBA_WString *) value);
      break;

    case tk_any:
#ifdef  __BORLANDC__
      // XXX BC++ doesn't yet accept explicit calls to destructors
      // with this syntax.  A simple workaround must exist, though;
      // other explicit destructor calls work.

      dmsg ("Delete Any-in-Any ... memory leak with BC++ 4.5");
#else
      ((CORBA_Any *) value)->~CORBA_Any ();
#endif
      break;

    default:
      return CORBA_TypeCode::TRAVERSE_CONTINUE;
    }

  if (env.exception_type () != NO_EXCEPTION) 
    return CORBA_TypeCode::TRAVERSE_STOP;
  else
    return retval;
}

// Destructor for an "Any" deep-frees memory if needed.
//
// NOTE that the assertion below will fire on application programmer
// errors, such as using AddRef/Release out of sync with the true
// lifetime of an Any value allocated on the stack.  BUT it involves
// changing the refcounting policy so that it's initialized to zero,
// not one ... which policy affects the whole source base, and not
// just this data type.  Get to this later.

CORBA_Any::~CORBA_Any (void) 
{
  CORBA_Environment env;

  // assert (_refcnt == 0);

  if (_orb_owns_data) 
    {
      (void) deep_free (_type, _value, 0, 0, env);
      delete _value;
    }

  if (_type) 
    _type->Release ();
}

// all-at-once replacement of the contents of an "Any"

void
CORBA_Any::replace (CORBA_TypeCode_ptr tc,
		    const void *v,
		    CORBA_Boolean orb_owns_data,
		    CORBA_Environment &env) 
{
  if (_orb_owns_data) 
    {
      (void) deep_free (_type, _value, 0, 0, env);
      delete _value;
    }

  if (_type != 0) 
    _type->Release ();

  env.clear ();

  _type = tc;
  tc->AddRef ();
  _value = (void *) v;
  _orb_owns_data = orb_owns_data;
}

// For COM -- IUnKnown operations

// {A201E4C8-F258-11ce-9598-0000C07CA898}
DEFINE_GUID (IID_CORBA_Any,
0xa201e4c8, 0xf258, 0x11ce, 0x95, 0x98, 0x0, 0x0, 0xc0, 0x7c, 0xa8, 0x98);

ULONG
__stdcall
CORBA_Any::AddRef (void) 
{
  ACE_GUARD_RETURN (ACE_Thread_Mutex, guard, lock_, 0);

  return ++_refcnt;
}

ULONG __stdcall
CORBA_Any::Release (void) 
{
  ACE_GUARD_RETURN (ACE_Thread_Mutex, guard, lock_, 0);

  if (--_refcnt != 0) 
    return _refcnt;

  guard.release ();

  delete this;
  return 0;
}

HRESULT __stdcall
CORBA_Any::QueryInterface (REFIID riid,
			   void **ppv) 
{
  *ppv = 0;

  if (IID_CORBA_Any == riid || IID_IUnknown == riid) 
    *ppv = this;

  if (*ppv == 0) 
    return ResultFromScode (E_NOINTERFACE);

 (void) AddRef ();
  return NOERROR;
}

// VARIANT conversions

// copy constructor

CORBA_Any::CORBA_Any (const VARIANT &src) 
{
  _orb_owns_data = CORBA_B_TRUE;
  _refcnt = 1;
  _type = _tc_CORBA_Void;
  _value = 0;

  *this = src;
}

// assignment operator
CORBA_Any &
CORBA_Any::operator = (const VARIANT &src) 
{
  this->~CORBA_Any ();

  assert ((src.vt & 0xB000) == 0);    // XXX better, report exception

  switch (src.vt & 0x0fff) 
    {
    case VT_EMPTY:
      _type = _tc_CORBA_Void;
      _value = 0;
      break;
        
    case VT_NULL:
      _type = _tc_CORBA_Null;
      _value = 0;
      break;
        
    case VT_I2:
      _type = _tc_CORBA_Short;
      _value = new CORBA_Short ((src.vt & VT_BYREF) 
				? (*src.piVal) : src.iVal);
      break;

    case VT_I4:
      _type = _tc_CORBA_Long;
      _value = new CORBA_Long ((src.vt & VT_BYREF) 
			       ? (*src.plVal) : src.lVal);
      break;

    case VT_R4:
      _type = _tc_CORBA_Float;
      _value = new CORBA_Float ((src.vt & VT_BYREF) 
				? (*src.pfltVal) : src.fltVal);
      break;

    case VT_R8:
      _type = _tc_CORBA_Double;
      _value = new CORBA_Double ((src.vt & VT_BYREF) 
				 ? (*src.pdblVal) : src.dblVal);
      break;

      // case VT_CY:
      // case VT_DATE:
      // XXX convert currency and date to TBD CORBA conventions

      // case VT_BSTR:
      // XXX convert to CORBA string

      // case VT_DISPATCH:
      // case VT_UNKNOWN:
      // case VT_VARIANT:
      // XXX convert to CORBA objref or appropriate pseudo-objref

      // case VT_BOOL:
      // XXX convert to CORBA boolean

      // case VT_ERROR:
      // XXX what to do?

    case VT_UI1:
      _type = _tc_CORBA_Octet;
      _value = new CORBA_Octet ((src.vt & VT_BYREF) 
				? (*src.pbVal) : src.bVal);
      break;

    default:
      // XXX report some exception ... throw it?
      _type = _tc_CORBA_Void;
      _value = 0;
      break;
    }

  return *this;
}

CORBA_Any::operator VARIANT () 
{
  VARIANT retval;

  // XXX convert it ... or report exception somehow!

  retval.vt = VT_EMPTY;
  return retval;
}