File: DKInterface.m

package info (click to toggle)
dbuskit 0.1.1-14
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,956 kB
  • sloc: objc: 10,543; sh: 9,463; ansic: 200; makefile: 32
file content (435 lines) | stat: -rw-r--r-- 12,006 bytes parent folder | download | duplicates (4)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/** Implementation of the DKInterface class encapsulating D-Bus interface information.
   Copyright (C) 2010 Free Software Foundation, Inc.

   Written by:  Niels Grewe <niels.grewe@halbordnung.de>
   Created: June 2010

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free
   Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02111 USA.
   */

#import <Foundation/NSDictionary.h>
#import <Foundation/NSMapTable.h>
#import <Foundation/NSObjCRuntime.h>
#import <Foundation/NSString.h>
#import <Foundation/NSXMLParser.h>

#import <GNUstepBase/NSDebug+GNUstepBase.h>

#define INCLUDE_RUNTIME_H
#include "config.h"
#undef INCLUDE_RUNTIME_H

#import "DBusKit/DKNotificationCenter.h"

#import "DKMethod.h"
#import "DKProperty.h"
#import "DKPropertyMethod.h"
#import "DKSignal.h"
#import "DKInterface.h"
#import "DKEndpoint.h"
#import "DKProxy+Private.h"

@implementation DKInterface
/**
 * Initializes the interface. Since interfaces need to be named, returns
 * <code>nil</code> when <var>aName</var> is <code>nil</code> or an empty
 * string.
 */
- (id) initWithName: (NSString*)aName
             parent: (id)aParent
{
  if (nil == (self = [super initWithName: aName
                                  parent: aParent]))
  {
    return nil;
  }

  if (0 == [aName length])
  {
    [self release];
    return nil;
  }

  methods = [NSMutableDictionary new];
  properties = [NSMutableDictionary new];
  signals = [NSMutableDictionary new];
  selectorToMethodMap = NSCreateMapTable(NSIntMapKeyCallBacks,
    NSObjectMapValueCallBacks,
    10);
  return self;
}

- (NSDictionary*)methods
{
  return [[methods copy] autorelease];
}

- (NSDictionary*)signals
{
  return [[signals copy] autorelease];
}

- (NSDictionary*)properties
{
  return [[properties copy] autorelease];
}

- (void) _addMember: (DKIntrospectionNode*)node
             toDict: (NSMutableDictionary*)dict
{
  NSString *nodeName = [node name];
  if (0 != [nodeName length])
  {
    if (nil != [dict objectForKey: name])
    {
      NSWarnMLog(@"Not adding duplicate '%@' to interface '%@'.",
        nodeName, name);
      return;
    }
    [dict setObject: node
             forKey: nodeName];
  }
}

/**
 * Adds a method to the interface.
 */
- (void)addMethod: (DKMethod*)method
{
  [self _addMember: method
            toDict: methods];
}

/**
 * Adds a signal to the interface.
 */
- (void)addSignal: (DKSignal*)signal
{
  [self _addMember: signal
            toDict: signals];
}

- (void)addProperty: (DKProperty*)property
{
  [self _addMember: property
            toDict: properties];
}

/**
 * Removes the signal specified. Needed by DKNotificationCenter to replace stub
 * signals with the real introspected specification.
 */
- (void)removeSignalNamed: (NSString*)signalName
{
  if (nil != signalName)
  {
    [signals removeObjectForKey: signalName];
  }
}

- (void) installMethod: (DKMethod*)method
           forSelector: (SEL)selector
{
  selector = sel_getUid(sel_getName(selector));
  if ((method == nil) || (0 == selector))
  {
    return;
  }

  if ((nil == [methods objectForKey: [method name]])
    && (NO == [method isKindOfClass: [DKPropertyMethod class]]))
  {
    [self addMethod: method];
  }
  if (NULL != NSMapInsertIfAbsent(selectorToMethodMap, selector, method))
  {
    NSWarnMLog(@"Overloading selector '%@' for method '%@' in interface '%@' not supported",
      NSStringFromSelector(selector),
      [method name],
      name);
  }
}

