OpenCores
URL https://opencores.org/ocsvn/scarts/scarts/trunk

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-gcc/] [gcc-4.1.1/] [libjava/] [gnu/] [CORBA/] [DynAn/] [gnuDynValue.java] - Blame information for rev 22

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 14 jlechner
/* gnuDynValue.java --
2
   Copyright (C) 2005 Free Software Foundation, Inc.
3
 
4
This file is part of GNU Classpath.
5
 
6
GNU Classpath is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2, or (at your option)
9
any later version.
10
 
11
GNU Classpath is distributed in the hope that it will be useful, but
12
WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with GNU Classpath; see the file COPYING.  If not, write to the
18
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
02110-1301 USA.
20
 
21
Linking this library statically or dynamically with other modules is
22
making a combined work based on this library.  Thus, the terms and
23
conditions of the GNU General Public License cover the whole
24
combination.
25
 
26
As a special exception, the copyright holders of this library give you
27
permission to link this library with independent modules to produce an
28
executable, regardless of the license terms of these independent
29
modules, and to copy and distribute the resulting executable under
30
terms of your choice, provided that you also meet, for each linked
31
independent module, the terms and conditions of the license of that
32
module.  An independent module is a module which is not derived from
33
or based on this library.  If you modify this library, you may extend
34
this exception to your version of the library, but you are not
35
obligated to do so.  If you do not wish to do so, delete this
36
exception statement from your version. */
37
 
38
 
39
package gnu.CORBA.DynAn;
40
 
41
import gnu.CORBA.Minor;
42
import gnu.CORBA.Unexpected;
43
 
44
import org.omg.CORBA.Any;
45
import org.omg.CORBA.BAD_PARAM;
46
import org.omg.CORBA.MARSHAL;
47
import org.omg.CORBA.ORB;
48
import org.omg.CORBA.TCKind;
49
import org.omg.CORBA.TypeCode;
50
import org.omg.CORBA.VM_TRUNCATABLE;
51
import org.omg.CORBA.portable.OutputStream;
52
import org.omg.CORBA.portable.ValueFactory;
53
import org.omg.DynamicAny.DynAny;
54
import org.omg.DynamicAny.DynAnyPackage.InvalidValue;
55
import org.omg.DynamicAny.DynAnyPackage.TypeMismatch;
56
import org.omg.DynamicAny.DynStruct;
57
import org.omg.DynamicAny.DynValue;
58
import org.omg.DynamicAny.DynValueCommon;
59
import org.omg.DynamicAny.DynValueOperations;
60
import org.omg.DynamicAny.NameDynAnyPair;
61
import org.omg.DynamicAny.NameValuePair;
62
 
63
import java.io.Serializable;
64
 
65
/**
66
 * Implementation of DynValue.
67
 *
68
 * @author Audrius Meskauskas, Lithuania (AudriusA@Bioinformatics.org)
69
 */
70
public class gnuDynValue extends RecordAny implements DynValue,
71
  Serializable
