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;
};
maxon::String ToString(const Filename &val, const maxon::FormatStatement *formatStatement, maxon::Bool checkDatatype=false)
Definition: objectbase.h:2651
Class to store formatting statements.
Definition: string.h:2030
Definition: objectbase.h:1371
Definition: string.h:1235
PyObject * other
Definition: dictobject.h:70
#define atom
Definition: graminit.h:72
#define argument
Definition: graminit.h:83
COMPARERESULT
Data type for comparison results.
Definition: compare.h:21
Int64 Int
signed 32/64 bit int, size depends on the platform
Definition: apibase.h:188
return OK
Definition: apibase.h:2690
@ LESS
result is less than
@ INCOMPARABLE
values are incomparable (either a compare function doesn't exist, or we have a partial order)
@ EQUAL
result is equal
@ GREATER
result is greater than
#define MAXON_COMPONENT(KIND,...)
Definition: objectbase.h:2212
#define FormatString(...)
Definition: string.h:2100
#define MAXON_METHOD
Definition: interfacebase.h:1001
Bool Compare(const T &x, const T &y, GETINT &&getInt)
Definition: sort_paradis.h:24

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;
};
Definition: crc32c.h:27
UInt32 GetCrc() const
Definition: crc32c.h:66
MAXON_ATTRIBUTE_FORCE_INLINE void UpdateUInt(UInt u)
Definition: crc32c.h:503
MAXON_ATTRIBUTE_FORCE_INLINE void UpdateInt(Int i)
Definition: crc32c.h:543
PyObject * error
Definition: codecs.h:206
bool Bool
boolean type, possible values are only false/true, 8 bit
Definition: apibase.h:181
UInt64 UInt
unsigned 32/64 bit int, size depends on the platform
Definition: apibase.h:189
MAXON_ATTRIBUTE_FORCE_INLINE Bool IsEqual(PREDICATE &&predicate, const T1 &a, const T2 &b)
Definition: collection.h:102

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));
Definition: object_interface.h:10
#define DiagnosticOutput(formatString,...)
Definition: debugdiagnostics.h:176
#define MAXON_SOURCE_LOCATION
Definition: memoryallocationbase.h:67
#define iferr_return
Definition: resultbase.h:1519

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