/** Installs the method with its default selector. */
- (void)installMethod: (DKMethod*)aMethod
{
  const char* selectorString = [[aMethod selectorString] UTF8String];
  SEL untypedSelector = 0;

  if (NULL == selectorString)
  {
    NSWarnMLog(@"Cannot register selector with empty name for method %@");
    return;
  }

  untypedSelector = sel_registerName(selectorString);
  [self installMethod: aMethod
          forSelector: untypedSelector];
  NSDebugMLog(@"Registered %s as %p.",
    selectorString,
    untypedSelector);
}

- (void)installMethods
{
  NSEnumerator *methodEnum = [methods objectEnumerator];
  DKMethod *method = nil;
  SEL installationSelector = @selector(installMethod:);
  IMP installMethod = [self methodForSelector: installationSelector];
  while (nil != (method = [methodEnum nextObject]))
  {
    installMethod(self, installationSelector, method);
  }
}

- (void)installProperties
{
  NSEnumerator *propertyEnum = [properties objectEnumerator];
  DKProperty *property = nil;
  SEL installationSelector = @selector(installMethod:);
  IMP installMethod = [self methodForSelector: installationSelector];
  while (nil != (property = [propertyEnum nextObject]))
  {
    DKPropertyAccessor *accessor = [property accessorMethod];
    DKPropertyMutator *mutator = [property mutatorMethod];
    BOOL accessorExists = (nil != [self DBusMethodForSelector: NSSelectorFromString([accessor selectorString])]);
    BOOL mutatorExists = (nil != [self DBusMethodForSelector: NSSelectorFromString([mutator selectorString])]);
    if ((nil != accessor) && (NO == accessorExists))
    {
      installMethod(self, installationSelector, accessor);
    }
    if ((nil != mutator) && (NO == mutatorExists))
    {
      installMethod(self, installationSelector, mutator);
    }
  }
}

- (void)registerSignalsWithNotificationCenter: (DKNotificationCenter*)center
{
  NSEnumerator *signalEnum = [signals objectEnumerator];
  DKSignal *signal = nil;
  SEL registrationSelector = @selector(registerWithNotificationCenter:);
  IMP registerSignal = class_getMethodImplementation([DKSignal class],
    registrationSelector);
  while (nil != (signal = [signalEnum nextObject]))
  {
    registerSignal(signal, registrationSelector, center);
  }
}


- (void)registerSignals
{
  DKProxy *theProxy = [self proxyParent];
  DKNotificationCenter *theCenter = nil;
  if (nil == theProxy)
  {
    return;
  }
  theCenter = [DKNotificationCenter centerForBusType: [[theProxy _endpoint] DBusBusType]];
  [self registerSignalsWithNotificationCenter: theCenter];
}


- (DKMethod*) DBusMethodForSelector: (SEL)selector
                          normalize: (BOOL)doNormalize
{
  DKMethod *theMethod = nil;
  if (0 == selector)
  {
    return nil;
  }
  if (doNormalize)
  {
    selector = sel_getUid(sel_getName(selector));
    return NSMapGet(selectorToMethodMap, selector);
  }
  else
  {
    theMethod = NSMapGet(selectorToMethodMap, selector);
    if (nil == theMethod)
    {
      // Second chance, find a normalized method:
      return [self DBusMethodForSelector: selector
                               normalize: YES];
    }
  }
  return theMethod;
}

- (DKMethod*)DBusMethodForSelector: (SEL)selector
{
  return [self DBusMethodForSelector: selector
                           normalize: NO];
}

- (NSString*)mangledName
{
  return [name stringByReplacingOccurrencesOfString: @"." withString: @"_"];
}

- (NSString*)protocolName
{
  NSString *protocolName = [annotations objectForKey: @"org.gnustep.objc.protocol"];
  if (nil == protocolName)
  {
    protocolName = [self mangledName];
  }
  return protocolName;
}

