Merge pull request #2301 from sonndinh/remove-dup-reactor-functions
[ACE_TAO.git] / TAO / examples / Persistent_Grid / Grid_i.cpp
blob8be57e517368dd933fa20d30eb36859dc7819812
1 // -*- C++ -*-
2 #include "Grid_i.h"
4 // Default constructor.
6 Grid_i::Grid_i ()
7 : width_ (0),
8 height_ (0),
9 array_ (0)
11 //no-op
14 // Constructor.
16 Grid_i::Grid_i (CORBA::Short x,
17 CORBA::Short y,
18 pool_t *mem_pool)
19 : width_ (x),
20 height_ (y),
21 array_ (0),
22 pool_t_ (0)
24 // First try to locate the matrix in the pool. If it is there then
25 // it has already been created. In such a case we just get that
26 // memory and assign it to array_
28 void *tmp_array (0);
30 if (mem_pool->find ("Array", tmp_array) != -1)
32 array_ = reinterpret_cast <CORBA::Long **> (tmp_array);
34 else
36 // Allocate memory for the matrix.
37 ACE_ALLOCATOR (array_,
38 static_cast<CORBA::Long **> (mem_pool->malloc (y * sizeof (CORBA::Long *))));
39 //array_ = (CORBA::Long **) mem_pool->malloc (y * sizeof (CORBA::Long *));
41 if (array_ != 0)
43 for (int ctr = 0; ctr < y; ctr++)
45 ACE_ALLOCATOR (array_[ctr],
46 static_cast<CORBA::Long *> (mem_pool->malloc (x *
47 sizeof (CORBA::Long ))));
49 //array_[ctr] = (CORBA::Long *)mem_pool->malloc (x *
50 // sizeof (CORBA::Long));
53 mem_pool->bind ("Array", array_);
59 // Default destructor.
61 Grid_i::~Grid_i ()
65 // Set a value in the grid.
67 void
68 Grid_i::set (CORBA::Short x,
69 CORBA::Short y,
70 CORBA::Long value)
72 if (x < 0
73 || y < 0
74 || x >= width_
75 || y >= height_)
76 throw Grid::RANGE_ERROR ();
77 else
78 array_[x][y] = value;
81 // Get a value from the grid.
83 CORBA::Long
84 Grid_i::get (CORBA::Short x,
85 CORBA::Short y)
87 if (x < 0
88 || y < 0
89 || x >= width_
90 || y >= height_)
91 throw Grid::RANGE_ERROR ();
92 else
93 return array_[x][y];
96 // Access methods.
98 CORBA::Short
99 Grid_i::width ()
101 return this->width_;
104 CORBA::Short
105 Grid_i::height ()
107 return this->height_;
110 void
111 Grid_i::width (CORBA::Short x)
113 this->width_ = x;
116 void
117 Grid_i::height (CORBA::Short y)
119 this->height_ = y;
122 // Destroy the grid
123 void
124 Grid_i::destroy ( )
126 // Delete the array.
127 for (int i = 0; i < height_; i++)
128 this->pool_t_->free (array_[i]);
130 // delete [] array_;
131 this->pool_t_->free (array_);
133 ACE_DEBUG ((LM_DEBUG,
134 " (%P|%t) %s\n",
135 "Grid has been destroyed"));
138 void
139 Grid_i::set_pool (pool_t *pool)
141 this->pool_t_ = pool;
143 // Constructor
145 Grid_Factory_i::Grid_Factory_i ()
146 : orb_ (0),
147 pool_name_ (0),
148 pool_t_ (0)
152 // Destructor
154 Grid_Factory_i::~Grid_Factory_i ()
156 delete this->pool_t_;
159 // Make a <Grid>.
161 Grid_ptr
162 Grid_Factory_i::make_grid (CORBA::Short width,
163 CORBA::Short height)
165 ACE_DEBUG ((LM_DEBUG,
166 " (%P|%t) Making a new Grid\n"));
168 // Set a default value for width.
169 if (width <= 0)
170 width = Grid_Factory::DEFAULT_WIDTH;
172 // Set a default value for height.
173 if (height <= 0)
174 height = Grid_Factory::DEFAULT_HEIGHT;
176 // Get a memory pool
177 ACE_NEW_THROW_EX (pool_t_,
178 pool_t (pool_name_),
179 CORBA::NO_MEMORY ());
181 // pool_t_ = new pool_t (pool_name_);
183 // This attempts to create a new Grid_i and throws an exception and
184 // returns a null value if it fails
185 int prev_no = errno;
186 Grid_i *grid_ptr = new Grid_i (width,
187 height,
188 pool_t_);
189 if (errno == ENOMEM)
190 throw CORBA::NO_MEMORY ();
192 errno = prev_no;
194 grid_ptr->set_pool (pool_t_);
196 // Register the Grid pointer.
197 return grid_ptr->_this ();
200 // Set the ORB pointer.
202 void
203 Grid_Factory_i::orb (CORBA::ORB_ptr o)
205 this->orb_ = CORBA::ORB::_duplicate (o);
208 // Shutdown.
209 void
210 Grid_Factory_i::shutdown ()
212 ACE_DEBUG ((LM_DEBUG,
213 " (%P|%t) %s\n",
214 "Grid Factory is shutting down"));
216 // Instruct the ORB to shutdown.
217 this->orb_->shutdown ();
220 void
221 Grid_Factory_i::cleanup ()
223 const char *name = "Array";
225 if (this->pool_t_->unbind (name) == -1)
226 ACE_DEBUG ((LM_DEBUG,
227 "\n Failed to unbind "));
230 void
231 Grid_Factory_i::pool_name (const ACE_TCHAR *name)
233 this->pool_name_ = ACE_OS::strdup (name);