1//
2//  ANTLRMapElement.m
3//  ANTLR
4//
5//  Created by Alan Condit on 6/8/10.
6// [The "BSD licence"]
7// Copyright (c) 2010 Alan Condit
8// All rights reserved.
9//
10// Redistribution and use in source and binary forms, with or without
11// modification, are permitted provided that the following conditions
12// are met:
13// 1. Redistributions of source code must retain the above copyright
14//    notice, this list of conditions and the following disclaimer.
15// 2. Redistributions in binary form must reproduce the above copyright
16//    notice, this list of conditions and the following disclaimer in the
17//    documentation and/or other materials provided with the distribution.
18// 3. The name of the author may not be used to endorse or promote products
19//    derived from this software without specific prior written permission.
20//
21// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32#import <Cocoa/Cocoa.h>
33#import "ANTLRMapElement.h"
34
35
36@implementation ANTLRMapElement
37
38@synthesize name;
39@synthesize node;
40
41+ (id) newANTLRMapElement
42{
43    return [[ANTLRMapElement alloc] init];
44}
45
46+ (id) newANTLRMapElementWithName:(NSString *)aName Type:(NSInteger)aTType
47{
48    return [[ANTLRMapElement alloc] initWithName:aName Type:aTType];
49}
50
51+ (id) newANTLRMapElementWithNode:(NSInteger)aTType Node:(id)aNode
52{
53    return [[ANTLRMapElement alloc] initWithNode:aTType Node:aNode];
54}
55
56+ (id) newANTLRMapElementWithName:(NSString *)aName Node:(id)aNode
57{
58    return [[ANTLRMapElement alloc] initWithName:aName Node:aNode];
59}
60
61+ (id) newANTLRMapElementWithObj1:(id)anObj1 Obj2:(id)anObj2
62{
63    return [[ANTLRMapElement alloc] initWithObj1:anObj1 Obj2:anObj2];
64}
65
66- (id) init
67{
68    self = [super init];
69    if ( self != nil ) {
70        index = nil;
71        name  = nil;
72    }
73    return self;
74}
75
76- (id) initWithName:(NSString *)aName Type:(NSInteger)aTType
77{
78    self = [super init];
79    if ( self != nil ) {
80        index = [[NSNumber numberWithInteger: aTType] retain];
81        name  = [[NSString stringWithString:aName] retain];
82    }
83    return self;
84}
85
86- (id) initWithNode:(NSInteger)aTType Node:(id)aNode
87{
88    self = [super initWithAnIndex:[NSNumber numberWithInteger:aTType]];
89    if ( self != nil ) {
90        node  = aNode;
91        if ( node ) [node retain];
92    }
93    return self;
94}
95
96- (id) initWithName:(NSString *)aName Node:(id)aNode
97{
98    self = [super init];
99    if ( self != nil ) {
100        name  = [[NSString stringWithString:aName] retain];
101        node = aNode;
102        if ( node ) [node retain];
103    }
104    return self;
105}
106
107- (id) initWithObj1:(id)anIndex Obj2:(id)aNode
108{
109    self = [super initWithAnIndex:anIndex];
110    if ( self != nil ) {
111        node = aNode;
112        if ( node ) [node retain];
113    }
114    return self;
115}
116
117- (void) dealloc
118{
119#ifdef DEBUG_DEALLOC
120    NSLog( @"called dealloc in ANTLRMapElement" );
121#endif
122    if ( name ) [name release];
123    if ( node ) [node release];
124    [super dealloc];
125}
126
127- (id) copyWithZone:(NSZone *)aZone
128{
129    ANTLRMapElement *copy;
130
131    copy = [super copyWithZone:aZone];
132    if (name) copy.name = name;
133    if (node) copy.node = node;
134    return( copy );
135}
136
137- (NSInteger) count
138{
139    NSInteger aCnt = 0;
140    if (name != nil) aCnt++;;
141    if (node != nil) aCnt++;;
142    return aCnt;
143}
144
145- (NSInteger)size
146{
147    NSInteger aSize = 0;
148    if ( name ) aSize += sizeof(id);
149    if ( node ) aSize += sizeof(id);
150    return aSize;
151}
152
153
154- (NSString *)getName
155{
156    return name;
157}
158
159- (void)setName:(NSString *)aName
160{
161    if ( aName != name ) {
162        if ( name ) [name release];
163        [aName retain];
164    }
165    name = aName;
166}
167
168- (id)getNode
169{
170    return node;
171}
172
173- (void)setNode:(id)aNode
174{   if ( aNode != node ) {
175        if ( node ) [node release];
176        [aNode retain];
177    }
178    node = aNode;
179}
180
181- (void)putNode:(id)aNode
182{
183    index = ((ANTLRMapElement *)aNode).index;
184    if (((ANTLRMapElement *)aNode).name) {
185        name = [((ANTLRMapElement *)aNode).name retain];
186        node = nil;
187    }
188    if (((ANTLRMapElement *)aNode).node) {
189        name = nil;
190        node = [((ANTLRMapElement *)aNode).node retain];
191    }
192}
193
194- (void)putNode:(id)aNode With:(NSInteger)uniqueID
195{
196    index = ((ANTLRMapElement *)aNode).index;
197    if (((ANTLRMapElement *)aNode).name) {
198        name = [((ANTLRMapElement *)aNode).name retain];
199        node = nil;
200    }
201    if (((ANTLRMapElement *)aNode).node) {
202        name = nil;
203        node = [((ANTLRMapElement *)aNode).node retain];
204    }
205}
206
207@end
208