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 {
75  ProtectedObject p(newObj);
76  newObj->setBehaviour(((RexxClass *)this)->getInstanceBehaviour());
77  /* does object have an UNINT method */
78  if (((RexxClass *)this)->hasUninitDefined())
79  {
80  newObj->hasUninit(); /* Make sure everyone is notified. */
81  }
82  /* call any rexx level init's */
83  newObj->sendMessage(OREF_INIT, args, argCount, named_argCount);
84  return newObj; /* return the new object */
85 }
86 
87 
88 /**
89  * Remove an object from an IdentityTable
90  *
91  * @param key The key of the object to remove
92  *
93  * @return The removed object (if any).
94  */
96 {
97  return this->contents->primitiveRemove(key);
98 }
99 
100 
101 /**
102  * Retrieve an object from the table using identity
103  * semantics. This is an override for the base
104  * collection get method.
105  *
106  * @param key The target index.
107  *
108  * @return The retrieved object. Returns OREF_NULL if the key is
109  * not found.
110  */
112 {
113  return this->contents->primitiveGet(key);
114 }
115 
116 
117 /**
118  * Create a new instance of an identity table.
119  *
120  * @param size The initial table capacity.
121  *
122  * @return The newly created table.
123  */
125 {
127 }
128 
129 
130 /**
131  * Virtual override for the default hash collection put()
132  * operation.
133  *
134  * @param _value The value to insert.
135  * @param _index The target index.
136  *
137  * @return Always returns OREF_NULL
138  */
140 {
141  /* try to place in existing hashtab */
142  RexxHashTable *newHash = this->contents->primitivePut(_value, _index);
143  if (newHash != OREF_NULL) /* have a reallocation occur? */
144  {
145  /* hook on the new hash table */
146  OrefSet(this, this->contents, newHash);
147  }
148  return OREF_NULL; /* always return nothing */
149 }
150 
151 
152 /**
153  * Override for the default hash collection add()
154  * method.
155  *
156  * @param _value The value to insert.
157  * @param _index The index this will be stored under.
158  *
159  * @return Always returns OREF_NULL.
160  */
162 {
163  /* try to place in existing hashtab */
164  RexxHashTable *newHash = this->contents->primitiveAdd(_value, _index);
165  if (newHash != OREF_NULL) /* have a reallocation occur? */
166  {
167  /* hook on the new hash table */
168  OrefSet(this, this->contents, newHash);
169  }
170  return OREF_NULL; /* always return nothing */
171 }
172 
173 
174 /**
175  * Remove an item specified by value.
176  *
177  * @param target The target object.
178  *
179  * @return The target object again.
180  */
182 {
183  // the contents handle all of this.
184  return this->contents->primitiveRemoveItem(target);
185 }
186 
187 
188 /**
189  * Test if a given item exists in the collection.
190  *
191  * @param target The target object.
192  *
193  * @return .true if the object exists, .false otherwise.
194  */
196 {
197  return this->contents->primitiveHasItem(target);
198 }
199 
200 
201 /**
202  * Retrieve an index for a given item. Which index is returned
203  * is indeterminate.
204  *
205  * @param target The target object.
206  *
207  * @return The index for the target object, or .nil if no object was
208  * found.
209  */
211 {
212  // retrieve this from the hash table
213  return contents->primitiveGetIndex(target);
214 }
@ T_IdentityTable
RexxIdentityTable * new_identity_table()
#define OREF_NULL
Definition: RexxCore.h:60
#define OrefSet(o, r, v)
Definition: RexxCore.h:94
RexxTable * new_hashCollection(size_t s, size_t s2, size_t t)
#define CLASS_CREATE(name, id, className)
Definition: RexxMemory.hpp:498
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:208