对象接口

内容表

关于

The maxon::ObjectInterface interface is the base of all interfaces. It defines the most basic functionality of all interface based objects. A custom implementation can (re)implement the methods of this interface.

注意
Every component can also implement various basic functions, see Default Functions .

Methods

These functions of the maxon::ObjectInterface interface are marked with MAXON_METHOD . They can be implemented in any interface based on maxon::ObjectInterface and are added to the reference class.

General Functions:

Comparison and Identity:

注意
The hash values returned by maxon::ObjectInterface::GetHashCode() should be used for hash tables etc. They should not be used for security related tasks e.g. storing password hashes.
For interfaces of the type MAXON_REFERENCE_NORMAL the hash value is based on the pointer; the method is not called. Only for copy-on-write interfaces the method is called.
It is advised to call the super function within maxon::ObjectInterface::IsEqual() etc. since a component may be reused in an unpredicted context.

函数

These function are marked with MAXON_FUNCTION . They cannot be implemented in a component but can be used in an implementation and in the reference class.

Component classes and interfaces:

Utility:

Internal

This function can only be used inside an implementation:

Implementation

// This example shows the implementation of basic maxon::ObjectInterface methods.

// implementation of AtomInterface class AtomImpl : public maxon::Component <AtomImpl, AtomInterface> { MAXON_COMPONENT ();

public : // AtomInterface methods
MAXON_METHOD maxon::Result<void> SetAtomData( maxon::Int protonCnt, maxon::Int neutronCnt, maxon::Int electronCnt) { _protonCnt = protonCnt; _neutronCnt = neutronCnt; _electronCnt = electronCnt; return maxon::OK ; } MAXON_METHOD maxon::Result<void> GetAtomData( maxon::Int & protonCnt, maxon::Int & neutronCnt, maxon::Int & electronCnt) const { protonCnt = _protonCnt; neutronCnt = _neutronCnt; electronCnt = _electronCnt; return maxon::OK ; }

// maxon::ObjectInterface methods MAXON_METHOD maxon::Result<void> InitObject( const void * argument) { // every new atom should be hydrogen by default _protonCnt = 1; _electronCnt = 1; _neutronCnt = 0; return maxon::OK ; } MAXON_METHOD maxon::COMPARERESULT 比较 ( const maxon::ObjectInterface * other) const { const maxon::COMPARERESULT superCompare = super.Compare(other); if (superCompare != maxon::COMPARERESULT::EQUAL ) return superCompare; const AtomImpl* const atom = GetOrNull (other); if (!atom) return maxon::COMPARERESULT::INCOMPARABLE ; const maxon::Int sum = _protonCnt + _neutronCnt + _electronCnt; const maxon::Int sumOther = atom->_protonCnt + atom->_neutronCnt + atom->_electronCnt; if (sum < sumOther) return maxon::COMPARERESULT::LESS ; if (sum > sumOther) return maxon::COMPARERESULT::GREATER ; return maxon::COMPARERESULT::EQUAL ; } MAXON_METHOD maxon::String ToString ( const maxon::FormatStatement * formatStatement) const { return FormatString ( "Protons: @, Neutrons: @, Electrons: @" , _protonCnt, _neutronCnt, _electronCnt); }

// implementing maxon::ComponentRoot::CopyFrom() maxon::Result<void> CopyFrom( const AtomImpl& atom) { _protonCnt = atom._protonCnt; _neutronCnt = atom._neutronCnt; _electronCnt = atom._electronCnt; return maxon::OK ; } private : maxon::Int _protonCnt = 0; maxon::Int _neutronCnt = 0; maxon::Int _electronCnt = 0; };

In a copy-on-write interface also maxon::ObjectInterface::GetHashCode() can be implemented.

// This example shows an implementation of a COW interface. It implements GetHashCodeImpl().

// implementation of ErrorCodeInterface class ErrorCodeImp : public maxon::Component <ErrorCodeImp, ErrorCodeInterface> { MAXON_COMPONENT (); public : // ErrorCodeInterface methods MAXON_METHOD void SetErrorCode( maxon::Int errorCode) { _error = errorCode; } MAXON_METHOD maxon::Int GetErrorCode() const { return _error; } MAXON_METHOD void SetTimestamp( maxon::UInt timestamp) { _timestamp = timestamp; } MAXON_METHOD maxon::UInt GetTimeStamp() const { return _timestamp; }

// maxon::ObjectInterface methods MAXON_METHOD maxon::Bool IsEqual ( const maxon::ObjectInterface * other) const { const maxon::Bool superEqual = super.IsEqual(other); if (!superEqual) return false ; const ErrorCodeImp* const error = GetOrNull (other); if (!error) return false ; if (_error != error->_error) return false ; if (_timestamp != error->_timestamp) return false ; return true ; } MAXON_METHOD maxon::UInt GetHashCodeImpl() const { // from maxon/crc32c.h maxon::Crc32C crc; const maxon::UInt superHash = super.GetHashCodeImpl(); crc. UpdateUInt (superHash); crc. UpdateInt (_error); crc. UpdateUInt (_timestamp); return crc. GetCrc (); }

