Revert "Use a variable on the stack to not have a temporary in the call"
[ACE_TAO.git] / TAO / examples / Simple / grid / Grid_i.cpp
blob30b90d65d1d6a93b36c5b7b528d02030ca3bf132
1 // -*- C++ -*-
2 #include "Grid_i.h"
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.
6 CORBA::UShort
7 Grid_i::ushort_min (CORBA::UShort a, CORBA::UShort b)
9 return a > b ? b : a;
12 // Default constructor.
13 Grid_i::Grid_i ()
14 : width_ (0),
15 height_ (0),
16 array_ (0)
18 //no-op
21 // Constructor.
22 Grid_i::Grid_i (CORBA::Short x,
23 CORBA::Short y)
24 : width_ (x),
25 height_ (y),
26 array_ (0)
30 // Default destructor.
31 Grid_i::~Grid_i ()
35 // Set a value in the grid.
36 void
37 Grid_i::set (CORBA::Short x,
38 CORBA::Short y,
39 CORBA::Long value)
41 if (x < 0
42 || y < 0
43 || x >= this->width_
44 || y >= this->height_)
45 throw Grid::RANGE_ERROR ();
46 else
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.
55 CORBA::Long
56 Grid_i::get (CORBA::Short x,
57 CORBA::Short y)
59 if (x < 0
60 || y < 0
61 || x >= this->width_
62 || y >= this->height_)
63 throw Grid::RANGE_ERROR ();
64 else
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);
72 // Access methods.
73 CORBA::Short
74 Grid_i::width ()
76 return this->width_;
79 CORBA::Short
80 Grid_i::height ()
82 return this->height_;
85 void
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);
97 array.release ();
98 this->width_ = x;
102 void
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);
114 array.release ();
115 this->height_ = y;
119 // Destroy the grid
120 void
121 Grid_i::destroy ()
123 // Delete the array.
124 std::unique_ptr<CORBA::Long[]> tmp (this->array_.release ());
125 this->width_ = 0;
126 this->height_ = 0;
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
133 CORBA::Long *
134 Grid_i::allocate_array (CORBA::Short x, CORBA::Short y)
136 CORBA::Long *array = 0;
137 ACE_NEW_THROW_EX (array,
138 CORBA::Long[x * y],
139 CORBA::NO_MEMORY ());
141 return array;
144 // Set the ORB pointer.
145 void
146 Grid_Factory_i::orb (CORBA::ORB_ptr o)
148 this->orb_ = CORBA::ORB::_duplicate (o);
151 // Shutdown.
152 void
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 ();
162 // Constructor
163 Grid_Factory_i::Grid_Factory_i ()
167 // Destructor
168 Grid_Factory_i::~Grid_Factory_i ()
172 // Make a <Grid>.
173 Grid_ptr
174 Grid_Factory_i::make_grid (CORBA::Short width,
175 CORBA::Short height)
177 ACE_DEBUG ((LM_DEBUG,
178 ACE_TEXT ("(%P|%t) Making a new Grid\n")));
180 // Set a default value for width.
181 if (width <= 0)
182 width = Grid_Factory::DEFAULT_WIDTH;
184 // Set a default value for height.
185 if (height <= 0)
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
190 Grid_i *grid = 0;
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 ());