blob: dc8e0feff6509381ee55e8378dc2b2e42a2dd378 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
// **********************************************************************
//
// Copyright (c) 2003-2017 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
#import <LocalObjectI.h>
#include <map>
#include <IceUtil/Shared.h>
#include <Foundation/Foundation.h>
#define CXXOBJECT ((IceUtil::Shared*)cxxObject_)
namespace
{
std::map<IceUtil::Shared*, ICELocalObject*> cachedObjects;
}
@implementation ICELocalObject
-(id) init
{
self = [super init];
if(!self)
{
return nil;
}
cxxObject_ = 0;
return self;
}
-(id) initWithCxxObject:(IceUtil::Shared*)arg
{
self = [super init];
if(!self)
{
return nil;
}
cxxObject_ = arg;
CXXOBJECT->__incRef();
//
// No synchronization because initWithCxxObject is always called with the wrapper class object locked
//
assert(cachedObjects.find(CXXOBJECT) == cachedObjects.end());
cachedObjects.insert(std::make_pair(CXXOBJECT, self));
return self;
}
-(IceUtil::Shared*) cxxObject
{
return CXXOBJECT;
}
-(void) dealloc
{
if(cxxObject_)
{
//
// No synchronization because dealloc is always called with the wrapper class object locked
//
cachedObjects.erase(CXXOBJECT);
CXXOBJECT->__decRef();
cxxObject_ = 0;
}
[super dealloc];
}
+(id) getLocalObjectWithCxxObjectNoAutoRelease:(IceUtil::Shared*)arg
{
//
// Note: the returned object is NOT retained. It must be held
// some other way by the calling thread.
//
if(arg == 0)
{
return nil;
}
@synchronized([ICELocalObject class])
{
std::map<IceUtil::Shared*, ICELocalObject*>::const_iterator p = cachedObjects.find(arg);
if(p != cachedObjects.end())
{
return p->second;
}
}
return nil;
}
+(id) localObjectWithCxxObjectNoAutoRelease:(IceUtil::Shared*)arg
{
if(arg == 0)
{
return nil;
}
@synchronized([ICELocalObject class])
{
std::map<IceUtil::Shared*, ICELocalObject*>::const_iterator p = cachedObjects.find(arg);
if(p != cachedObjects.end())
{
return [p->second retain];
}
else
{
return [[self alloc] initWithCxxObject:arg];
}
}
return nil; // Keep the compiler happy.
}
+(id) localObjectWithCxxObject:(IceUtil::Shared*)arg
{
return [[self localObjectWithCxxObjectNoAutoRelease:arg] autorelease];
}
-(id) retain
{
NSIncrementExtraRefCount(self);
return self;
}
-(oneway void) release
{
@synchronized([ICELocalObject class])
{
if(NSDecrementExtraRefCountWasZero(self))
{
[self dealloc];
}
}
}
@end
|