PointerClass.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.ibm.com/developerworks/oss/CPLv1.0.htm */
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 RexxPointer.cpp */
40 /* */
41 /* Primitive Pointer Class */
42 /* */
43 /******************************************************************************/
44 #include <stdlib.h>
45 #include <string.h>
46 #include "RexxCore.h"
47 #include "PointerClass.hpp"
48 #include "ActivityManager.hpp"
49 
50 
51 RexxClass *RexxPointer::classInstance = OREF_NULL; // singleton class instance
52 RexxPointer *RexxPointer::nullPointer = OREF_NULL; // single version of a null pointer
53 
54 
56 /******************************************************************************/
57 /* Function: Create initial bootstrap objects */
58 /******************************************************************************/
59 {
60  CLASS_CREATE(Pointer, "Pointer", RexxClass);
61  TheNullPointer = new_pointer(NULL); // a NULL pointer object
62 }
63 
64 
65 /**
66  * Primitive-level comparison of pointer values.
67  *
68  * @param other The other comparison value.
69  *
70  * @return True if the two objects are equal, false otherwise.
71  */
73 {
74  requiredArgument(other, OREF_positional, ARG_ONE); /* must have the other argument */
75 
76  if (!isOfClass(Pointer, other))
77  {
78  return TheFalseObject;
79  }
80 
81  return this->pointer() == ((RexxPointer *)other)->pointer() ? TheTrueObject : TheFalseObject;
82 }
83 
84 
85 /**
86  * Primitive-level comparison of pointer values.
87  *
88  * @param other The other comparison value.
89  *
90  * @return True if the two objects are equal, false otherwise.
91  */
93 {
94  requiredArgument(other, OREF_positional, ARG_ONE); /* must have the other argument */
95 
96  if (!isOfClass(Pointer, other))
97  {
98  return TheTrueObject;
99  }
100 
101  return this->pointer() != ((RexxPointer *)other)->pointer() ? TheTrueObject : TheFalseObject;
102 }
103 
104 
105 /**
106  * Override of the default hash value method.
107  */
109 {
110  // generate a hash from the pointer value...but obscure this a touch to get
111  // a better bit distribution
112  return (HashCode)(~((uintptr_t)pointerData));
113 }
114 
115 
116 void *RexxPointer::operator new(size_t size)
117 /******************************************************************************/
118 /* Function: Create a new pointer object */
119 /******************************************************************************/
120 {
121  /* Get new object */
122  RexxObject *newObject = new_object(size, T_Pointer);
123  newObject->setHasNoReferences(); /* this has no references */
124  return (void *)newObject; /* return the new object */
125 }
126 
127 
128 RexxObject *RexxPointer::newRexx(RexxObject **args, size_t argc, size_t named_argc)
129 /******************************************************************************/
130 /* Function: Allocate a pointer object from Rexx code. */
131 /******************************************************************************/
132 {
133  // we do not allow these to be allocated from Rexx code...
135  return TheNilObject;
136 }
137 
138 
139 /**
140  * Format this as a character string value.
141  *
142  * @return The character string value.
143  */
145 {
147 }
148 
149 
150 /**
151  * Test if this is a null pointer value.
152  *
153  * @return True if the pointer value is NULL, false for non-null.
154  */
156 {
157  return pointer() == NULL ? TheTrueObject : TheFalseObject;
158 }
void reportException(wholenumber_t error)
@ T_Pointer
size_t HashCode
Definition: ObjectClass.hpp:77
RexxPointer * new_pointer(void *p)
#define OREF_NULL
Definition: RexxCore.h:60
#define TheTrueObject
Definition: RexxCore.h:186
#define TheNullPointer
Definition: RexxCore.h:187
#define isOfClass(t, r)
Definition: RexxCore.h:212
#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_Unsupported_new_method
RexxObject * new_object(size_t s)
Definition: RexxMemory.hpp:431
#define CLASS_CREATE(name, id, className)
Definition: RexxMemory.hpp:498
static RexxString * pointerToString(void *)
Definition: Numerics.cpp:888
static RexxPointer * nullPointer
void * pointerData
RexxObject * notEqual(RexxObject *other)
static RexxClass * classInstance
void * pointer()
static void createInstance()
virtual RexxString * stringValue()
virtual HashCode getHashValue()
RexxObject * newRexx(RexxObject **args, size_t argc, size_t named_argc)
RexxObject * equal(RexxObject *)
RexxObject * isNull()
UINT_PTR uintptr_t