summaryrefslogtreecommitdiff
path: root/TAO/examples/Simple/grid/Grid_i.cpp
blob: fd666f864b35bf0a85e8dfdb348dfdc945ddca7d (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
// -*- C++ -*-
// $Id$
#include "Grid_i.h"
#include "tao/corba.h"

// Default constructor.
Grid_i::Grid_i (void)
  : width_ (0),
    height_ (0),
    array_ (0)
{
  //no-op
}

//  Constructor.
Grid_i::Grid_i (CORBA::Short x,
                CORBA::Short y,
                CORBA::Environment &TAO_IN_ENV)
  : width_ (x),
    height_ (y)
{
  ACE_NEW_THROW (array_,
	         CORBA::Long *[y],
                 CORBA::NO_MEMORY (CORBA::COMPLETED_NO));

  // Allocate memory for the matrix.

  for (int ctr = 0; ctr < y; ctr++)
    ACE_NEW_THROW (array_[ctr],
	           CORBA::Long[x],
                   CORBA::NO_MEMORY (CORBA::COMPLETED_NO));
}

// Default destructor.

Grid_i::~Grid_i (void)
{
  // no-op.
}

//  Set a value in the grid.

void 
Grid_i::set (CORBA::Short x,
	     CORBA::Short y,
	     CORBA::Long value,
	     CORBA::Environment &TAO_IN_ENV)
{
  if (x < 0
      || y < 0
      || x >= width_
      || y >= height_)
    TAO_THROW (Grid::RANGE_ERROR ());
  else 
    array_[x][y] = value;
}

//  Get a value from  the grid.

CORBA::Long
Grid_i::get (CORBA::Short x,
             CORBA::Short y,
             CORBA::Environment &TAO_IN_ENV)
{
  if (x < 0
      || y < 0
      || x >= width_
      || y >= height_)
    TAO_THROW_RETURN (Grid::RANGE_ERROR (), 0);
  else 
    return array_[x][y];
}

// Access methods.

CORBA::Short 
Grid_i::width (CORBA::Environment &TAO_IN_ENV)
{
  return this->width_;
}

CORBA::Short 
Grid_i::height (CORBA::Environment &TAO_IN_ENV)
{
  return this->height_;
}

void 
Grid_i::width (CORBA::Short x,
	       CORBA::Environment &TAO_IN_ENV)
{
  this->width_ = x;
}

void 
Grid_i::height (CORBA::Short y,
		CORBA::Environment &TAO_IN_ENV)
{
  this->height_ = y;
}

// Destroy the grid

void 
Grid_i::destroy (CORBA::Environment &TAO_IN_ENV)
{
  // Delete the array.
  for (int i = 0; i < height_; i++)
      delete [] array_[i];

  delete [] array_;

  ACE_DEBUG ((LM_DEBUG,
              "(%P|%t) %s\n",
              "Grid has been destroyed"));
}

// Set the ORB pointer.

void
Grid_Factory_i::orb (CORBA::ORB_ptr o)
{
  this->orb_ = CORBA::ORB::_duplicate (o);
}

// Shutdown.

void 
Grid_Factory_i::shutdown (CORBA::Environment &)
{
  ACE_DEBUG ((LM_DEBUG,
              "(%P|%t) %s\n",
              "Grid Factory is shutting down"));

  // Instruct the ORB to shutdown.
  this->orb_->shutdown ();
}

// Constructor
Grid_Factory_i::Grid_Factory_i (void)
{
  // no-op
}

// Destructor
Grid_Factory_i::~Grid_Factory_i (void)
{
  // no-op
}

// Make a <Grid>.

Grid_ptr 
Grid_Factory_i::make_grid (CORBA::Short width,
                           CORBA::Short height,
                           CORBA::Environment &TAO_IN_ENV)
{
  Grid_i *grid_ptr = 0;
 
  ACE_DEBUG ((LM_DEBUG,
              "(%P|%t) Making a new Grid\n"));

  // Set a default value for width.
  if (width <= 0)
    width = Grid_Factory::DEFAULT_WIDTH;
 
  // Set a default value for height.
  if (height <= 0)
    height = Grid_Factory::DEFAULT_HEIGHT;

  // This attempts to create a new Grid_i and throws an exception and
  // returns a null value if it fails
  ACE_NEW_THROW_RETURN (grid_ptr,
			Grid_i (width, height, TAO_IN_ENV),
			CORBA::NO_MEMORY (CORBA::COMPLETED_NO),
			Grid::_nil ());

  // Register the Grid pointer.
  return grid_ptr->_this (TAO_IN_ENV);
}