SupplierClass.cpp
Go to the documentation of this file.
1 /*----------------------------------------------------------------------------*/
2 /* */
3 /* Copyright (c) 1995, 2004 IBM Corporation. All rights reserved. */
4 /* Copyright (c) 2005-2009 Rexx Language Association. All rights reserved. */
5 /* */
6 /* This program and the accompanying materials are made available under */
7 /* the terms of the Common Public License v1.0 which accompanies this */
8 /* distribution. A copy is also available at the following address: */
9 /* http://www.oorexx.org/license.html */
10 /* */
11 /* Redistribution and use in source and binary forms, with or */
12 /* without modification, are permitted provided that the following */
13 /* conditions are met: */
14 /* */
15 /* Redistributions of source code must retain the above copyright */
16 /* notice, this list of conditions and the following disclaimer. */
17 /* Redistributions in binary form must reproduce the above copyright */
18 /* notice, this list of conditions and the following disclaimer in */
19 /* the documentation and/or other materials provided with the distribution. */
20 /* */
21 /* Neither the name of Rexx Language Association nor the names */
22 /* of its contributors may be used to endorse or promote products */
23 /* derived from this software without specific prior written permission. */
24 /* */
25 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
26 /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
27 /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS */
28 /* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
29 /* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
30 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
31 /* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */
32 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY */
33 /* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
34 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
35 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
36 /* */
37 /*----------------------------------------------------------------------------*/
38 /******************************************************************************/
39 /* REXX Kernel */
40 /* */
41 /* Primitive Supplier Class */
42 /* */
43 /******************************************************************************/
44 
45 #include "RexxCore.h"
46 #include "IntegerClass.hpp"
47 #include "ArrayClass.hpp"
48 #include "SupplierClass.hpp"
49 #include "ActivityManager.hpp"
50 #include "ProtectedObject.hpp"
51 
52 // singleton class instance
54 
55 
56 /**
57  * Create initial class object at bootstrap time.
58  */
60 {
61  CLASS_CREATE(Supplier, "Supplier", RexxClass);
62 }
63 
64 
66  RexxArray *_values, /* array of values */
67  RexxArray *_indexes ) /* array of indexes */
68 /****************************************************************************/
69 /* Function: Initialize a supplier */
70 /****************************************************************************/
71 {
72  OrefSet(this, this->values, _values); /* store the values array */
73  /* and the index array also */
74  OrefSet(this, this->indexes, _indexes);
75  this->position = 1; /* set the first position */
76 }
77 
78 
80 /****************************************************************************/
81 /* Function: Initialize a supplier */
82 /****************************************************************************/
83 {
84  values = OREF_NULL;
86 }
87 
88 
89 void RexxSupplier::live(size_t liveMark)
90 /******************************************************************************/
91 /* Function: Normal garbage collection live marking */
92 /******************************************************************************/
93 {
94  memory_mark(this->values);
95  memory_mark(this->indexes);
96  memory_mark(this->objectVariables);
97 }
98 
99 void RexxSupplier::liveGeneral(int reason)
100 /******************************************************************************/
101 /* Function: Generalized object marking */
102 /******************************************************************************/
103 {
106  memory_mark_general(this->objectVariables);
107 }
108 
110 /******************************************************************************/
111 /* Function: Flatten an object */
112 /******************************************************************************/
113 {
115 
116  flatten_reference(newThis->values, envelope);
117  flatten_reference(newThis->indexes, envelope);
118 
120 }
121 
123 /****************************************************************************/
124 /* Function: Return indication of object availability */
125 /****************************************************************************/
126 {
127  /* check the array size */
128  return (this->position > this->values->size()) ? TheFalseObject : TheTrueObject;
129 }
130 
132 /****************************************************************************/
133 /* Function: Step to the next element of the supplier */
134 /****************************************************************************/
135 {
136  /* already gone past the end? */
137  if (this->position > this->values->size())
138  {
139  /* oops, give an error */
141  }
142  this->position++; /* step the position pointer */
143  return OREF_NULL; /* this returns nothing */
144 }
145 
147 /****************************************************************************/
148 /* Function: Retrieve the value of a collection item */
149 /****************************************************************************/
150 {
151  /* already gone past the end? */
152  if (this->position > this->values->size())
153  {
154  /* oops, give an error */
156  }
157  /* get the value */
158  RexxObject *_value = this->values->get(this->position);
159  if (_value == OREF_NULL) /* returned nothing? */
160  {
161  _value = TheNilObject; /* change this to .nil */
162  }
163  return _value; /* return this value */
164 }
165 
167 /****************************************************************************/
168 /* Function: Retrieve the index of a collection item */
169 /****************************************************************************/
170 {
171  RexxObject *_value; /* supplier value */
172 
173  /* already gone past the end? */
174  if (this->position > this->values->size())
175  {
176  /* oops, give an error */
178  }
179  if (this->indexes == OREF_NULL) /* no index array given? */
180  {
181  /* just return current position */
182  return(RexxObject *)new_integer(this->position);
183  }
184  /* already gone past the end? */
185  if (this->position > this->indexes->size())
186  {
187  _value = TheNilObject; /* no value to return */
188  }
189  else
190  {
191  /* get the value */
192  _value = this->indexes->get(this->position);
193  if (_value == OREF_NULL) /* returned nothing? */
194  {
195  _value = TheNilObject; /* change this to .nil */
196  }
197  }
198  return _value; /* and return the value */
199 }
200 
201 void *RexxSupplier::operator new(size_t size)
202 /****************************************************************************/
203 /* Function: Create a new supplier object */
204 /****************************************************************************/
205 {
206  /* Get new object */
207  return new_object(size, T_Supplier);
208 }
209 
210 
211 /**
212  * Supplier initializer for suppliers created via
213  * .supplier~new(values, indexes).
214  *
215  * @param values The values array object
216  * @param indexes The indexes array object
217  *
218  * @return Nothing
219  */
221 {
222  requiredArgument(_values, OREF_positional, ARG_ONE); // both values are required
223  requiredArgument(_indexes, OREF_positional, ARG_TWO);
224 
225  // now verify both values
226  RexxArray *new_values = REQUEST_ARRAY(_values);
227  RexxArray *new_indexes = REQUEST_ARRAY(_indexes);
228  if (new_values == (RexxArray *)TheNilObject || new_values->getDimension() != 1)
229  {
231  }
232  if (new_indexes == (RexxArray *)TheNilObject || new_indexes->getDimension() != 1)
233  {
235  }
236 
237  OrefSet(this, this->values, new_values);
238  OrefSet(this, this->indexes, new_indexes);
239  this->position = 1;
240  return OREF_NULL;
241 }
242 
243 
244 
246  RexxObject **init_args, /* subclass init arguments */
247  size_t argCount, /* count of arguments */
248  size_t named_argCount)
249 /****************************************************************************/
250 /* Function: Public REXX supplier new method */
251 /****************************************************************************/
252 {
253  RexxObject *newObj = new RexxSupplier();
254  ProtectedObject p(newObj);
255  newObj->setBehaviour(this->getInstanceBehaviour());
256  if (this->hasUninitDefined())
257  {
258  newObj->hasUninit();
259  }
260  /* Initialize the new instance */
261  newObj->sendMessage(OREF_INIT, init_args, argCount, named_argCount);
262  return newObj; /* return the new supplier */
263 }
void reportException(wholenumber_t error)
@ T_Supplier
RexxInteger * new_integer(wholenumber_t v)
#define OREF_NULL
Definition: RexxCore.h:60
RexxArray * REQUEST_ARRAY(RexxObject *obj)
Definition: RexxCore.h:469
#define OrefSet(o, r, v)
Definition: RexxCore.h:94
#define TheTrueObject
Definition: RexxCore.h:186
const int ARG_TWO
Definition: RexxCore.h:81
#define TheNilObject
Definition: RexxCore.h:181
#define TheFalseObject
Definition: RexxCore.h:185
const int ARG_ONE
Definition: RexxCore.h:80
void requiredArgument(RexxObject *object, RexxString *kind, size_t position)
Definition: RexxCore.h:291
#define Error_Incorrect_method_supplier
#define Error_Incorrect_method_noarray
#define memory_mark(oref)
Definition: RexxMemory.hpp:445
RexxObject * new_object(size_t s)
Definition: RexxMemory.hpp:431
#define flatten_reference(oref, envel)
Definition: RexxMemory.hpp:493
#define CLASS_CREATE(name, id, className)
Definition: RexxMemory.hpp:498
#define memory_mark_general(oref)
Definition: RexxMemory.hpp:446
#define cleanUpFlatten
Definition: RexxMemory.hpp:479
#define setUpFlatten(type)
Definition: RexxMemory.hpp:473
size_t getDimension()
Definition: ArrayClass.cpp:693
size_t size()
Definition: ArrayClass.hpp:202
RexxObject * get(size_t pos)
Definition: ArrayClass.hpp:203
RexxBehaviour * getInstanceBehaviour()
Definition: ClassClass.hpp:127
bool hasUninitDefined()
Definition: ClassClass.hpp:117
void setBehaviour(RexxBehaviour *b)
void sendMessage(RexxString *, RexxArray *, RexxDirectory *, ProtectedObject &)
RexxObject * newRexx(RexxObject **, size_t, size_t)
static RexxClass * classInstance
RexxObject * next()
void liveGeneral(int reason)
RexxInteger * available()
void flatten(RexxEnvelope *)
RexxArray * indexes
RexxObject * value()
RexxArray * values
RexxObject * index()
RexxObject * initRexx(RexxArray *values, RexxArray *indexes)
void live(size_t)
static void createInstance()