Factories

内容表

关于

The maxon::Factory template is used to create factory functions for a given interface. Such factory functions allow to safely create new, initialized instances of that interface.

Declaration

Based on the maxon::Factory template, a factory is typically declared as a published object using MAXON_DECLARATION .

// This example shows the decalaration of a factory creating a StringHashRef object from a given maxon::String argument. // StringHashInterface is a custom interface.
using StringHashFactoryType = maxon::Factory<StringHashRef( maxon::String )>; MAXON_DECLARATION (StringHashFactoryType, StringHashFromStringFactory, "net.maxonexample.factory.stringhashfromstring" );

Implementation

The published object containing the new factory must be defined in a source code file using MAXON_DECLARATION_REGISTER . The behaviour of the factory depends on a referenced function that is invoked by the factory:

范例

This example shows a simple interface that just provides read-only access to its data.

// This example shows a simple interface that stores the hash value of a string. // The hash of a given string can be compared to this stored hash value.

// --------------------------------------------------------------------- // Class to store a string's hash value. // --------------------------------------------------------------------- class StringHashInterface : MAXON_INTERFACE_BASES (maxon::ObjectInterface) { MAXON_INTERFACE (StringHashInterface, MAXON_REFERENCE_NORMAL , "net.maxonexample.stringhash" ); public : //---------------------------------------------------------------------------------------- // Compares the hash value of the given string with the stored hash value. // @return True if the hashes are identical. May return an error if no hash value could be calculated. //---------------------------------------------------------------------------------------- MAXON_METHOD maxon::Result<maxon::Bool> IsStringEqual( maxon::String str) const ; };

The implementation can look like this:

// This example shows the implementation of a simple interface. The public functions only give // indirect access to the internally stored data. The data must be set when an instance is created.

// implementation of StringHashInterface class SecretStringHashImplementation : public maxon::Component <SecretStringHashImplementation, StringHashInterface> { MAXON_COMPONENT (); public : MAXON_METHOD maxon::Result<maxon::Bool> IsStringEqual( maxon::String str) const { iferr_scope ; // create hash value of the given string const maxon::String hash = maxon::GetPasswordHash (str, maxon::StreamConversions::HashSHA256()) iferr_return ; // compare return _hash. IsEqual (hash); }

// initializes the object with the given hash value maxon ::Result< void > InitFromHash(const maxon :: String & hash) { iferr_scope ; if (_hash.IsPopulated()) return maxon::IllegalStateError( MAXON_SOURCE_LOCATION , "SecretStringHash is already initialized." _s); if (hash.GetLength() != 64) return maxon::IllegalArgumentError( MAXON_SOURCE_LOCATION , "Invalid hash size." _s); _hash = hash; return maxon::OK ; }

// initializes the object with the given string maxon::Result<void> InitFromString( maxon::String & string ) { iferr_scope ; // create hash value of the given string const maxon::String hash = maxon::GetPasswordHash ( string , maxon::StreamConversions::HashSHA256()) iferr_return ; // init return InitFromHash(hash); }

// Factory method to be used with Factory::CreateObjectFactory() maxon ::Result< void > FactoryInit( maxon ::FactoryInterface::ConstPtr, maxon :: String string ) { iferr_scope ; InitFromString( string ) iferr_return ; return maxon::OK ; } private : maxon::String _hash; };

Since it should not be possible to change the internal data of an existing instance, the internal data must be set when the object is created. Within a header file a factory can be defined as a published object.

// This example shows the decalaration of a factory creating a StringHashRef object from a given maxon::String argument. // StringHashInterface is a custom interface. using StringHashFactoryType = maxon::Factory<StringHashRef( maxon::String )>; MAXON_DECLARATION (StringHashFactoryType, StringHashFromStringFactory, "net.maxonexample.factory.stringhashfromstring" );

A factory can call a static function that in return will create a new instance with the given arguments:

// This example shows a static function that is defined in the same source code file as the implementation. // This static function creates a new instance and configures it by accessing an internal implementation function. // The factory below creates the new instance by calling this static function. static maxon::Result<StringHashRef> MakeSecretStringFromHash(maxon::FactoryInterface::ConstPtr factory, maxon::String hash) { iferr_scope ; // create new instance StringHashRef res = SecretStringHashImplementation::GetClass().Create() iferr_return ; // get implementation class instance SecretStringHashImplementation* const implementation = SecretStringHashImplementation::Get (res); // configure instance implementation->InitFromHash(hash) iferr_return ; return res; } MAXON_DECLARATION_REGISTER (StringHashFromHashFactory) { return StringHashFactoryType::CreateFactory(&MakeSecretStringFromHash); }

A different factory can invoke a member function of the implementation class. Then it will create an instance of that specific implementation automatically:

// This example shows the registration of a published object defining a factory. // CreateObjectFactory() is calling a member function of the implementation class. // It will create a new instance of this implementation and then call the given member function. MAXON_DECLARATION_REGISTER (StringHashFromStringFactory) { return StringHashFactoryType::CreateObjectFactory(&SecretStringHashImplementation::FactoryInit); }

The factory is then used as any other published object:

// This example creates a new instance of StringHashInterface using a factory.

// create new instance const StringHashRef stringHash = StringHashFromStringFactory().Create(secretText) iferr_return ;

// use new instance const maxon::Bool equal = stringHash.IsStringEqual( "Hello World" _s) iferr_return ; if (equal) DiagnosticOutput ( "The given string is equal to the secret string." );

延伸阅读

MAXON_DECLARATION_REGISTER
#define MAXON_DECLARATION_REGISTER(...)
定义: module.h:872
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
The maxon namespace contains all declarations of the MAXON API.
定义: c4d_basedocument.h:15
MAXON_INTERFACE_BASES
#define MAXON_INTERFACE_BASES(...)
定义: objectbase.h:977
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
String
定义: c4d_string.h:38
maxon::Result
定义: apibase.h:314
MAXON_METHOD
#define MAXON_METHOD
定义: interfacebase.h:855
MAXON_DECLARATION
#define MAXON_DECLARATION(T, Name, id)
定义: module.h:797
maxon::Classes::Get
const Class< R > & Get(const Id &cls)
定义: objectbase.h:1903
iferr_scope
#define iferr_scope
定义: resultbase.h:1343
MAXON_INTERFACE
#define MAXON_INTERFACE(Name, REFKIND, ID)
定义: objectbase.h:1048
MAXON_REFERENCE_NORMAL
#define MAXON_REFERENCE_NORMAL(DUMMY)
定义: interfacebase.h:957
maxon::GetPasswordHash
Result< String > GetPasswordHash(const String &password, const StreamConversionFactory &hashClass, const DataDictionary &settings=DataDictionary())

Copyright  © 2014-2025 乐数软件    

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