summaryrefslogtreecommitdiff
path: root/XMPCore/source/ArrayNodeImpl.cpp
blob: 30895c0dcdfa6089c7ea1d9211e622af0829932a (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
// =================================================================================================
// 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. 
// =================================================================================================

#define IMPLEMENTATION_HEADERS_CAN_BE_INCLUDED 1
	#include "XMPCore/ImplHeaders/ArrayNodeImpl.h"
	#include "XMPCore/ImplHeaders/TNodeIteratorImpl.h"
#undef IMPLEMENTATION_HEADERS_CAN_BE_INCLUDED

#include "XMPCommon/Interfaces/IError_I.h"
#include "XMPCore/XMPCoreErrorCodes.h"
#include "XMPCommon/Interfaces/IUTF8String_I.h"
#include "XMPCommon/Utilities/AutoSharedLock.h"
#include "XMPCore/Interfaces/INodeIterator_I.h"
#include "XMPCommon/Utilities/TSmartPointers_I.h"

namespace AdobeXMPCore_Int {

	// All virtual functions
	
	ArrayNodeImpl::ArrayNodeImpl( const char * nameSpace, sizet nameSpaceLength, const char * name, sizet nameLength, eArrayForm arrayForm )
		: NodeImpl( nameSpace, nameSpaceLength, name, nameLength )
		, mArrayForm( arrayForm ) {}

	IArrayNode_v1::eArrayForm APICALL ArrayNodeImpl::GetArrayForm() const {
		return mArrayForm;
	}

	INode_v1::eNodeType APICALL ArrayNodeImpl::GetChildNodeType() const {
		AutoSharedLock lock( mSharedMutex );
		if ( mChildren.size() != 0 )
			return mChildren[ 0 ]->GetNodeType();
		return kNTAll;
	}

	spINode APICALL ArrayNodeImpl::GetNodeAtIndex( sizet index ) {
		sizet actualIndex = index - 1;

		AutoSharedLock lock( mSharedMutex );

		if ( actualIndex < mChildren.size() ) {
			return MakeUncheckedSharedPointer( mChildren.at( actualIndex ).get(), __FILE__, __LINE__ );
		}
		return spINode_I();
	}

	void APICALL ArrayNodeImpl::InsertNodeAtIndex( const spINode & node, sizet index ) {
		sizet actualIndex = index - 1;
		{
			AutoSharedLock lock( mSharedMutex );
			if ( actualIndex > mChildren.size() )
				NOTIFY_ERROR( IError_base::kEDGeneral, kGECIndexOutOfBounds,
					"Trying to insert a node at an invalid index", IError_v1::kESOperationFatal,
					true, index, true, static_cast< sizet >( mChildren.size() ) );
		}
		bool goAhead = CheckSuitabilityToBeUsedAsChildNode( node );

		if ( goAhead ) {
			AutoSharedLock lock( mSharedMutex, true );
			auto it = mChildren.begin();
			std::advance( it, actualIndex );
			it = mChildren.insert( it, MakeUncheckedSharedPointer( node.get(), __FILE__, __LINE__ ) );
			pINode_I node_I = node->GetINode_I();
			node_I->SetIndex( index );
			node_I->ChangeParent( this );
			std::advance( it, 1 );
			for ( auto endIt = mChildren.end(); it != endIt; ++it ) {
				( *it )->GetINode_I()->SetIndex( ++index );
			}
		}
	}

	spINode APICALL ArrayNodeImpl::RemoveNodeAtIndex( sizet index ) {
		spINode node = GetNodeAtIndex( index );
		if ( node ) {
			sizet actualIndex = index - 1;
			AutoSharedLock lock( mSharedMutex, true );
			auto it = mChildren.begin();
			std::advance( it, actualIndex );
			it = mChildren.erase( it );
			node->GetINode_I()->ChangeParent( NULL );
			for ( auto endIt = mChildren.end(); it != endIt; ++it ) {
				( *it )->GetINode_I()->SetIndex( index++ );
			}
		}
		return node;
	}

	spINode APICALL ArrayNodeImpl::ReplaceNodeAtIndex( const spINode & node, sizet index ) {
		if ( CheckSuitabilityToBeUsedAsChildNode( node ) && GetNodeAtIndex( index ) ) {
			auto retValue = RemoveNodeAtIndex( index );
			InsertNodeAtIndex( node, index );
			return retValue;
		} else {
			NOTIFY_ERROR( IError_v1::kEDGeneral, kGECIndexOutOfBounds,
				"Trying to replace a node at an invalid index", IError_v1::kESOperationFatal,
				true, index, true, static_cast< sizet >( mChildren.size() ) );
		}
		return spINode();
	}

	void APICALL ArrayNodeImpl::AppendNode( const spINode & node ) {
		InsertNodeAtIndex( node, ChildCount() + 1 );
	}

	spINodeIterator APICALL ArrayNodeImpl::Iterator() {
		AutoSharedLock lock( mSharedMutex );
		auto beginIt = mChildren.begin(), endIt = mChildren.end();
		if ( beginIt == endIt )
			return spINodeIterator();
		else
			return MakeUncheckedSharedPointer( new TNodeIteratorImpl< NodeVector::iterator >( beginIt, endIt ), __FILE__, __LINE__, true );
	}

	sizet APICALL ArrayNodeImpl::ChildCount() const __NOTHROW__ {
		AutoSharedLock lock( mSharedMutex );
		return mChildren.size();
	}

	spIArrayNode APICALL ArrayNodeImpl::ConvertToArrayNode() {
		return MakeUncheckedSharedPointer( this, __FILE__, __LINE__ );
	}

	INode_v1::eNodeType APICALL ArrayNodeImpl::GetNodeType() const {
		return kNTArray;
	}

	bool APICALL ArrayNodeImpl::HasContent() const {
		AutoSharedLock lock( mSharedMutex );
		return mChildren.size() > 0;
	}

	void APICALL ArrayNodeImpl::ClearContents() {
		AutoSharedLock lock( mSharedMutex, true );
		for ( auto it = mChildren.begin(), itEnd = mChildren.end(); it != itEnd; ++it ) {
			( *it )->GetINode_I()->ChangeParent( NULL );
		}
		mChildren.clear();
	}

	spINode APICALL ArrayNodeImpl::CloneContents( bool ignoreEmptyNodes, bool ignoreNodesWithOnlyQualifiers, sizet qualifiersCount ) const {
		spIArrayNode newNode;
		if ( ignoreEmptyNodes && mChildren.size() == 0 ) {
			if ( ignoreNodesWithOnlyQualifiers && qualifiersCount == 0 )
				return newNode;
		}

		newNode = IArrayNode_I::CreateArrayNode( mNameSpace, mName, mArrayForm );

		for ( auto it = mChildren.begin(), endIt = mChildren.end(); it != endIt; ++it ) {
			spINode childNode = ( *it )->Clone( ignoreEmptyNodes, ignoreNodesWithOnlyQualifiers );
			if ( childNode ) {
				newNode->AppendNode( childNode );
			}
		}

		if ( ignoreEmptyNodes && newNode->ChildCount() == 0 ) {
			if ( ignoreNodesWithOnlyQualifiers )
				return spINode();
			else if ( !ignoreNodesWithOnlyQualifiers && qualifiersCount == 0 )
				return spINode();
		}
		return newNode;
	}

	void ArrayNodeImpl::resetChangesForChildren() const {
		AutoSharedLock lock( mSharedMutex );
		for ( auto it = mChildren.begin(), itEnd = mChildren.end(); it != itEnd; ++it ) {
			( *it )->AcknowledgeChanges();
		}
	}

	bool ArrayNodeImpl::CheckSuitabilityToBeUsedAsChildNode( const spcINode & node ) const {
		bool result = CompositeNodeImpl::CheckSuitabilityToBeUsedAsChildNode( node );
		if ( !result ) return false;

		eNodeType currentChildType = GetChildNodeType();
		if ( currentChildType == kNTAll || currentChildType == node->GetNodeType() )
			return true;
		else {
			NOTIFY_ERROR( IError_v1::kEDDataModel, kDMECArrayItemTypeDifferent,
				"node type is different than what currently array can hold", IError_v1::kESOperationFatal,
				true, static_cast< uint64 >( currentChildType ), true, static_cast< uint64 >( node->GetNodeType() ) );
		}
		return false;
	}

	spIArrayNode IArrayNode_I::CreateArrayNode( const spcIUTF8String & nameSpace, const spcIUTF8String & name, eArrayForm arrayForm ) {
		return MakeUncheckedSharedPointer( new ArrayNodeImpl(
			nameSpace ? nameSpace->c_str() : NULL, nameSpace ? nameSpace->size() : 0,
			name ? name->c_str() : NULL, name ? name->size() : 0, arrayForm ), __FILE__, __LINE__, true );
	}

	template<>
	spINode TNodeIteratorImpl< ArrayNodeImpl::NodeVector::iterator >::GetNodeFromIterator( const ArrayNodeImpl::NodeVector::iterator & it ) const {
		return MakeUncheckedSharedPointer( it->get(), __FILE__, __LINE__, false );
	}
}

#if BUILDING_XMPCORE_LIB || SOURCE_COMPILING_XMPCORE_LIB
namespace AdobeXMPCore {
	using namespace AdobeXMPCore_Int;
	spIArrayNode IArrayNode_v1::MakeShared( pIArrayNode_base ptr ) {
		if ( !ptr ) return spIArrayNode();
		pIArrayNode p = IArrayNode::GetInterfaceVersion() > 1 ? ptr->GetInterfacePointer< IArrayNode >() : ptr;
		return MakeUncheckedSharedPointer( p, __FILE__, __LINE__, false );
	}

	spIArrayNode IArrayNode_v1::CreateUnorderedArrayNode( const char * nameSpace, sizet nameSpaceLength, const char * name, sizet nameLength ) {
		return MakeUncheckedSharedPointer( new ArrayNodeImpl( nameSpace, nameSpaceLength, name, nameLength, IArrayNode::kAFUnordered ), __FILE__, __LINE__, true );
	}

	spIArrayNode IArrayNode_v1::CreateOrderedArrayNode( const char * nameSpace, sizet nameSpaceLength, const char * name, sizet nameLength ) {
		return MakeUncheckedSharedPointer( new ArrayNodeImpl( nameSpace, nameSpaceLength, name, nameLength, IArrayNode::kAFOrdered ), __FILE__, __LINE__, true );
	}

	spIArrayNode IArrayNode_v1::CreateAlternativeArrayNode( const char * nameSpace, sizet nameSpaceLength, const char * name, sizet nameLength ) {
		return MakeUncheckedSharedPointer( new ArrayNodeImpl( nameSpace, nameSpaceLength, name, nameLength, IArrayNode::kAFAlternative ), __FILE__, __LINE__, true );
	}

}
#endif  // BUILDING_XMPCORE_LIB || SOURCE_COMPILING_XMPCORE_LIB