IdentityTableClass.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 IdentityTableClass.cpp */
40 /* */
41 /* Primitive IdentityTable 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(IdentityTable, "IdentityTable", RexxClass);
61 }
62 
63 
64 /**
65  * Create a new identity table instance.
66  *
67  * @param args The new arguments.
68  * @param argCount The count of new arguments.
69  *
70  * @return The constructed instance.
71  */
72 RexxObject *RexxIdentityTable::newRexx(RexxObject **args, size_t argCount, size_t named_argCount)
73 {
74  // this method is defined on the object class, but this is actually attached
75  // to a class object instance. Therefore, any use of the this pointer
76  // will be touching the wrong data. Use the classThis pointer for calling
77  // any methods on this object from this method.
78  RexxClass *classThis = (RexxClass *)this;
79  classThis->checkAbstract(); // ooRexx5
80 
82  ProtectedObject p(newObj);
83  newObj->setBehaviour(classThis->getInstanceBehaviour());
84  /* does object have an UNINT method */
85  if (classThis->hasUninitDefined())
86  {
87  newObj->hasUninit(); /* Make sure everyone is notified. */
88  }
89  /* call any rexx level init's */
90  newObj->sendMessage(OREF_INIT, args, argCount, named_argCount);
91  return newObj; /* return the new object */
92 }
93 
94 
95 /**
96  * Remove an object from an IdentityTable
97  *
98  * @param key The key of the object to remove
99  *
100  * @return The removed object (if any).
101  */
103 {
104  return this->contents->primitiveRemove(key);
105 }
106 
107 
108 /**
109  * Retrieve an object from the table using identity
110  * semantics. This is an override for the base
111  * collection get method.
112  *
113  * @param key The target index.
114  *
115  * @return The retrieved object. Returns OREF_NULL if the key is
116  * not found.
117  */
119 {
120  return this->contents->primitiveGet(key);
121 }
122 
123 
124 /**
125  * Create a new instance of an identity table.
126  *
127  * @param size The initial table capacity.
128  *
129  * @return The newly created table.
130  */
132 {
134 }
135 
136 
137 /**
138  * Virtual override for the default hash collection put()
139  * operation.
140  *
141  * @param _value The value to insert.
142  * @param _index The target index.
143  *
144  * @return Always returns OREF_NULL
145  */
147 {
148  /* try to place in existing hashtab */
149  RexxHashTable *newHash = this->contents->primitivePut(_value, _index);
150  if (newHash != OREF_NULL) /* have a reallocation occur? */
151  {
152  /* hook on the new hash table */
153  OrefSet(this, this->contents, newHash);
154  }
155  return OREF_NULL; /* always return nothing */
156 }
157 
158 
159 /**
160  * Override for the default hash collection add()
161  * method.
162  *
163  * @param _value The value to insert.
164  * @param _index The index this will be stored under.
165  *
166  * @return Always returns OREF_NULL.
167  */
169 {
170  /* try to place in existing hashtab */
171  RexxHashTable *newHash = this->contents->primitiveAdd(_value, _index);
172  if (newHash != OREF_NULL) /* have a reallocation occur? */
173  {
174  /* hook on the new hash table */
175  OrefSet(this, this->contents, newHash);
176  }
177  return OREF_NULL; /* always return nothing */
178 }
179 
180 
181 /**
182  * Remove an item specified by value.
183  *
184  * @param target The target object.
185  *
186  * @return The target object again.
187  */
189 {
190  // the contents handle all of this.
191  return this->contents->primitiveRemoveItem(target);
192 }
193 
194 
195 /**
196  * Test if a given item exists in the collection.
197  *
198  * @param target The target object.
199  *
200  * @return .true if the object exists, .false otherwise.
201  */
203 {
204  return this->contents->primitiveHasItem(target);
205 }
206 
207 
208 /**
209  * Retrieve an index for a given item. Which index is returned
210  * is indeterminate.
211  *
212  * @param target The target object.
213  *
214  * @return The index for the target object, or .nil if no object was
215  * found.
216  */
218 {
219  // retrieve this from the hash table
220  return contents->primitiveGetIndex(target);
221 }
@ T_IdentityTable
RexxIdentityTable * new_identity_table()
#define OREF_NULL
Definition: RexxCore.h:61
#define OrefSet(o, r, v)
Definition: RexxCore.h:101
RexxTable * new_hashCollection(size_t s, size_t s2, size_t t)
#define CLASS_CREATE(name, id, className)
Definition: RexxMemory.hpp:503
void checkAbstract()
RexxBehaviour * getInstanceBehaviour()
Definition: ClassClass.hpp:135
bool hasUninitDefined()
Definition: ClassClass.hpp:125
RexxHashTable * contents
RexxObject * primitiveGet(RexxObject *key)
RexxObject * primitiveRemoveItem(RexxObject *value, RexxObject *key)
RexxObject * primitiveRemove(RexxObject *key)
RexxHashTable * primitiveAdd(RexxObject *value, RexxObject *key)
RexxObject * primitiveHasItem(RexxObject *, RexxObject *)
RexxHashTable * primitivePut(RexxObject *value, RexxObject *key)
RexxObject * primitiveGetIndex(RexxObject *value)
virtual RexxObject * removeItem(RexxObject *value)
RexxObject * newRexx(RexxObject **, size_t, size_t)
static RexxClass * classInstance
virtual RexxObject * put(RexxObject *, RexxObject *)
virtual RexxObject * hasItem(RexxObject *targetIndex)
virtual RexxObject * get(RexxObject *key)
static void createInstance()
virtual RexxObject * getIndex(RexxObject *value)
virtual RexxObject * remove(RexxObject *key)
virtual RexxObject * add(RexxObject *, RexxObject *)
void setBehaviour(RexxBehaviour *b)
void sendMessage(RexxString *, RexxArray *, RexxDirectory *, ProtectedObject &)
static RexxTable * newInstance()
Definition: TableClass.cpp:215