4 // Some Windows compilers don't have min in std namespaces
5 // moreover on Windows 'min' is a macro, so we have to avoid using it literally.
7 Grid_i::ushort_min (CORBA::UShort a
, CORBA::UShort b
)
12 // Default constructor.
22 Grid_i::Grid_i (CORBA::Short x
,
30 // Default destructor.
35 // Set a value in the grid.
37 Grid_i::set (CORBA::Short x
,
44 || y
>= this->height_
)
45 throw Grid::RANGE_ERROR ();
48 if (this->array_
.get () == 0)
49 this->array_
.reset (allocate_array (this->width_
, this->height_
));
50 *(this->array_
.get () + this->width_
* y
+ x
) = value
;
54 // Get a value from the grid.
56 Grid_i::get (CORBA::Short x
,
62 || y
>= this->height_
)
63 throw Grid::RANGE_ERROR ();
66 if (this->array_
.get () == 0)
67 this->array_
.reset (allocate_array (this->width_
, this->height_
));
68 return *(this->array_
.get () + this->width_
* y
+ x
);
86 Grid_i::width (CORBA::Short x
)
88 if (x
> 0 && x
!= this->width_
)
90 GridArray
array (allocate_array (x
, this->height_
));
91 for (CORBA::Short ctr
= 0; ctr
< this->height_
; ++ctr
)
93 ACE_OS::memcpy (array
.get () + x
* ctr
, this->array_
.get () + this->width_
* ctr
,
94 Grid_i::ushort_min (this->width_
, x
) * sizeof (CORBA::Long
));
96 this->array_
= std::move(array
);
103 Grid_i::height (CORBA::Short y
)
105 if (y
> 0 && y
!= this->height_
)
107 GridArray
array (allocate_array (this->width_
, y
));
108 for (CORBA::Short ctr
= 0; ctr
< Grid_i::ushort_min (this->height_
, y
); ++ctr
)
110 ACE_OS::memcpy (array
.get () + this->width_
* ctr
, this->array_
.get () + this->width_
* ctr
,
111 this->width_
* sizeof (CORBA::Long
));
113 this->array_
= std::move(array
);
124 std::unique_ptr
<CORBA::Long
[]> tmp (this->array_
.release ());
128 ACE_DEBUG ((LM_DEBUG
,
129 ACE_TEXT ("(%P|%t) Grid has been destroyed\n")));
132 // Allocates a new array; even if NO_MEMORY is thrown there should be no memory leak
134 Grid_i::allocate_array (CORBA::Short x
, CORBA::Short y
)
136 CORBA::Long
*array
= 0;
137 ACE_NEW_THROW_EX (array
,
139 CORBA::NO_MEMORY ());
144 // Set the ORB pointer.
146 Grid_Factory_i::orb (CORBA::ORB_ptr o
)
148 this->orb_
= CORBA::ORB::_duplicate (o
);
153 Grid_Factory_i::shutdown ()
155 ACE_DEBUG ((LM_DEBUG
,
156 ACE_TEXT ("(%P|%t) Grid Factory is shutting down\n")));
158 // Instruct the ORB to shutdown.
159 this->orb_
->shutdown ();
163 Grid_Factory_i::Grid_Factory_i ()
168 Grid_Factory_i::~Grid_Factory_i ()
174 Grid_Factory_i::make_grid (CORBA::Short width
,
177 ACE_DEBUG ((LM_DEBUG
,
178 ACE_TEXT ("(%P|%t) Making a new Grid\n")));
180 // Set a default value for width.
182 width
= Grid_Factory::DEFAULT_WIDTH
;
184 // Set a default value for height.
186 height
= Grid_Factory::DEFAULT_HEIGHT
;
188 // This attempts to create a new Grid_i and throws an exception and
189 // returns a null value if it fails
191 ACE_NEW_THROW_EX (grid
,
192 Grid_i (width
, height
),
193 CORBA::NO_MEMORY ());
194 this->grids_holder_
.push_back (PortableServer::ServantBase_var (grid
));
196 // Register the Grid pointer.
197 CORBA::Object_var poa_object
=
198 this->orb_
->resolve_initial_references("RootPOA");
200 PortableServer::POA_var root_poa
=
201 PortableServer::POA::_narrow (poa_object
.in ());
203 PortableServer::ObjectId_var id
=
204 root_poa
->activate_object (grid
);
206 CORBA::Object_var object
= root_poa
->id_to_reference (id
.in ());
208 return Grid::_narrow (object
.in ());