72
{
73
  /**
74
   * Use serialVersionUID for interoperability.
75
   */
76
  private static final long serialVersionUID = 1;
77
 
78
  /**
79
   * If true, the value of this ValueType is set to null.
80
   */
81
  boolean isNull;
82
 
83
  /**
84
   * Create an instance.
85
   */
86
  public gnuDynValue(TypeCode oType, TypeCode aType,
87
    gnuDynAnyFactory aFactory, ORB anOrb
88
  )
89
  {
90
    super(oType, aType, aFactory, anOrb);
91
 
92
    // Initialise fields. The array of fields also includes all inherited
93
    // fields.
94
    try
95
      {
96
        array = new DynAny[ final_type.member_count() ];
97
        fNames = new String[ array.length ];
98
        for (int i = 0; i < array.length; i++)
99
          {
100
            array [ i ] =
101
              factory.create_dyn_any_from_type_code(final_type.member_type(i));
102
            fNames [ i ] = final_type.member_name(i);
103
          }
104
 
105
        // Search of inherited members.
106
        if (final_type.type_modifier() == VM_TRUNCATABLE.value)
107
          {
108
            TypeCode parent = final_type.concrete_base_type();
109
            DynAny ancestor = factory.create_dyn_any_from_type_code(parent);
110
 
111
            if (ancestor instanceof DynValue)
112
              {
113
                // Add members of ancestor in front of the curren members.
114
                DynValue anc = (DynValue) ancestor;
115
                anc.set_to_value();
116
 
117
                NameDynAnyPair[] aar = anc.get_members_as_dyn_any();
118
                inheritFields(aar);
119
              }
120
            else if (ancestor instanceof DynStruct)
121
              {
122
                // Add members of ancestor in front of the curren members.
123
                DynStruct anc = (DynStruct) ancestor;
124
                NameDynAnyPair[] aar = anc.get_members_as_dyn_any();
125
                inheritFields(aar);
126
              }
127
            else
128
              throw new BAD_PARAM("The parent of " + final_type.id() + ", " +
129
                parent.id() + ", is not structure nor value."
130
              );
131
          }
132
      }
133
    catch (Exception e)
134
      {
135
        throw new Unexpected(e);
136
      }
137
 
138
    set_to_null();
139
  }
140
 
141
  /**
142
   * Inherit the provided fields.
143
   */
144
  private void inheritFields(NameDynAnyPair[] aar)
145
  {
146
    DynAny[] nArray = new DynAny[ array.length + aar.length ];
147
    String[] nNames = new String[ array.length + aar.length ];
148
    int p = 0;
149
    for (int i = 0; i < aar.length; i++)
150
      {
151
        nArray [ p ] = aar [ i ].value;
152
        nNames [ p ] = aar [ i ].id;
153
        p++;
154
      }
155
 
156
    for (int i = 0; i < array.length; i++)
157
      {
158
        nArray [ p ] = array [ i ];
159
        nNames [ p ] = fNames [ i ];
160
        p++;
161
      }
162
 
163
    array = nArray;
164
    fNames = nNames;
165
  }
166
 
167
  /** @inheritDoc */
168
  public TCKind current_member_kind() throws TypeMismatch, InvalidValue
169
  {
170
    if (isNull)
171
      throw new TypeMismatch(ISNULL);
172
    else
173
      return super.current_member_kind();
174
  }
175
  ;
176
 
177
  /** @inheritDoc */
178
  public String current_member_name() throws TypeMismatch, InvalidValue
179
  {
180
    if (isNull)
181
      throw new TypeMismatch(ISNULL);
182
    else
183
      return super.current_member_name();
184
  }
185
  ;
186
 
187
  /** @inheritDoc */
188
  public NameDynAnyPair[] get_members_as_dyn_any() throws InvalidValue
189
  {
190
    if (isNull)
191
      throw new InvalidValue(ISNULL);
192
    return super.gnu_get_members_as_dyn_any();
193
  }
194
  ;
195
 
196
  /** @inheritDoc */
197
  public NameValuePair[] get_members() throws InvalidValue
198
  {
199
    if (isNull)
200
      throw new InvalidValue(ISNULL);
201
    else
202
      return super.gnu_get_members();
203
  }
204
  ;
205
 
206
  /** @inheritDoc */
207
  public void set_members_as_dyn_any(NameDynAnyPair[] value)
208
    throws TypeMismatch, InvalidValue
209
  {
210
    super.set_members_as_dyn_any(value);
211
    isNull = false;
212
  }
213
  ;
214
 
215
  /** @inheritDoc */
216
  public void set_members(NameValuePair[] value)
217
    throws TypeMismatch, InvalidValue
218
  {
219
    super.set_members(value);
220
    isNull = false;
221
  }
222
  ;
223
 
224
  /** @inheritDoc */
225
  public boolean is_null()
226
  {
227
    return isNull;
228
  }
229
 
230
  /** @inheritDoc */
231
  public void set_to_null()
232
  {
233
    isNull = true;
234
    valueChanged();
235
  }
236
 
237
  /** @inheritDoc */
238
  public void set_to_value()
239
  {
240
    isNull = false;
241
    valueChanged();
242
  }
243
 
244
  /**
245
   * Create a new instance.
246
   */
247
  protected RecordAny newInstance(TypeCode oType, TypeCode aType,
248
    gnuDynAnyFactory aFactory, ORB anOrb
249
  )
250
  {
251
    gnuDynValue v = new gnuDynValue(oType, aType, aFactory, anOrb);
252
    if (isNull)
253
      v.set_to_null();
254
    else
255
      v.set_to_value();
256
    return v;
257
  }
258
 
259
  /**
260
   * Compare for equality, minding null values.
261
   */
262
  public boolean equal(DynAny other)
263
  {
264
    if (other instanceof DynValueOperations)
265
      {
266
        DynValueCommon o = (DynValueCommon) other;
267
        if (isNull)
268
          return o.is_null() && o.type().equal(official_type);
269
        else
270
          return !o.is_null() && record_equal(other); // GCJ LOCAL bug #24938
271
      }
272
    else
273
      return false;
274
  }
275
 
276
  /**
277
   * Get the focused component, throwing exception if the current value is null.
278
   */
279
  protected DynAny focused() throws InvalidValue, TypeMismatch
280
  {
281
    if (isNull)
282
      throw new TypeMismatch(ISNULL);
283
    else
284
      return super.focused();
285
  }
286
 
287
  /**
288
   * Convert into Any.
289
   */
290
  public Any to_any()
291
  {
292
    if (isNull)
293
      {
294
        Any a0 = createAny();
295
        a0.type(orb.get_primitive_tc(TCKind.tk_null));
296
        return a0;
297
      }
298
    else
299
      {
300
        try
301
          {
302
            ValueFactory factory =
303
              ((org.omg.CORBA_2_3.ORB) orb).lookup_value_factory(official_type.id());
304
            if (factory == null)
305
              {
306
                MARSHAL m = new MARSHAL("Factory for " + official_type.id() +
307
                " not registered.");
308
                m.minor = Minor.Factory;
309
                throw m;
310
              }
311
 
312
            OutputStream out = orb.create_output_stream();
313
 
314
            for (int i = 0; i < array.length; i++)
315
              array [ i ].to_any().write_value(out);
316
 
317
            org.omg.CORBA_2_3.portable.InputStream in =
318
              (org.omg.CORBA_2_3.portable.InputStream) out.create_input_stream();
319
            Serializable v = factory.read_value(in);
320
 
321
            Any g = createAny();
322
            g.type(official_type);
323
            g.insert_Value(v, official_type);
324
 
325
            return g;
326
          }
327
        catch (Exception e)
328
          {
329
            throw new Unexpected(e);
330
          }
331
      }
332
  }
333
 
334
  /** @inheritDoc */
335
  public void assign(DynAny from) throws TypeMismatch
336
  {
337
    checkType(official_type, from.type());
338
 
339
    if (from instanceof DynValue)
340
      {
341
        DynValue other = (DynValue) from;
342
        if (other.is_null())
343
          set_to_null();
344
        else
345
          {
346
            set_to_value();
347
            try
348
              {
349
                DynValueOperations src = (DynValueOperations) from;
350
                set_members_as_dyn_any(src.get_members_as_dyn_any());
351
              }
352
            catch (InvalidValue e)
353
              {
354
                TypeMismatch t = new TypeMismatch("Invalid value");
355
                t.initCause(e);
356
                throw t;
357
              }
358
          }
359
      }
360
    else
361
      throw new TypeMismatch("Not a DynValue");
362
  }
363
 
364
  /**
365
   * Get the number of components.
366
   */
367
  public int component_count()
368
  {
369
    return isNull ? 0 : record_component_count(); // GCJ LOCAL bug #24938
370
  }
371
 
372
  /** {@inheritDoc} */
373
  public Serializable get_val() throws TypeMismatch, InvalidValue
374
  {
375
    return to_any().extract_Value();
376
  }
377
 
378
  /** {@inheritDoc} */
379
  public void insert_val(Serializable a_x) throws InvalidValue, TypeMismatch
380
  {
381
    Any a = to_any();
382
    a.insert_Value(a_x);
383
    from_any(a);
384
    valueChanged();
385
  }
386
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.