Object Interface

About

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.

Note
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:

Note
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.

Functions

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>
{
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;
}
{
const maxon::COMPARERESULT superCompare = super.Compare(other);
if (superCompare != maxon::COMPARERESULT::EQUAL)
return superCompare;
const AtomImpl* const atom = GetOrNull(other);
if (!atom)
const maxon::Int sum = _protonCnt
+ _neutronCnt
+ _electronCnt;
const maxon::Int sumOther = atom->_protonCnt
+ atom->_neutronCnt
+ atom->_electronCnt;
if (sum < sumOther)
if (sum > sumOther)
}
{
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>
{
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
{
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
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;
};

Usage

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);

Further Reading

maxon::ComponentWithBase::GetOrNull
static C * GetOrNull(ObjectInterface *object)
Definition: objectbase.h:2660
maxon::IsEqual
MAXON_ATTRIBUTE_FORCE_INLINE Bool IsEqual(PREDICATE &&predicate, const T1 &a, const T2 &b)
Definition: collection.h:102
maxon::ComponentWithBase
Definition: objectbase.h:2631
maxon::Crc32C::UpdateInt
MAXON_ATTRIBUTE_FORCE_INLINE void UpdateInt(Int i)
Definition: crc32c.h:543
maxon::COMPARERESULT::EQUAL
@ EQUAL
result is equal
MAXON_COMPONENT
#define MAXON_COMPONENT(KIND,...)
Definition: objectbase.h:2193
maxon::String
Definition: string.h:1225
maxon::OK
return OK
Definition: apibase.h:2620
maxon::Bool
bool Bool
boolean type, possible values are only false/true, 8 bit
Definition: apibase.h:183
iferr_return
#define iferr_return
Definition: resultbase.h:1465
MAXON_SOURCE_LOCATION
#define MAXON_SOURCE_LOCATION
Definition: memoryallocationbase.h:67
DiagnosticOutput
#define DiagnosticOutput(formatString,...)
Definition: debugdiagnostics.h:176
maxon::Crc32C::GetCrc
UInt32 GetCrc() const
Definition: crc32c.h:66
maxon::Crc32C::UpdateUInt
MAXON_ATTRIBUTE_FORCE_INLINE void UpdateUInt(UInt u)
Definition: crc32c.h:503
maxon::Result< void >
MAXON_METHOD
#define MAXON_METHOD
Definition: interfacebase.h:940
maxon::ToString
String ToString(const String &str, const FormatStatement *formatStatement, Bool=false)
Definition: string.h:1726
maxon::Int
Int64 Int
signed 32/64 bit int, size depends on the platform
Definition: apibase.h:190
maxon::COMPARERESULT::LESS
@ LESS
result is less than
maxon::ObjectInterface
Definition: objectbase.h:1362
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(...)
Definition: string.h:2082
maxon::FormatStatement
Class to store formatting statements.
Definition: string.h:2011
maxon::Crc32C
Definition: crc32c.h:26
maxon::UInt
UInt64 UInt
unsigned 32/64 bit int, size depends on the platform
Definition: apibase.h:191
maxon::Compare
COMPARERESULT Compare(const GradientKnot &a, const GradientKnot &b)
Definition: gradient.h:47
maxon::COMPARERESULT
COMPARERESULT
Data type for comparison results.
Definition: compare.h:20