summaryrefslogtreecommitdiff
path: root/XMPCommon/source/ConfigurableImpl.cpp
blob: 0ef3a69cb21ffbce75f70471069d19176e73c8e2 (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
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
// =================================================================================================
// Copyright Adobe
// Copyright 2014 Adobe
// All Rights Reserved
//
// NOTICE:  Adobe permits you to use, modify, and distribute this file in accordance with the terms
// of the Adobe license agreement accompanying it. If you have received this file from a source other 
// than Adobe, then your use, modification, or distribution of it requires the prior written permission
// of Adobe.
// =================================================================================================

#define IMPLEMENTATION_HEADERS_CAN_BE_INCLUDED 1
#include "XMPCommon/ImplHeaders/ConfigurableImpl.h"
#undef IMPLEMENTATION_HEADERS_CAN_BE_INCLUDED

#include "XMPCommon/Interfaces/IErrorNotifier_I.h"
#include "XMPCommon/Utilities/AutoSharedLock.h"
#include "XMPCommon/Interfaces/IError_I.h"

namespace XMP_COMPONENT_INT_NAMESPACE {

	static uint64 ConvertToLowerCase( const uint64 & key ) {
		static char add = 'a' - 'A';
		union temp { uint64 uInt64; char chars[ 8 ]; }; 
		temp t; t.uInt64 = key;
		for ( sizet i = 0; i < 8; i++ ) {
			if ( t.chars[ i ] >= 'A' && t.chars[ i ] <= 'Z' ) {
				t.chars[ i ] += add;
			}
		}
		return t.uInt64;
	}

	template < typename T1, typename T2 >
	void NotifyError( const char * errorMsg, const uint64 & key, eConfigurableErrorCode errorCode,
		IConfigurable::eDataType type, T1 v1,
		IConfigurable::eDataType oldType, T2 v2 )
	{
		bool valuePresent = type != IConfigurable::kDTNone;
		bool oldValuePresent = oldType != IConfigurable::kDTNone;
		NOTIFY_ERROR( IError_v1::kEDConfigurable, errorCode, errorMsg, IError_v1::kESOperationFatal,
			true, key, valuePresent, static_cast< uint64 >( type ), oldValuePresent, static_cast< uint64 >( oldType ),
			valuePresent, v1, oldValuePresent, v2 );
	}

	template < typename T1 >
	void NotifyError( const char * errorMsg, const uint64 & key, eConfigurableErrorCode errorCode,
		IConfigurable::eDataType type, T1 v1,
		IConfigurable::eDataType oldType, const IConfigurable::CombinedDataValue & oldValue )
	{
		char temp[2] = { '\0', '\0' };
		switch ( oldType ) {
		case IConfigurable::kDTBool:
			NotifyError< T1, bool >( errorMsg, key, errorCode, type, v1, oldType, oldValue.boolValue );
			break;

		case IConfigurable::kDTChar:
			temp[0] = oldValue.charValue;
			NotifyError< T1, const char * >( errorMsg, key, errorCode, type, v1, oldType, &temp[0] );
			break;

		case IConfigurable::kDTUint64:
			NotifyError< T1, uint64 >( errorMsg, key, errorCode, type, v1, oldType, oldValue.uint64Value );
			break;

		case IConfigurable::kDTInt64:
			NotifyError< T1, int64 >( errorMsg, key, errorCode, type, v1, oldType, oldValue.int64Value );
			break;

		case IConfigurable::kDTDouble:
			NotifyError< T1, double >( errorMsg, key, errorCode, type, v1, oldType, oldValue.doubleValue );
			break;

		case IConfigurable::kDTConstCharBuffer:
			NotifyError< T1, void * >( errorMsg, key, errorCode, type, v1, oldType, ( void * ) oldValue.constCharPtrValue );
			break;

		default:
		case IConfigurable::kDTConstVoidPtr:
			NotifyError< T1, void * >( errorMsg, key, errorCode, type, v1, oldType, ( void * ) oldValue.constVoidPtrValue );
			break;
		}
	}

    
    
    
    static void NotifyError( const char * errorMsg, const uint64 & key, eConfigurableErrorCode errorCode,
		IConfigurable::eDataType type, const IConfigurable::CombinedDataValue & value,
		IConfigurable::eDataType oldType, const IConfigurable::CombinedDataValue & oldValue )
	{
		char temp[2] = { '\0', '\0' };
		switch ( type ) {
		case IConfigurable::kDTBool:
			NotifyError< bool >( errorMsg, key, errorCode, type, value.boolValue, oldType, oldValue );
			break;

		case IConfigurable::kDTChar:
			temp[0] = value.charValue;
			NotifyError< const char * >( errorMsg, key, errorCode, type, &temp[0], oldType, oldValue );
			break;

		case IConfigurable::kDTUint64:
			NotifyError< uint64 >( errorMsg, key, errorCode, type, value.uint64Value, oldType, oldValue );
			break;

		case IConfigurable::kDTInt64:
			NotifyError< int64 >( errorMsg, key, errorCode, type, value.int64Value, oldType, oldValue );
			break;

		case IConfigurable::kDTDouble:
			NotifyError< double >( errorMsg, key, errorCode, type, value.doubleValue, oldType, oldValue );
			break;

		case IConfigurable::kDTConstCharBuffer:
			NotifyError< void * >( errorMsg, key, errorCode, type, ( void * ) value.constCharPtrValue, oldType, oldValue );
			break;

		default:
		case IConfigurable::kDTConstVoidPtr:
			NotifyError< void * >( errorMsg, key, errorCode, type, ( void * ) value.constVoidPtrValue, oldType, oldValue );
			break;
		}
	}

	//definition of class methods
	void ConfigurableImpl::SetParameter( const uint64 & actualKey, eDataType type, const CombinedDataValue & value ) {
		uint64 key = ModifyKey( actualKey );
		if ( mTreatKeyAsCaseInsensitiveCharBuffer )
			key = ConvertToLowerCase( key );
		eConfigurableErrorCode validKey = ValidateKey( key );
		eDataType oldType = kDTNone;
		CombinedDataValue oldValue;
		if ( validKey == kCECNone ) {
			AutoSharedLock lock( GetMutex(), true );
			if ( mKeysSet ) {
				auto it = mKeysSet->find( key );
				if ( it == mKeysSet->end() )
					NotifyError( "Key is not supported", key, kCECKeyNotSupported, type, value, oldType, oldValue );
			}
            
            eConfigurableErrorCode validValue = ValidateValue( key, type, value );

			if ( validValue == kCECNone && mKeyValueTypeMap ) {
				auto it = mKeyValueTypeMap->find( key );
				if ( it != mKeyValueTypeMap->end() ) {
					if ( type != it->second ) {
						validValue = kCECValueTypeNotSupported;
					}
				}
			}

			if ( validValue == kCECNone && !mAllowDifferentValueTypesForExistingEntries ) {
				auto it = mMap.find( key );
				if ( it != mMap.end() && it->second.first != type )
					validValue = kCECPreviousTypeDifferent;
			}

			if ( validValue == kCECNone ) {
				TypeValuePair pair;
				pair.first = type;
				pair.second = value;
				mMap[ key ] = pair;
			} else {
				auto it = mMap.find( key );
				if ( it != mMap.end() ) {
					oldType = it->second.first;
					oldValue = it->second.second;
				}
				NotifyError( "Validation failed for the parameter, type and value combination", key, validValue, type,
					value, oldType, oldValue );
			}
		} else {
			NotifyError( "Key is not valid", key, validKey, type, value, oldType, oldValue );
		}
	}

	bool ConfigurableImpl::GetParameter( const uint64 & actualKey, eDataType type, CombinedDataValue & value ) const {
		uint64 key = ModifyKey( actualKey );
		if ( mTreatKeyAsCaseInsensitiveCharBuffer )
			key = ConvertToLowerCase( key );
		AutoSharedLock lock( GetMutex() );
		auto it = mMap.find( key );
		if ( it == mMap.end() )
			return false;
		if ( it->second.first != type ) {
			NOTIFY_ERROR( IError_v1::kEDConfigurable, kCECValueTypeMismatch,
				"Type mismatch for a parameter", IError_v1::kESOperationFatal,
				true, key, true, static_cast< uint64 >( it->second.first ), true, static_cast< uint64 >( type ) );
			return false;
		}
		value = it->second.second;
		return true;
	}

	bool ConfigurableImpl::RemoveParameter( const uint64 & actualKey ) {
		uint64 key = ModifyKey( actualKey );
		if ( mTreatKeyAsCaseInsensitiveCharBuffer )
			key = ConvertToLowerCase( key );
		AutoSharedLock lock( GetMutex(), true );
		bool returnValue = mMap.erase( key ) >= 1 ? true : false;
		return returnValue;
	}

	std::vector< uint64 > ConfigurableImpl::GetAllParameters() const {
		auto allocator = TAllocator< uint64 >();
		std::vector< uint64 > vec( allocator );
		AutoSharedLock lock( GetMutex() );
		vec.reserve( mMap.size() );
		auto it = mMap.begin();
		auto itEnd = mMap.end();
		for ( ; it != itEnd; it++ ) {
			vec.push_back( it->first );
		}
		return vec;
	}

	sizet ConfigurableImpl::Size() const __NOTHROW__ {
		AutoSharedLock lock( GetMutex() );
		return mMap.size();
	}

	IConfigurable::eDataType ConfigurableImpl::GetDataType( const uint64 & actualKey ) const {
		uint64 key = ModifyKey( actualKey );
		if ( mTreatKeyAsCaseInsensitiveCharBuffer )
			key = ConvertToLowerCase( key );
		AutoSharedLock lock( GetMutex() );
		auto it = mMap.find( key );
		if ( it == mMap.end() )
			return kDTNone;
		else
			return it->second.first;
	}
    //using IConfigurable_I::ValidateValue;
    
	eConfigurableErrorCode ConfigurableImpl::ValidateValue( const uint64 & key, eDataType type, const CombinedDataValue & value ) const {
		return kCECNone;
	}

	void APICALL ConfigurableImpl::TreatKeyAsCaseInsensitive( bool flag ) {
		mTreatKeyAsCaseInsensitiveCharBuffer = flag;
	}

	void APICALL ConfigurableImpl::AllowDifferentValueTypesForExistingEntries( bool flag ) {
		mAllowDifferentValueTypesForExistingEntries = flag;
	}

	void APICALL ConfigurableImpl::SetAllowedKeys( uint64 * keysTable, sizet sizeOfTable ) {
		AutoSharedLock lock ( GetMutex(), true );
		if ( mKeysSet ) { delete mKeysSet; mKeysSet = NULL; }
		if ( sizeOfTable > 0 && keysTable != NULL ) {
			mKeysSet = new KeysSet();
			for ( sizet i = 0; i < sizeOfTable; i++ ) {
				uint64 key = keysTable[ i ];
				key = ModifyKey( key );
				if ( mTreatKeyAsCaseInsensitiveCharBuffer )
					key = ConvertToLowerCase( key );
				mKeysSet->insert( key );
			}
		}
	}

	void APICALL ConfigurableImpl::SetAllowedValueTypesForKeys( KeyValueTypePair * keyValueTypePairTable, sizet sizeOfTable ) {
		AutoSharedLock lock( GetMutex(), true );
		if ( mKeyValueTypeMap ) { delete mKeyValueTypeMap; mKeyValueTypeMap = NULL; }
		if ( sizeOfTable > 0 && keyValueTypePairTable != NULL ) {
			mKeyValueTypeMap = new keyValueTypeMap();
			for ( sizet i = 0; i < sizeOfTable; i++ ) {
				uint64 key = keyValueTypePairTable[i].first;
				key = ModifyKey( key );
				if ( mTreatKeyAsCaseInsensitiveCharBuffer )
					key = ConvertToLowerCase( key );
				( *mKeyValueTypeMap )[ key ] = keyValueTypePairTable[i].second;
			}
		}
	}

	ConfigurableImpl::~ConfigurableImpl() __NOTHROW__ {
		if ( mKeysSet ) { delete mKeysSet; mKeysSet = NULL; }
		if ( mKeyValueTypeMap ) { delete mKeyValueTypeMap; mKeyValueTypeMap = NULL; }
	}


}