summaryrefslogtreecommitdiff
path: root/TAO/TAO_IDL/be/be_visitor_sequence/sequence_cs.cpp
blob: 13609f31b03f67b208da2b7939b4d43af10cc412 (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

//=============================================================================
/**
 *  @file    sequence_cs.cpp
 *
 *  Visitor generating code for Sequences in the client stubs file
 *
 *  @author Aniruddha Gokhale
 */
//=============================================================================

#include "sequence.h"

// ************************************************************
// Root visitor for client stub class
// ************************************************************

be_visitor_sequence_cs::be_visitor_sequence_cs (be_visitor_context *ctx)
  : be_visitor_decl (ctx)
{
}

be_visitor_sequence_cs::~be_visitor_sequence_cs ()
{
}

int be_visitor_sequence_cs::visit_sequence (be_sequence *node)
{
  // First create a name for ourselves.
  if (node->create_name (this->ctx_->tdef ()) == -1)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         ACE_TEXT ("be_visitor_sequence_cs::")
                         ACE_TEXT ("visit_sequence - ")
                         ACE_TEXT ("failed creating name\n")),
                        -1);
    }

  // We don't check cli_stub_gen() here. If we are generated more
  // than once as an anonymous sequence, the name guard will cause
  // the C++ preprocessor to catch it. If we are generated more than
  // once as a typedef (caused by a comma separated list of
  // typedefs), our name will be changed by the call above and the
  // name guard will not catch it, but that's ok - we want to
  // be generated for each typedef.
  if (node->imported ())
    {
      return 0;
    }

  if (idl_global->dcps_sequence_type_defined (node->full_name ()))
    {
      return 0;
    }

  be_type *bt = dynamic_cast<be_type*> (node->base_type ());

  if (bt == nullptr)
    {
      ACE_ERROR_RETURN ((LM_ERROR,
                         ACE_TEXT ("be_visitor_sequence_cs::")
                         ACE_TEXT ("visit_sequence - ")
                         ACE_TEXT ("Bad element type\n")),
                        -1);
    }

  AST_Decl::NodeType nt = bt->node_type ();

  // If our base type is an anonymous sequence, we must create a name
  // and generate a class declaration for it as well.
  if (nt == AST_Decl::NT_sequence)
    {
      // Temporarily make the context's tdef node 0 so the nested call
      // to create_name will not get confused and give our anonymous
      // sequence element type the same name as we have.
      be_typedef *tmp = this->ctx_->tdef ();
      this->ctx_->tdef (nullptr);

      if (bt->accept (this) != 0)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             ACE_TEXT ("be_visitor_sequence_cs::")
                             ACE_TEXT ("visit_sequence - ")
                             ACE_TEXT ("codegen for anonymous ")
                             ACE_TEXT ("base type failed\n")),
                            -1);
        }

      // Restore the tdef value.
      this->ctx_->tdef (tmp);
    }

  if (be_global->alt_mapping () && node->unbounded ())
    {
      // We are just a typedef and don't need any stub source
      // code generation.
      return 0;
    }

  TAO_OutStream *os = this->ctx_->stream ();

  *os << be_nl_2;

  TAO_INSERT_COMMENT (os);

  os->gen_ifdef_macro (node->flat_name ());

  // for unbounded sequences, we have a different set of constructors
  if (node->unbounded ())
    {
      *os << be_nl_2
          << node->name () << "::" << node->local_name () << " ("
          << be_idt << be_idt_nl
          << "::CORBA::ULong max)" << be_uidt_nl
          << ": " << be_idt;

      int status =
        node->gen_base_class_name (os,
                                   "",
                                   this->ctx_->scope ()->decl ());

      // Pass it to the base constructor.
      if (status == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             ACE_TEXT ("be_visitor_sequence_cs::")
                             ACE_TEXT ("visit_sequence - ")
                             ACE_TEXT ("codegen for base ")
                             ACE_TEXT ("sequence class failed\n")),
                           -1);
        }


      *os << " (max)" << be_uidt << be_uidt_nl
          << "{}";
    }

  /// If we are using std::vector, we can't implement this
  /// constructor.
  if (!be_global->alt_mapping () || !node->unbounded ())
    {
      // Constructor with the buffer
      *os << be_nl_2
          << node->name () << "::" << node->local_name () << " ("
          << be_idt << be_idt_nl;

      if (node->unbounded ())
        {
          // Unbounded seq takes this extra parameter.
          *os << "::CORBA::ULong max," << be_nl;
        }

      *os << "::CORBA::ULong length," << be_nl;

      // generate the base type for the buffer
      be_visitor_context ctx (*this->ctx_);
      be_visitor_sequence_buffer_type bt_visitor (&ctx);

      if (bt->accept (&bt_visitor) == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             ACE_TEXT ("be_visitor_sequence_cs::")
                             ACE_TEXT ("visit_sequence - ")
                             ACE_TEXT ("base type visit failed\n")),
                            -1);
        }

      *os << " * buffer," << be_nl
          << "::CORBA::Boolean release)" << be_uidt
          << be_uidt_nl
          << "  : " << be_idt << be_idt;

      // Pass it to the base constructor.
      if (node->gen_base_class_name (os,
                                     "",
                                     this->ctx_->scope ()->decl ()) == -1)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             ACE_TEXT ("be_visitor_sequence_cs::")
                             ACE_TEXT ("visit_sequence - ")
                             ACE_TEXT ("codegen for base ")
                             ACE_TEXT ("sequence class\n")),
                            -1);
        }

      *os << be_nl << "(";

      if (node->unbounded ())
        {
          *os << "max, ";
        }

      *os << "length, buffer, release)" << be_uidt << be_uidt_nl
          << "{}";
  }

  if (be_global->alt_mapping () && node->unbounded ())
    {
      *os << be_nl_2
          << "::CORBA::ULong" << be_nl
          << node->name () << "::length () const" << be_nl
          << "{" << be_idt_nl
          << "return this->size ();" << be_uidt_nl
          << "}";

      *os << be_nl_2
          << "void" << be_nl
          << node->name () << "::length ( ::CORBA::ULong length)"
          << be_nl
          << "{" << be_idt_nl
          << "this->resize (length);" << be_uidt_nl
          << "}";

      *os << be_nl_2
          << "::CORBA::ULong" << be_nl
          << node->name () << "::maximum () const" << be_nl
          << "{" << be_idt_nl
          << "return this->capacity ();" << be_uidt_nl
          << "}";
    }

  if (be_global->any_support ()
      && !node->anonymous ()
      && (!node->is_local ()
          || be_global->gen_local_iface_anyops ()))
    {
      *os << be_nl_2
          << "void "
          << node->name () << "::_tao_any_destructor ("
          << be_idt << be_idt_nl
          << "void * _tao_void_pointer)" << be_uidt << be_uidt_nl
          << "{" << be_idt_nl
          << node->local_name () << " * _tao_tmp_pointer ="
          << be_idt_nl
          << "static_cast<" << node->local_name ()
          << " *> (_tao_void_pointer);" << be_uidt_nl
          << "delete _tao_tmp_pointer;" << be_uidt_nl
          << "}";
    }

  os->gen_endif ();

  node->cli_stub_gen (true);
  return 0;
}