About
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.
 
Definition: string.h:1287
 
maxon::Factory< StringHashRef(maxon::String)> StringHashFactoryType
[factory_example_declaration]
Definition: factoryexamples.h:34
 
#define MAXON_DECLARATION(T, Name, id,...)
Definition: module.h:939
 
  
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:
- maxon::Factory::CreateFactory(): References a static function for creation of the desired object.
 
- maxon::Factory::CreateObjectFactory(): References a member function of a specific implementation. This function will create an instance and will call that member function.
 
Examples
This example shows a simple interface that just provides read-only access to its data.
 
{
 
public:
  
  
  
  
};
[factory_example_interface]
Definition: factoryexamples.h:15
 
MAXON_INTERFACE(StringHashInterface, MAXON_REFERENCE_NORMAL, "net.maxonexample.stringhash")
 
MAXON_METHOD maxon::Result< maxon::Bool > IsStringEqual(maxon::String str) const
 
Definition: resultbase.h:766
 
void * str
Definition: bytesobject.h:77
 
#define MAXON_REFERENCE_NORMAL(FREEIMPL)
Definition: interfacebase.h:1192
 
#define MAXON_METHOD
Definition: interfacebase.h:1020
 
#define MAXON_INTERFACE_BASES(...)
Definition: objectbase.h:1049
 
  The implementation can look like this:
 
class SecretStringHashImplementation : 
public maxon::Component<SecretStringHashImplementation, StringHashInterface>
 
{
 
public:
  {
    
    
  }
 
  
  {
    if (_hash.IsPopulated())
    if (
hash.GetLength() != 64)
 
  }
 
  
  {
    
    
    return InitFromHash(
hash);
 
  }
 
  
  maxon::Result<
void> FactoryInit(
maxon::FactoryInterface::ConstPtr, 
maxon::String 
string)
 
  {
  }
 
private:
};
Definition: objectbase.h:2672
 
PyObject Py_hash_t hash
Definition: dictobject.h:35
 
Result< String > GetPasswordHash(const String &password, const StreamConversionFactory &hashClass, const DataDictionary &settings=DataDictionary())
 
return OK
Definition: apibase.h:2740
 
MAXON_ATTRIBUTE_FORCE_INLINE Bool IsEqual(PREDICATE &&predicate, const T1 &a, const T2 &b)
Definition: collection.h:102
 
#define MAXON_SOURCE_LOCATION
Definition: memoryallocationbase.h:69
 
#define MAXON_COMPONENT(KIND,...)
Definition: objectbase.h:2229
 
The maxon namespace contains all declarations of the Maxon API.
Definition: autoweight.h:21
 
#define iferr_scope
Definition: resultbase.h:1396
 
#define iferr_return
Definition: resultbase.h:1531
 
  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.
 A factory can call a static function that in return will create a new instance with the given arguments:
 
{
  
  StringHashRef 
res = SecretStringHashImplementation::GetClass().Create() 
iferr_return;
 
  
  
 
}
 
{
  return StringHashFactoryType::CreateFactory(&MakeSecretStringFromHash);
}
Py_UCS4 * res
Definition: unicodeobject.h:1113
 
#define MAXON_DECLARATION_REGISTER(...)
Definition: module.h:1008
 
const Class< R > & Get(const Id &cls)
Definition: objectbase.h:2090
 
  A different factory can invoke a member function of the implementation class. Then it will create an instance of that specific implementation automatically:
 
{
  return StringHashFactoryType::CreateObjectFactory(&SecretStringHashImplementation::FactoryInit);
}
  The factory is then used as any other published object: 
  
 
  
  const StringHashRef stringHash = StringHashFromStringFactory().Create(secretText) 
iferr_return;
 
 
  
 
  if (equal)
bool Bool
boolean type, possible values are only false/true, 8 bit
Definition: apibase.h:180
 
#define DiagnosticOutput(formatString,...)
Definition: debugdiagnostics.h:170
 
  
Further Reading