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
|
//
// Lynkeos
// $Id$
//
// Created by Jean-Etienne LAMIAUD on Thu Sep 14 2006.
// Copyright (c) 2006-2018. Jean-Etienne LAMIAUD
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
#include <errno.h>
#include <objc/runtime.h>
#include "LynkeosThreadConnection.h"
#include "LynkeosProcessingParameterMgr.h"
// These notification keys are defined here to avoid a dependency on MyDocument
NSString * const LynkeosItemChangedNotification = @"LynkeosItemChanged";
NSString * const LynkeosUserInfoItem = @"item";
//! Global lock to protect dictionary and rw lock creation
static NSLock *paramLock = nil;
@implementation LynkeosProcessingParameterMgr
+ (void) initialize
{
paramLock = [[NSLock alloc] init];
}
- (id) init
{
if ( (self = [super init]) != nil )
{
_parent = nil;
_document = nil;
_parametersDict = nil;
_mainThread = [NSThread currentThread];
}
return( self );
}
- (id) initWithParent: (LynkeosProcessingParameterMgr*)parent
{
if ( (self = [self init]) != nil )
_parent = [parent retain];
return( self );
}
- (id) initWithDocument: (NSDocument <LynkeosDocument> *)document
{
if ( (self = [self init]) != nil )
_document = document; // No retain, because we are agregated to the doc
return( self );
}
- (void) dealloc
{
if ( _parent != nil )
[_parent release];
if ( _parametersDict != nil )
{
[_parametersDict release];
_parametersDict = nil;
pthread_rwlock_destroy( &_lock );
}
[super dealloc];
}
- (NSDictionary*) getDictionary { return( _parametersDict ); }
- (void) setDictionary:(NSDictionary*)dict
{
int err;
if ( _parametersDict == nil && dict != nil )
{
[paramLock lock];
err = pthread_rwlock_init( &_lock, NULL );
[paramLock unlock];
NSAssert1( err == 0, @"Failed to create the parameters RW lock : %s",
strerror(errno) );
}
if ( _parametersDict != nil )
{
[_parametersDict release];
_parametersDict = nil;
}
if ( dict != nil )
_parametersDict =
[[NSMutableDictionary dictionaryWithDictionary:dict] retain];
}
- (oneway void) notifyItemModification:(id)item
{
// Always notify in the main thread
if ( [NSThread currentThread] != _mainThread )
{
NSObject* target = (_parent != nil ? _parent : self);
[LynkeosThreadConnection performSelectorOnMainThread:
@selector(notifyItemModification:)
forObject:target
withArg:item];
}
else
{
if ( _parent != nil )
// Propagate up the chain
[_parent notifyItemModification:item];
else
{
// We are at the top : notify of the change
[[NSNotificationCenter defaultCenter] postNotificationName: LynkeosItemChangedNotification
object: _document
userInfo:
[NSDictionary dictionaryWithObject:item
forKey:LynkeosUserInfoItem]];
// To be deleted when all notifiers will implement undo
if ( ![[_document undoManager] isUndoing]
&& ![_document isDocumentEdited] )
[_document updateChangeCount:NSChangeDone];
}
}
}
- (id <LynkeosProcessingParameter>) getProcessingParameterWithRef:(NSString*)ref
forProcessing:(NSString*)processing
goUp:(BOOL)goUp
{
int err;
id param = nil;
if ( _parametersDict != nil )
{
// Nil is not authorized as an NSDictionary key
NSString *processKey =
(processing == nil ? @"LynkeosNilProcess" : processing);
// Get exclusive read access
err = pthread_rwlock_rdlock( &_lock );
NSAssert1( err == 0, @"Failed to lock the parameters for reading : %s",
strerror(errno) );
NSDictionary *processDict = [_parametersDict objectForKey:processKey];
if ( processDict != nil )
param = [processDict objectForKey:ref];
err = pthread_rwlock_unlock( &_lock );
NSAssert1( err == 0, @"Failed to unlock the parameters : %s",
strerror(errno) );
}
// Forward search to container if needed
if ( param == nil && _parent != nil && goUp )
param = [_parent getProcessingParameterWithRef:ref
forProcessing:processing goUp:YES];
return( param );
}
- (void) setProcessingParameter:(id <LynkeosProcessingParameter>)parameter
withRef:(NSString*)ref
forProcessing:(NSString*)processing
{
int err;
if ( _parametersDict == nil )
{
[paramLock lock];
err = pthread_rwlock_init( &_lock, NULL );
if ( err == 0 )
_parametersDict = [[NSMutableDictionary dictionary] retain];
[paramLock unlock];
NSAssert1( err == 0, @"Failed to create the parameters RW lock : %s",
strerror(errno) );
}
// Nil is not authorized as an NSDictionary key
NSString *processKey = (processing == nil ? @"LynkeosNilProcess" : processing);
// Get exclusive write access
err = pthread_rwlock_wrlock( &_lock );
NSAssert1( err == 0, @"Failed to lock the parameters for writing : %s",
strerror(errno) );
NSMutableDictionary *processDict = [_parametersDict objectForKey:processKey];
if ( processDict == nil )
{
// Create a fresh one
processDict = [NSMutableDictionary dictionary];
[_parametersDict setObject:processDict forKey:processKey];
}
// setValue will delete the entry if parameter is nil,
// if the entry already exists, the parameter replaces the previous one.
[processDict setValue:parameter forKey:ref];
err = pthread_rwlock_unlock( &_lock );
NSAssert1( err == 0, @"Failed to unlock the parameters : %s",
strerror(errno) );
}
@end
|