summaryrefslogtreecommitdiff
path: root/XMPCommon/source/ErrorImpl.cpp
blob: 804bef57000e6097a0fad539e3ea0c8a830c6415 (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
// =================================================================================================
// 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/ErrorImpl.h"
#undef IMPLEMENTATION_HEADERS_CAN_BE_INCLUDED

#include "XMPCommon/Utilities/UTF8String.h"
#include "XMPCommon/Interfaces/IUTF8String_I.h"
#include "XMPCommon/XMPCommonErrorCodes_I.h"
#include "XMPCommon/Interfaces/IObjectFactory.h"
#include <cstdarg>

#include "XMPCommon/Utilities/TSmartPointers_I.h"
#include <sstream>
#include <iomanip>
#define PRECISION_LIMIT 6

namespace XMP_COMPONENT_INT_NAMESPACE {

	ErrorImpl::ErrorImpl( eErrorDomain domain, eErrorCode code, eErrorSeverity severity )
		: mErrorDomain( domain )
		, mErrorCode( code )
		, mErrorSeverity( severity ) { }

	ErrorImpl::~ErrorImpl() __NOTHROW__ {
		mNextError.reset();
		mLocation.reset();
		mMessage.reset();
	}

	IError_v1::eErrorCode APICALL ErrorImpl::GetCode() const {
		return mErrorCode;
	}

	IError_v1::eErrorDomain APICALL ErrorImpl::GetDomain() const {
		return mErrorDomain;
	}

	IError_v1::eErrorSeverity APICALL ErrorImpl::GetSeverity() const {
		return mErrorSeverity;
	}

	spcIUTF8String APICALL ErrorImpl::GetMessage() const {
		return mMessage;
	}

	spcIUTF8String APICALL ErrorImpl::GetLocation() const {
		return mLocation;
	}

	spcIUTF8String APICALL ErrorImpl::GetParameter( sizet index ) const {
		if ( index < mParameters.size() ) {
			return mParameters[ index ];
		} else {
			NOTIFY_ERROR( kEDGeneral, kGECParametersNotAsExpected,
				"Parameter to IError::GetParamater() is out of range", kESOperationFatal,
				true, static_cast< sizet >( mParameters.size() ),
				true, index );
			return spcIUTF8String_I();
		}
	}

	sizet APICALL ErrorImpl::GetParametersCount() const __NOTHROW__ {
		return mParameters.size();
	}

	spIError APICALL ErrorImpl::GetNextError() {
		return mNextError;
	}

	spIError APICALL ErrorImpl::SetNextError( const spIError & error ) {
		auto returnValue = mNextError;
		mNextError = error;
		return returnValue;
	}

	void APICALL ErrorImpl::SetMessage( const char * message, sizet len /*= npos */ ) __NOTHROW__ {
		if ( mMessage )
			mMessage->assign( message, len );
		else
			mMessage = IUTF8String_I::CreateUTF8String( message, len );
	}

	void APICALL ErrorImpl::SetLocation( const char * fileName, sizet lineNumber ) __NOTHROW__ {
		if ( mLocation )
			mLocation->assign( fileName, npos );
		else
			mLocation = IUTF8String_I::CreateUTF8String( fileName, npos );
		mLocation->append(":", 1 );
		UTF8StringStream ss;
		ss<<lineNumber;
		mLocation->append( ss.str().c_str(), ss.str().size() );
	}

	void APICALL ErrorImpl::AppendParameter( const char * parameter, sizet len /*= npos */ ) __NOTHROW__ {
		mParameters.push_back( IUTF8String_I::CreateUTF8String( parameter, len ) );
	}

	void APICALL ErrorImpl::AppendParameter( void * addressParameter ) __NOTHROW__ {
		UTF8StringStream stm;
		stm.setf( std::ios::hex );
        bool checkDone( false );
		bool appends0x( false );

		if ( !checkDone ) {
			UTF8StringStream stm1;
			stm1.setf( std::ios::hex );
			stm1 << "0x" << addressParameter;
			if ( stm1.str().size() > 4 ) {
				const char * charPtr = stm1.str().c_str();
				if ( charPtr[ 2 ] == '0' && ( charPtr[ 3 ] == 'x' || charPtr[ 3 ] == 'X' ) ) {
					appends0x = true;
				}
				checkDone = true;
			}
		}

		if ( appends0x ) stm << addressParameter;
		else stm << "0x" << addressParameter;
		AppendParameter( stm.str().c_str() );
	}

	template< typename numericType >
	void TAppendParameter( ErrorImpl * ptr, const numericType & numericValue ) {
        std::ostringstream oss;
        oss << numericValue;
        
		std::string str = oss.str();
		ptr->AppendParameter( str.c_str(), str.size() );
	}

	template<>
	void TAppendParameter(ErrorImpl * ptr, const double & numericValue) {
		std::ostringstream oss;
		oss << std::fixed<<std::setprecision(PRECISION_LIMIT)<<numericValue;

		std::string str = oss.str();
		ptr->AppendParameter(str.c_str(), str.size());
	}

	template<>
	void TAppendParameter(ErrorImpl * ptr, const float & numericValue) {
		std::ostringstream oss;
		oss << std::fixed << std::setprecision(PRECISION_LIMIT) << numericValue;

		std::string str = oss.str();
		ptr->AppendParameter(str.c_str(), str.size());
	}

	void APICALL ErrorImpl::AppendParameter( const uint32 & integerValue ) __NOTHROW__ {
		TAppendParameter( this, integerValue );
	}

	void APICALL ErrorImpl::AppendParameter( const uint64 & integerValue ) __NOTHROW__ {
		TAppendParameter( this, integerValue );
	}

	void APICALL ErrorImpl::AppendParameter( const int32 & integerValue ) __NOTHROW__ {
		TAppendParameter( this, integerValue );
	}

	void APICALL ErrorImpl::AppendParameter( const int64 & integerValue ) __NOTHROW__ {
		TAppendParameter( this, integerValue );
	}

	void APICALL ErrorImpl::AppendParameter( const float & floatValue ) __NOTHROW__ {
		TAppendParameter( this, floatValue );
	}

	void APICALL ErrorImpl::AppendParameter( const double & doubleValue ) __NOTHROW__ {
		TAppendParameter( this, doubleValue );
	}

	void APICALL ErrorImpl::AppendParameter( bool booleanValue ) __NOTHROW__ {
		if ( booleanValue )
			AppendParameter( "true", 4 );
		else
			AppendParameter( "false", 5 );
	}

	spIError_I IError_I::CreateError( eErrorDomain errDomain, eErrorCode errCode, eErrorSeverity errSeverity ) {
		return MakeUncheckedSharedPointer( new ErrorImpl( errDomain, errCode, errSeverity ), __FILE__, __LINE__, true );
	}
}

#if BUILDING_XMPCOMMON_LIB || SOURCE_COMPILING_XMP_ALL
namespace AdobeXMPCommon {
	using namespace XMP_COMPONENT_INT_NAMESPACE;

	spIError IError_v1::CreateError( pIObjectFactory objFactory, eErrorDomain errDomain, eErrorCode errCode, eErrorSeverity errSeverity ) {
		pcIError error( NULL );
		auto retValue = MakeShared( objFactory->CreateError( static_cast< uint32 >( errDomain ), 
			static_cast< uint32 >( errCode ), static_cast< uint32 >( errSeverity ), error ) );
		//if ( error ) throw error;
		return retValue;
	}

	spIError IError_v1::MakeShared( pIError_base ptr ) {
		if ( !ptr ) return spIError();
		pIError p = IError::GetInterfaceVersion() > 1 ? ptr->GetInterfacePointer< IError >() : ptr;
		return MakeUncheckedSharedPointer( p, __FILE__, __LINE__, false );
	}

}
#endif  // BUILDING_XMPCOMMON_LIB || SOURCE_COMPILING_XMP_ALL