summaryrefslogtreecommitdiff
path: root/TAO/tao/Typecode.h
blob: 4742ae45da6dd86769cacf539bbbd26cb6d35390 (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
// This may look like C, but it's really -*- C++ -*-
// $Id$

// ============================================================================
//
// = LIBRARY
//    TAO
//
// = FILENAME
//    Typecode.h
//
// = DESCRIPTION
//    Header file CORBA's "TypeCode" type.
//
// = AUTHOR
//     Copyright 1994-1995 by Sun Microsystems, Inc.
//
// ============================================================================

#ifndef TAO_TYPECODE_H
#define TAO_TYPECODE_H

#include "ace/Synch.h"

#include "tao/Exception.h"

// Forward decl.
class TAO_InputCDR;

// A TypeCode describes data.  This one's as thin a wrapper around CDR
// octet sequences as is practical.  There are guesses here about how
// the OMG C++ mapping and CORBA 2.0 IFR specification will interact.
//
// NOTE: Use TypeCode_ptr, except in code (e.g. output of and OMG-IDL
// compiler) that needs to create typecodes from their octet-sequence
// encodings.

class TC_Private_State;

class TAO_Export CORBA_TypeCode
{
  // = TITLE
  //   The CORBA TypeCode class. It maintains the in-memory
  //   representation of any OMG CORBA IDL data type.
  //
  // = DESCRIPTION
  //   Implements the CORBA::TypeCode interface specified by CORBA 2.0
  //   spec.  Typecodes essentially consist of just the CDR octets
  //   that get marshaled and unmarshaled, and this code knows how to
  //   parse those octets and answer questions CORBA's TypeCode APIs
  //   require.
  //
  //   NOTE: This isn't well tuned performance-wise.  Given how much
  //   is variable (byte order, alignment) it's clear tuning has its
  //   limits with respect to CDR bytecode interpretation.
  //
  //   THREADING NOTE: Typecodes are readonly data structures, and the
  //   only mutual exclusion relates to reference counting and
  //   construction.
public:

  // Two "user exceptions" are defined for manipulating TypeCodes. These
  // two classes are defined inside the TypeCode class.
  class TAO_Export Bounds : public CORBA_UserException
  {
  public:
    Bounds (void);

    virtual void _raise (void);
    Bounds* _narrow (CORBA_Exception *ex);
    virtual int _is_a (const char* interface_id) const;
  };

  class TAO_Export BadKind : public CORBA_UserException
  {
  public:
    BadKind (void);

    virtual void _raise (void);
    BadKind* _narrow (CORBA_Exception *ex);
    virtual int _is_a (const char* interface_id) const;
  };

  static CORBA::TypeCode_ptr _tc_Bounds;
  static CORBA::TypeCode_ptr _tc_BadKind;

  static CORBA::TypeCode_ptr _duplicate (CORBA::TypeCode_ptr tc);
  // Duplicates i.e., increments ref count.

  static CORBA::TypeCode_ptr _nil (void);
  // Returns a NULL typecode.

  CORBA::Boolean equal (const CORBA::TypeCode_ptr,
                        CORBA_Environment &TAO_IN_ENV = CORBA::default_environment ()) const;
  // Compares two typecodes.

  CORBA::Boolean equivalent (CORBA::TypeCode_ptr,
                             CORBA_Environment &TAO_IN_ENV = CORBA::default_environment ()) const;
  // Unaliases receiver and argument before comparing.

  CORBA::TCKind kind (CORBA_Environment &TAO_IN_ENV = CORBA::default_environment ()) const;
  // For all TypeCode kinds, returns the "kind" of the typecode.

  const char *id (CORBA_Environment &TAO_IN_ENV = CORBA::default_environment ()) const;
  // For tk_{objref,struct,union,enum,alias,except}. Returns the
  // repository ID, raises BadKind.

  const char *name (CORBA_Environment &TAO_IN_ENV = CORBA::default_environment ()) const;
  // Returns name (), raises (BadKind).

  CORBA::ULong member_count (CORBA_Environment &TAO_IN_ENV = CORBA::default_environment ()) const;
  // Returns member_count (), raises (BadKind). Useful for tk_struct,
  // tk_union, tk_enum, tk_alias, and tk_except.

  const char *member_name (CORBA::ULong index,
                           CORBA_Environment &TAO_IN_ENV = CORBA::default_environment ()) const;
  // Returns member_name (...), raises (BadKind, Bounds); Useful for
  // tk_struct, tk_union, tk_enum, tk_alias, and tk_except.

  CORBA::TypeCode_ptr member_type (CORBA::ULong index,
                                   CORBA_Environment &TAO_IN_ENV = CORBA::default_environment ()) const;
  // Returns member_type (...), raises (BadKind, Bounds); Useful for
  // tk_struct, tk_union, and tk_except.

  CORBA::Any_ptr member_label (CORBA::ULong n,
                               CORBA_Environment &TAO_IN_ENV = CORBA::default_environment ()) const;
  // For tk_union. Returns the label. Raises BadKind, Bounds.

  CORBA::TypeCode_ptr discriminator_type (CORBA_Environment &TAO_IN_ENV = CORBA::default_environment ()) const;
  // Returns the discriminator type for tk_union. raises (BadKind).

  CORBA::Long default_index (CORBA_Environment &TAO_IN_ENV = CORBA::default_environment ()) const;
  // Returns the default index for the tk_union. Raises (BadKind).

  CORBA::ULong length (CORBA_Environment &TAO_IN_ENV = CORBA::default_environment ()) const;
  // Returns length, raises (BadKind). Used for tk_string,
  // tk_sequence, and tk_array.

  CORBA::TypeCode_ptr content_type (CORBA_Environment &TAO_IN_ENV = CORBA::default_environment ()) const;
  // Returns the content type (element type). Raises (BadKind); Useful
  // for tk_sequence, tk_array, and tk_alias.

  CORBA::ULong TAO_discrim_pad_size (CORBA_Environment &TAO_IN_ENV = CORBA::default_environment ());
  // Calculates the padded size of discriminant type TAO Extension.

  // = Creation/refcounting

  // These aren't really public APIs, but an IDL compiler will need to
  // be able to create TypeCodes as part of creating stubs.

  CORBA_TypeCode (CORBA::TCKind kind);
  // This constructor is used only for built-in TypeCode constants,
  // with no parameters.

  CORBA_TypeCode (CORBA::TCKind kind,
                  size_t length,
                  const char *buffer,
                  CORBA::Boolean orb_owns_tc,
                  CORBA::ULong size,
                  CORBA::TypeCode_ptr parent = 0);
  // This constructor is used both for typecode constants and for
  // heap-allocated TypeCodes.  The two are distinguished by the
  // orb_owns_tc flag passed in by the creator.
  //
  // For simple param lists with a single numeric parameter, only
  // 'length' matters.
  //
  // For complex param lists, or simple param lists for which the
  // parameter is a string or typecode, length _and_ buffer matter.
  //
  // For typecodes that are precomputed from the encapsulation stream
  // of the parent, even the "parent" argument matters because this
  // implies that all children will share the octet buffers of its
  // parent

  ~CORBA_TypeCode (void);
  // destructor

  enum traverse_status
  {
    TRAVERSE_STOP,
    TRAVERSE_CONTINUE
  };
  // these are used to indicate the status of marshaling

  size_t size (CORBA_Environment &TAO_IN_ENV = CORBA::default_environment ());
  // returns the size. Used by the IIOP marshaling engine.

  size_t alignment (CORBA_Environment &TAO_IN_ENV = CORBA::default_environment ());
  // returns the alignment requirements for this typecode. used by the
  // IIOP marshaling engine.

  // Reference counting operations.
  CORBA::ULong _incr_refcnt (void);
  CORBA::ULong _decr_refcnt (void);

  // = Following three are deprecated

  CORBA::ULong param_count (CORBA_Environment &TAO_IN_ENV = CORBA::default_environment ()) const;
  // Deprecated, CORBA 1.2, not fully usable. Returns the number of
  // parameters that the typecode takes.

  CORBA::ULong ulong_param (CORBA::ULong n,
                            CORBA_Environment &TAO_IN_ENV = CORBA::default_environment ()) const;

  CORBA::TypeCode_ptr typecode_param (CORBA::ULong n,
                                      CORBA_Environment &TAO_IN_ENV = CORBA::default_environment ()) const;
  // Internal utilities, pending CORBA 2.0 IFR APIs; just enough to
  // make array and sequence typecode interpretation cheap

  // private:
  //
  // = The guts of the typecode implementation class

  // This is implemented as a counted set of bytes, in marshaled CDR
  // format.

  size_t length_;
  // length of the encapsulated stream

  const char* buffer_;
  // the encapsulated stream

  CORBA::Long byte_order_;
  // The byte order in the encapsulated stream.

  CORBA::TCKind kind_;
  // the TypeCode kind

  CORBA::TypeCode_ptr parent_;
  // Indirected typecodes share "buffer" with a parent, and hold a
  // reference to that parent to ensure its memory is not freed
  // inappropriately.

  static CORBA::Boolean skip_typecode (TAO_InputCDR &stream);
  // skip a typecode encoding in a given CDR stream.  This is just a
  // helper function.

private:
  // = All the private/helper methods

  CORBA::Boolean private_equal (CORBA::TypeCode_ptr tc,
                                CORBA_Environment &TAO_IN_ENV = CORBA::default_environment ()) const;
  // Compares the typecodes.

  const char *private_id (CORBA_Environment &TAO_IN_ENV = CORBA::default_environment ()) const;
  // For tk_{objref,struct,union,enum,alias,except}. Returns the
  // repository ID, raises BadKind.

  const char *private_name (CORBA_Environment &TAO_IN_ENV = CORBA::default_environment ()) const;
  // returns name (), raises (BadKind)

  CORBA::ULong private_member_count (CORBA_Environment &TAO_IN_ENV = CORBA::default_environment ()) const;
  // returns member_count (), raises (BadKind). Useful for tk_struct,
  // tk_union, tk_enum, tk_alias, and tk_except.

  CORBA::TypeCode_ptr private_member_type (CORBA::ULong index,
                                           CORBA_Environment &TAO_IN_ENV = CORBA::default_environment ()) const;
  // returns member_type (...), raises (BadKind, Bounds); Useful for
  // tk_struct, tk_union, and tk_except

  const char *private_member_name (CORBA::ULong index,
                                    CORBA_Environment &TAO_IN_ENV = CORBA::default_environment ()) const;
  // returns member_name (...), raises (BadKind, Bounds); Useful for tk_union,
  // tk_struct, tk_except, and tk_enum

  CORBA::Any_ptr private_member_label (CORBA::ULong n,
                                       CORBA_Environment &TAO_IN_ENV = CORBA::default_environment ()) const;
  // For tk_union. Returns the label. Raises BadKind, Bounds.

  CORBA::TypeCode_ptr private_discriminator_type (CORBA_Environment &TAO_IN_ENV = CORBA::default_environment ()) const;
  // returns the discriminator type for tk_union. raises (BadKind);

  CORBA::Long private_default_index (CORBA_Environment &TAO_IN_ENV = CORBA::default_environment ()) const;
  // returns the default index for the tk_union. Raises (BadKind);

  CORBA::Long private_length (CORBA_Environment &TAO_IN_ENV = CORBA::default_environment ()) const;
  // returns length, raises (BadKind). Used for tk_string,
  // tk_sequence, and tk_array

  CORBA::TypeCode_ptr private_content_type (CORBA_Environment &TAO_IN_ENV = CORBA::default_environment ()) const;
  // returns the content type (element type). Raises (BadKind); Useful
  // for tk_sequence, tk_array, and tk_alias

  size_t private_size (CORBA_Environment &TAO_IN_ENV = CORBA::default_environment ());
  // returns the size. Used by the IIOP marshaling engine.

  size_t private_alignment (CORBA_Environment &TAO_IN_ENV = CORBA::default_environment ());
  // returns the alignment requirements for this typecode. used by the
  // IIOP marshaling engine.

  CORBA::ULong private_discrim_pad_size (CORBA_Environment &TAO_IN_ENV = CORBA::default_environment ());
  // Calculates the padded size of discriminant type TAO Extension

  // = All the private helpers testing for equality of typecodes

  CORBA::Boolean private_equal_objref (CORBA::TypeCode_ptr tc,
                                       CORBA_Environment &TAO_IN_ENV = CORBA::default_environment ()) const;
  // test equality for typecodes of objrefs

  CORBA::Boolean private_equal_struct (CORBA::TypeCode_ptr tc,
                                       CORBA_Environment &TAO_IN_ENV = CORBA::default_environment ()) const;
  // test equality for typecodes of structs

  CORBA::Boolean private_equal_union (CORBA::TypeCode_ptr tc,
                                      CORBA_Environment &TAO_IN_ENV = CORBA::default_environment ()) const;
  // test equality for typecodes of unions

  CORBA::Boolean private_equal_enum (CORBA::TypeCode_ptr tc,
                                     CORBA_Environment &TAO_IN_ENV = CORBA::default_environment ()) const;
  // test equality for typecodes of enums

  CORBA::Boolean private_equal_string (CORBA::TypeCode_ptr tc,
                                       CORBA_Environment &TAO_IN_ENV = CORBA::default_environment ()) const;
  // test equality for typecodes of strings

  CORBA::Boolean private_equal_wstring (CORBA::TypeCode_ptr tc,
                                        CORBA_Environment &TAO_IN_ENV = CORBA::default_environment ()) const;
  // test equality for typecodes of wide strings

  CORBA::Boolean private_equal_sequence (CORBA::TypeCode_ptr tc,
                                         CORBA_Environment &TAO_IN_ENV = CORBA::default_environment ()) const;
  // test equality for typecodes of sequences

  CORBA::Boolean private_equal_array (CORBA::TypeCode_ptr tc,
                                      CORBA_Environment &TAO_IN_ENV = CORBA::default_environment ()) const;
  // test equality for typecodes of array

  CORBA::Boolean private_equal_alias (CORBA::TypeCode_ptr tc,
                                      CORBA_Environment &TAO_IN_ENV = CORBA::default_environment ()) const;
  // test equality for typecodes of typedefs

  CORBA::Boolean private_equal_except (CORBA::TypeCode_ptr tc,
                                       CORBA_Environment &TAO_IN_ENV = CORBA::default_environment ()) const;
  // test equality for typecodes of exceptions

  CORBA::ULong refcount_;
  // if refcount reaches 0, free this typecode

  ACE_SYNCH_MUTEX refcount_lock_;
  // Protect the reference count, this is OK because we do no
  // duplicates or releases on the critical path.

  CORBA::Boolean orb_owns_;
  // TAO's approach differs from the SunSoft IIOP. Constant typecodes
  // are owned by the ORB and get freed only when the ORB dies.

  // If "orb_owns" is false, the value is a not a constant typecode
  // with both the typecode and the buffer allocated (typically, this
  // will be created by the IDL compiler generated code)

  TC_Private_State *private_state_;
  // maintains precomputed state. We need a separate class that
  // maintains the precomputed state since most of the TypeCode class
  // operations keep the state of the object constant. However, for
  // the purpose of precomputation, we need to update the state. We
  // cannot update state directly in the TypeCode class as that
  // defeats the constness. However, we can keep an object in our
  // typecode class that remains constant, but we can update its
  // state.

  // = No copy constructor or assignment operator supported;

  // Use TypeCode_ptr values, duplicate (), release ().
  CORBA_TypeCode (const CORBA::TypeCode &src);
  CORBA_TypeCode &operator = (const CORBA::TypeCode &src);

  char *non_aligned_buffer_;
  // Original buffer that may possibly be non-aligned. We still need a
  // handle to the allocated memory so that all of it can be freed by
  // the destructor.
};

class TAO_Export TC_Private_State
  // = TITLE
  //   Private state of the TypeCode.
  //
  // = DESCRIPTION
  //   Used to store precomputed values
{
public:
  TC_Private_State (CORBA::TCKind kind);
  // constructor

  ~TC_Private_State (void);
  // destructor

  ACE_SYNCH_MUTEX mutex_;

  CORBA::TCKind  tc_kind_;
  // our kind that will determine what kind of children we may have

  // = data members that indicate if the desired quantify was precomputed or not.
  CORBA::Boolean tc_id_known_;
  CORBA::Boolean tc_name_known_;
  CORBA::Boolean tc_member_count_known_;
  CORBA::Boolean tc_member_type_list_known_;
  CORBA::Boolean tc_member_name_list_known_;
  CORBA::Boolean tc_member_label_list_known_;
  CORBA::Boolean tc_discriminator_type_known_;
  CORBA::Boolean tc_default_index_used_known_;
  CORBA::Boolean tc_length_known_;
  CORBA::Boolean tc_content_type_known_;
  CORBA::Boolean tc_size_known_;
  CORBA::Boolean tc_alignment_known_;
  CORBA::Boolean tc_discrim_pad_size_known_;

  // = These data members store the precomputed values
  CORBA::String  tc_id_;
  CORBA::String  tc_name_;
  CORBA::ULong   tc_member_count_;
  CORBA::TypeCode_ptr *tc_member_type_list_;
  char **tc_member_name_list_;
  CORBA::Any_ptr *tc_member_label_list_;
  CORBA::TypeCode_ptr tc_discriminator_type_;
  CORBA::Long   tc_default_index_used_;
  CORBA::ULong   tc_length_;
  CORBA::TypeCode_ptr  tc_content_type_;
  CORBA::ULong  tc_size_;
  CORBA::ULong  tc_alignment_;
  CORBA::ULong  tc_discrim_pad_size_;

};

class TAO_Export CORBA_TypeCode_var
{
  //
  // = TITLE
  //   A smart pointer for TypeCodes.
  //
  // = DESCRIPTION
  //   Implements the _var class for the TypeCode pseudo object.
  //
public:
  CORBA_TypeCode_var (void); // default constructor
  CORBA_TypeCode_var (CORBA_TypeCode_ptr);
  CORBA_TypeCode_var (const CORBA_TypeCode_var &); // copy constructor
  ~CORBA_TypeCode_var (void); // destructor

  CORBA_TypeCode_var &operator= (CORBA_TypeCode_ptr);
  CORBA_TypeCode_var &operator= (const CORBA_TypeCode_var &);
  CORBA_TypeCode_ptr operator-> (void) const;

  operator const CORBA_TypeCode_ptr &() const;
  operator CORBA_TypeCode_ptr &();
  // in, inout, out, _retn
  CORBA_TypeCode_ptr in (void) const;
  CORBA_TypeCode_ptr &inout (void);
  CORBA_TypeCode_ptr &out (void);
  CORBA_TypeCode_ptr _retn (void);
  CORBA_TypeCode_ptr ptr (void) const;

private:
  CORBA_TypeCode_ptr ptr_;
};

class TAO_Export CORBA_TypeCode_out
{
  //
  // = TITLE
  //   The _out class for TypeCode.
  //
  // = DESCRIPTION
  //   Implements the _out class for the TypeCode pseudo object.
  //
public:
  CORBA_TypeCode_out (CORBA_TypeCode_ptr &);
  CORBA_TypeCode_out (CORBA_TypeCode_var &);
  CORBA_TypeCode_out (const CORBA_TypeCode_out &);
  CORBA_TypeCode_out &operator= (const CORBA_TypeCode_out &);
  CORBA_TypeCode_out &operator= (const CORBA_TypeCode_var &);
  CORBA_TypeCode_out &operator= (CORBA_TypeCode_ptr);
  operator CORBA_TypeCode_ptr &();
  CORBA_TypeCode_ptr &ptr (void);
  CORBA_TypeCode_ptr operator-> (void);

private:
  CORBA_TypeCode_ptr &ptr_;
};

class TAO_Export TAO_TypeCodes
{
  // = TITLE
  //   This class is a namespace for TypeCode-related static data that is owned
  //   by the ORB.
public:

  static void init (void);
  // Runtime initialization of all standard typecodes.
  // Called from <CORBA::ORB_init>.

  static void fini (void);
  // Runtime finalization of all standard typecodes.

};

#if defined (__ACE_INLINE__)
# include "tao/Typecode.i"
#endif /* __ACE_INLINE__ */

#endif /* TAO_TYPECODE_H */