TableClass.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 TableClass.cpp */
40 /* */
41 /* Primitive Table Class */
42 /* */
43 /****************************************************************************/
44 #include "RexxCore.h"
45 #include "IntegerClass.hpp"
46 #include "TableClass.hpp"
47 #include "RexxActivity.hpp"
48 #include "ActivityManager.hpp"
49 #include "ProtectedObject.hpp"
50 
51 // singleton class instance
53 
54 
55 /**
56  * Create initial class object at bootstrap time.
57  */
59 {
60  CLASS_CREATE(Table, "Table", RexxClass);
61 }
62 
63 
65  size_t _value, /* object to add */
66  RexxObject *_index) /* added index */
67 /******************************************************************************/
68 /* Function: This add method is used by the envelope packing/copybuffer */
69 /* processing. it is used to maintain the nodupTable. The value argument */
70 /* is not really an OREF but rather a offSet into the smartBuffer. */
71 /* This routine will do the same function as Add except that we won't verify */
72 /* any arguments and f we need to grow the hashtab we mark it as */
73 /* having to references. This is needed to so that we don't try and Mark */
74 /* (Collect) the offset values. */
75 /******************************************************************************/
76 {
77  memoryObject.disableOrefChecks(); /* Turn off OrefSet Checking. */
78  /* try to place in existing hashtab */
79  RexxHashTable *newHash = this->contents->primitiveAdd((RexxObject *)_value, _index);
80  if (newHash != OREF_NULL)
81  { /* have a reallocation occur? */
82  /* mark the hash as not having refere*/
83  /* even though the indices are objs */
84  /* we don't need to mark this Hash. */
85  /* Trust me !!! */
86  newHash->setHasNoReferences();
87  /* hook on the new hash table */
88  OrefSet(this, this->contents, newHash);
89  }
90  memoryObject.enableOrefChecks(); /* Turn OrefSet Checking. */
91  return OREF_NULL; /* always return nothing */
92 }
93 
95  RexxObject *_value, /* object to add */
96  RexxString *_index) /* added index */
97 /******************************************************************************/
98 /* Function: Add an object to a table using a string index. */
99 /******************************************************************************/
100 {
101  requiredArgument(_value, OREF_positional, ARG_ONE); /* make sure we have an value */
102  requiredArgument(_index, OREF_positional, ARG_TWO); /* make sure we have an index */
103  /* try to place in existing hashtab */
104  RexxHashTable *newHash = this->contents->stringAdd(_value, _index);
105  if (newHash != OREF_NULL) /* have a reallocation occur? */
106  {
107  /* hook on the new hash table */
108  OrefSet(this, this->contents, newHash);
109  }
110  return OREF_NULL; /* always return nothing */
111 }
112 
114  RexxObject *_value, /* value to insert */
115  RexxString *_index) /* item index */
116 /******************************************************************************/
117 /* Function: Put an object into the table using a string index */
118 /******************************************************************************/
119 {
120  requiredArgument(_value, OREF_positional, ARG_ONE); /* make sure we have an value */
121  requiredArgument(_index, OREF_positional, ARG_TWO); /* make sure we have an index */
122  /* try to place in existing hashtab */
123  RexxHashTable *newHash = this->contents->stringPut(_value, _index);
124  if (newHash != OREF_NULL) /* have a reallocation occur? */
125  {
126  /* hook on the new hash table */
127  OrefSet(this, this->contents, newHash);
128  }
129  return OREF_NULL; /* always return nothing */
130 }
131 
133 /******************************************************************************/
134 /* Function: Primitive level request('ARRAY') fast path */
135 /******************************************************************************/
136 {
137  if (isOfClass(Table, this)) /* primitive level object? */
138  {
139  return this->makeArray(); /* just do the makearray */
140  }
141  else /* need to so full request mechanism */
142  {
143  return(RexxArray *)this->sendMessage(OREF_REQUEST, OREF_ARRAYSYM);
144  }
145 }
146 
148 /******************************************************************************/
149 /* Function: Return the count of items in the table */
150 /******************************************************************************/
151 {
152  size_t numEntries = this->contents->totalEntries();
153  return(RexxObject *)new_integer(numEntries);
154 }
155 
157 /******************************************************************************/
158 /* Function: Reset a table by clearing out the old contents table */
159 /******************************************************************************/
160 {
162 }
163 
165 /******************************************************************************/
166 /* Function: Put an object into a table, ensuring no duplicates. */
167 /******************************************************************************/
168 {
169  /* try to place in existing hashtab */
170  RexxHashTable *newHash = this->contents->putNodupe(_value, _index);
171  if (newHash != OREF_NULL) /* have a reallocation occur? */
172  {
173  /* hook on the new hash table */
174  OrefSet(this, this->contents, newHash);
175  }
176  return OREF_NULL;
177 }
178 
180 /******************************************************************************/
181 /* Function: Create an instance of a table */
182 /******************************************************************************/
183 {
184  OrefSet(this, this->contents, this->contents->reHash());
185 }
186 
188  RexxObject **args, /* subclass init arguments */
189  size_t argCount, /* count of arguments */
190  size_t named_argCount)
191 /******************************************************************************/
192 /* Function: Create an instance of a table */
193 /******************************************************************************/
194 {
195  // this method is defined on the object class, but this is actually attached
196  // to a class object instance. Therefore, any use of the this pointer
197  // will be touching the wrong data. Use the classThis pointer for calling
198  // any methods on this object from this method.
199  RexxClass *classThis = (RexxClass *)this;
200  classThis->checkAbstract(); // ooRexx5
201 
202  RexxTable *newObj = new_table(); /* get a new table */
203  ProtectedObject p(newObj);
204  newObj->setBehaviour(classThis->getInstanceBehaviour());
205  /* does object have an UNINT method */
206  if (classThis->hasUninitDefined())
207  {
208  newObj->hasUninit(); /* Make sure everyone is notified. */
209  }
210  /* call any rexx level init's */
211  newObj->sendMessage(OREF_INIT, args, argCount, named_argCount);
212  return newObj; /* return the new object */
213 }
214 
216 /******************************************************************************/
217 /* Function: Create an instance of a table */
218 /******************************************************************************/
219 {
221 }
222 
223 
@ T_Table
RexxInteger * new_integer(wholenumber_t v)
#define OREF_NULL
Definition: RexxCore.h:61
#define OrefSet(o, r, v)
Definition: RexxCore.h:101
const int ARG_TWO
Definition: RexxCore.h:84
#define isOfClass(t, r)
Definition: RexxCore.h:224
const int ARG_ONE
Definition: RexxCore.h:83
void requiredArgument(RexxObject *object, RexxString *kind, size_t position)
Definition: RexxCore.h:303
RexxTable * new_hashCollection(size_t s, size_t s2, size_t t)
RexxHashTable * new_hashtab(size_t s)
RexxMemory memoryObject
Definition: RexxMemory.cpp:86
#define CLASS_CREATE(name, id, className)
Definition: RexxMemory.hpp:503
RexxTable * new_table()
Definition: TableClass.hpp:76
void checkAbstract()
RexxBehaviour * getInstanceBehaviour()
Definition: ClassClass.hpp:135
bool hasUninitDefined()
Definition: ClassClass.hpp:125
RexxHashTable * contents
RexxHashTable * primitiveAdd(RexxObject *value, RexxObject *key)
RexxHashTable * stringPut(RexxObject *value, RexxString *key)
RexxHashTable * putNodupe(RexxObject *value, RexxObject *key)
RexxHashTable * stringAdd(RexxObject *value, RexxString *key)
RexxHashTable * reHash()
size_t totalEntries()
void setBehaviour(RexxBehaviour *b)
void disableOrefChecks()
Definition: RexxMemory.hpp:267
void enableOrefChecks()
Definition: RexxMemory.hpp:268
void sendMessage(RexxString *, RexxArray *, RexxDirectory *, ProtectedObject &)
void reHash()
Definition: TableClass.cpp:179
RexxObject * stringAdd(RexxObject *, RexxString *)
Definition: TableClass.cpp:94
static RexxTable * newInstance()
Definition: TableClass.cpp:215
RexxObject * stringPut(RexxObject *, RexxString *)
Definition: TableClass.cpp:113
RexxArray * requestArray()
Definition: TableClass.cpp:132
RexxObject * putNodupe(RexxObject *, RexxObject *)
Definition: TableClass.cpp:164
RexxObject * newRexx(RexxObject **, size_t, size_t)
Definition: TableClass.cpp:187
static RexxClass * classInstance
Definition: TableClass.hpp:72
RexxObject * addOffset(size_t, RexxObject *)
Definition: TableClass.cpp:64
void reset()
Definition: TableClass.cpp:156
static void createInstance()
Definition: TableClass.cpp:58
RexxObject * itemsRexx()
Definition: TableClass.cpp:147