- (NSString*)protocolDeclaration
{
  NSMutableString *declaration = [NSMutableString stringWithFormat: @"@protocol %@\n\n", [self protocolName]];
  NSEnumerator *methodEnum = [methods objectEnumerator];
  NSEnumerator *propertyEnum = [properties objectEnumerator];
  DKMethod *method = nil;
  DKProperty *property = nil;
  while (nil != (method = [methodEnum nextObject]))
  {
    [declaration appendFormat: @"%@\n\n", [method methodDeclaration]];
  }

  // TODO: Also generate Objective-C 2 syle @property declarations.
  while (nil != (property = [propertyEnum nextObject]))
  {
    DKPropertyAccessor *accessor = [property accessorMethod];
    DKPropertyMutator *mutator = [property mutatorMethod];

    if (nil != accessor)
    {
      [declaration appendFormat: @"%@\n\n", [accessor methodDeclaration]];
    }
    if (nil != mutator)
    {
      [declaration appendFormat: @"%@\n\n", [mutator methodDeclaration]];
    }
  }
  [declaration appendFormat: @"@end\n"];
  return declaration;
}

- (Protocol*)protocol
{
  return NSProtocolFromString([self protocolName]);
}

- (void)setMethods: (NSMutableDictionary*)newMethods
{
  ASSIGN(methods,newMethods);
  [[methods allValues] makeObjectsPerformSelector: @selector(setParent:)
                                       withObject: self];
}

- (void)setSignals: (NSMutableDictionary*)newSignals
{
  ASSIGN(signals,newSignals);
  [[signals allValues] makeObjectsPerformSelector: @selector(setParent:)
                                       withObject: self];
}

- (void)setProperties: (NSMutableDictionary*)newProperties
{
  ASSIGN(properties,newProperties);
  [[properties allValues] makeObjectsPerformSelector: @selector(setParent:)
                                          withObject: self];
}

/**
 * Regenerate the map table from another one, e.g. when copying the object.
 */
- (void)regenerateSelectorMethodMapWithMap: (NSMapTable*)sourceMap
                                   andZone: (NSZone*)zone
{
  /*
   * Keep a reference to the old map, just in case somebody is actually making
   * us regenerate our own mappings.
   */
  NSMapTable *oldMap = selectorToMethodMap;

  /* Setup enumerator and associated variables. */
  NSMapEnumerator theEnum = NSEnumerateMapTable(sourceMap);
  SEL thisSel = 0;
  DKMethod *thisMethod = nil;

  if (NULL == zone)
  {
    zone = NSDefaultMallocZone();
  }

  /*
   * Create a new map table, setting the capacity to the one we know from the
   * sourceMap.
   */
  selectorToMethodMap = NSCreateMapTableWithZone(NSIntMapKeyCallBacks,
    NSObjectMapValueCallBacks,
    NSCountMapTable(sourceMap),
    zone);

  /*
   * Enumerate the source map and add selector-method pairs with the matching
   * methods from our own method table.
   */
  while (NSNextMapEnumeratorPair(&theEnum, (void**)&thisSel, (void**)&thisMethod))
  {
    DKMethod *newMethod = [methods objectForKey: [thisMethod name]];
    if (newMethod)
    {
      NSMapInsert(selectorToMethodMap, (void*)thisSel, (void*)newMethod);
    }
  }
  NSEndMapTableEnumeration(&theEnum);

  /* Free the old map table, if any. */
  if (NULL != oldMap)
  {
    NSFreeMapTable(oldMap);
  }
}

- (id)copyWithZone: (NSZone*)zone
{
  DKInterface *newNode = [super copyWithZone: zone];
  NSMutableDictionary *newMethods = nil;
  NSMutableDictionary *newSignals = nil;
  NSMutableDictionary *newProperties = nil;
  newMethods = [[NSMutableDictionary allocWithZone: zone] initWithDictionary: methods
                                                                   copyItems: YES];
  newSignals = [[NSMutableDictionary allocWithZone: zone] initWithDictionary: signals
                                                                   copyItems: YES];
  newProperties = [[NSMutableDictionary allocWithZone: zone] initWithDictionary: properties
                                                                      copyItems: YES];
  [newNode setMethods: newMethods];
  [newNode regenerateSelectorMethodMapWithMap: selectorToMethodMap
                                      andZone: zone];
  [newNode setSignals: newSignals];
  [newNode setProperties: newProperties];
  [newMethods release];
  [newSignals release];
  [newProperties release];
  return newNode;
}

- (void)dealloc
{
  [methods release];
  [signals release];
  [properties release];
  NSFreeMapTable(selectorToMethodMap);
  [super dealloc];
}
@end