// implementing maxon::ComponentRoot::CopyFrom() maxon::Result<void> CopyFrom( const ErrorCodeImp& error) { _error = error._error; _timestamp = error._timestamp; return maxon::OK ; } private : maxon::Int _error = 0; maxon::UInt _timestamp = 0; };

用法

Basic methods of maxon::ObjectInterface are simply used with the reference object:

// This example shows how to use basic maxon::ObjectInterface functions. const AtomRef hydrogen = componentClass.Create() iferr_return ;

// use ToString() DiagnosticOutput ( "Hydrogen: @" , hydrogen); const AtomRef oxygen = componentClass.Create() iferr_return ; oxygen.SetAtomData(8, 8, 8) iferr_return ;

// use ToString() DiagnosticOutput ( "Oxygen: @" , oxygen);

// compare

if (hydrogen.IsEqual(oxygen) == false ) DiagnosticOutput ( "The atoms are not equal!" ); const maxon::UInt hashHydrogen = hydrogen.GetHashCode(); const maxon::UInt hashOxygen = oxygen.GetHashCode(); DiagnosticOutput ( "Hashes: @, @" , hashHydrogen, hashOxygen);

// use Object::IsInstanceOf() if (!hydrogen.IsInstanceOf<AtomInterface>()) return maxon::UnexpectedError( MAXON_SOURCE_LOCATION );

// use Object::Clone() const AtomRef clone = hydrogen.Clone() iferr_return ; DiagnosticOutput (clone.ToString( nullptr ));

References of a copy-on-write interface are used like this:

// This example shows how to use COW objects and when data will be copied. ErrorCode error = ErrorComponentClass.Create() iferr_return ; error.SetErrorCode(100) iferr_return ; error.SetTimestamp(10000) iferr_return ; ErrorCode copy = error;

// GetHashCodeImpl() of the implementation is called maxon::UInt hashA = error.GetHashCode(); maxon::UInt hashB = copy.GetHashCode(); DiagnosticOutput ( "Hashes: @, @" , hashA, hashB);

// this will call CopyFrom() to create a new instance copy.SetErrorCode(200) iferr_return ; hashA = error.GetHashCode(); hashB = copy.GetHashCode(); DiagnosticOutput ( "Hashes: @, @" , hashA, hashB);

延伸阅读

maxon::ComponentWithBase::GetOrNull
static C * GetOrNull(ObjectInterface *object)
定义: objectbase.h:2501
maxon::IsEqual
MAXON_ATTRIBUTE_FORCE_INLINE Bool IsEqual(PREDICATE &&predicate, const T1 &a, const T2 &b)
定义: collection.h:102
maxon::ComponentWithBase
定义: objectbase.h:2472
maxon::Crc32C::UpdateInt
MAXON_ATTRIBUTE_FORCE_INLINE void UpdateInt(Int i)
定义: crc32c.h:543
maxon::COMPARERESULT::EQUAL
@ EQUAL
result is equal
MAXON_COMPONENT
#define MAXON_COMPONENT(KIND,...)
定义: objectbase.h:2036
maxon::String
定义: string.h:1197
maxon::OK
return OK
定义: apibase.h:2532
maxon::Bool
bool Bool
boolean type, possible values are only false/true, 8 bit
定义: apibase.h:177
iferr_return
#define iferr_return
定义: resultbase.h:1434
MAXON_SOURCE_LOCATION
#define MAXON_SOURCE_LOCATION
定义: memoryallocationbase.h:66
DiagnosticOutput
#define DiagnosticOutput(formatString,...)
定义: debugdiagnostics.h:166
maxon::Crc32C::GetCrc
UInt32 GetCrc() const
定义: crc32c.h:66
maxon::Crc32C::UpdateUInt
MAXON_ATTRIBUTE_FORCE_INLINE void UpdateUInt(UInt u)
定义: crc32c.h:503
maxon::Result< void >
MAXON_METHOD
#define MAXON_METHOD
定义: interfacebase.h:855
maxon::ToString
String ToString(const String &str, const FormatStatement *formatStatement, Bool=false)
定义: string.h:1680
maxon::Int
Int64 Int
signed 32/64 bit int, size depends on the platform
定义: apibase.h:184
maxon::COMPARERESULT::LESS
@ LESS
result is less than
maxon::ObjectInterface
定义: objectbase.h:1285
maxon::COMPARERESULT::INCOMPARABLE
@ INCOMPARABLE
values are incomparable (either a compare function doesn't exist, or we have a partial order)
maxon::COMPARERESULT::GREATER
@ GREATER
result is greater than
FormatString
#define FormatString(...)
定义: string.h:2031
maxon::FormatStatement
Class to store formatting statements.
定义: string.h:1960
maxon::Crc32C
定义: crc32c.h:26
maxon::UInt
UInt64 UInt
unsigned 32/64 bit int, size depends on the platform
定义: apibase.h:185
maxon::Compare
COMPARERESULT Compare(const GradientKnot &a, const GradientKnot &b)
定义: gradient.h:47
maxon::COMPARERESULT
COMPARERESULT
Data type for comparison results.
定义: compare.h:20

Copyright  © 2014-2025 乐数软件    

工业和信息化部: 粤ICP备14079481号-1