ObjectHost

Files CCore/inc/ObjHost.h CCore/src/ObjHost.cpp

ObjectHost can host a set of objects. Each object is identified by its name.

ObjBase

ObjBase is a base class of an object to be hosted.


class ObjBase : public MemBase_nocopy , public InterfaceHost
 {
   ....

  public:
  
   ObjBase();
   
   virtual ~ObjBase();
   
   void wait_released();
   
   bool wait_released(MSec timeout);
   
   bool wait_released(TimeScope time_scope);
 };

This class is polymorphic. It has a virtual destructor, so you can destroy an object of derived type using a pointer to the base class. It is derived from MemBase_nocopy, so it is non-copyable and inherits memory allocation operators, based on CCore heap. This class is also derived from the InterfaceHost.

wait_release() methods wait until the object gets out of use. You should use these methods after you detach the object from the host, in case if you manage an object not using one of the ObjMaster class.

ObjHost

ObjHost is a hosting class. There is a default global object of this type.


class ObjHost : NoCopy
 {
   ....

  public:
  
   explicit ObjHost(TextLabel name);
   
   ~ObjHost();
   
   void add(ObjBase &obj,StrLen name);
   
   void del(ObjBase &obj);
   
   static ObjHost & Default();
   
   template <class Info> class List;
 };

Constructor uses the name to name the internal mutex.

add() attaches the given object with the given name. When it is done, the object can be "hooked" using the class ObjHook.

del() detaches the object. After it is finished, the object is no more visible, but may still be in use. Use the wait_release() method of the object to block execution while the object is used and cannot be safely destroyed.

Default() returns a reference to the default host object.

ObjHook

ObjHook "hooks" an object, attached to the host.


class ObjHook : NoCopy
 {
   ....

  public:
   
   //
   // assign/move/swap are not provided to prevent cyclic dependencies
   //
   
   // constructors
  
   ObjHook(ObjHost &host,StrLen name);
   
   explicit ObjHook(StrLen name);
    
   ObjHook(JustTryType,ObjHost &host,StrLen name);
   
   ObjHook(JustTryType,StrLen name);
    
   ~ObjHook();
   
   // cast
   
   bool operator + () const;
   
   bool operator ! () const;
    
   void requestInterface(InterfaceCaster &caster) { if( obj ) obj->requestInterface(caster); }

   template <class T>
   T * cast() const;
   
   template <class T>
   T * cast_guarded() const; 
   
   template <class T>
   operator T * () const { return cast_guarded<T>(); }
   
   // copy objects
   
   ObjHook(const ObjHook &hook);
 };

Constructor hooks an object with the given name at the given host, or default host. Try-variants do not throw exceptions if the object is not found, they create a null hook.

Destructor releases the object.

operator + and operator ! can be used to check, if the hook is null.

requestInterface() forwards the interface request to the hooked object, if any.

cast() calls the pickInterface() of the object to acquire a desired interface.

cast_guarder() is a guarded variant of the cast(), if the result if null, an exception is thrown.

Copy constructor creates a copy of the hook, it points to the same object.

ObjMaster

ObjMaster can be used to attach and detach an object to a host.


class ObjMaster : NoCopy
 {
   .... 
   
  public: 
  
   ObjMaster(ObjHost &host,ObjBase &obj,StrLen name); 
   
   ObjMaster(ObjBase &obj,StrLen name); 
   
   ~ObjMaster(); 
 };

Constructor does attach. If the host is not specified, the default host is used. The object is attached with the given name. An exception is thrown in case of error.

Destructor does detach and waits until the object gets out of use. After the destructor is finished, it is safe to destroy the object.

ObjMaster_delete

This class is similar to the ObjMaster. The difference is: the object is given by a pointer, not by a reference, and destructor deletes the object. So the object must be constructed using the operator new.


class ObjMaster_delete : NoCopy
 {
   ....
   
  public: 
  
   ObjMaster_delete(ObjHost &host,ObjBase *obj,StrLen name); 
   
   ObjMaster_delete(ObjBase *obj,StrLen name); 
   
   ~ObjMaster_delete(); 
 };

ObjHost::List

This class can be used to build a list of records with information about objects, attached to a host.


template <class Info>
class ObjHost::List : NoCopy
 {
   ....
    
  public:
  
   List();
   
   ~List(); 
   
   void build(ObjHost &host=Default());
   
   const Info * getPtr() const;
   
   const Info * getPtr_const() const;

   ulen getLen() const;
 };

Constructor creates an empty object.

build() appends the list with Info records about objects, attached to the given host. If the record contains a pointer to the object, the object is hooked and released later by the destructor of the list. Objects may be selected based on they properties to be included in the list.

The range access is provided to the record range.

There are four type of Info records.

ObjInfo

This info record contains only the name of object.


struct ObjInfo
 {
  ....

  // data
  
  StrLen name;
  
  // print object
  
  template <class P>
  void print(P &out) const;
 };

print() prints the name.

ObjInfo_obj

This info record contains the name of object and the pointer to the object.


struct ObjInfo_obj
 {
  ....

  // data
  
  StrLen name;
  ObjBase *obj;
  
  // methods
  
  ObjBase * getObj() const { return obj; }
  
  // print object
  
  template <class P>
  void print(P &out) const;
 };

print() prints the name.

ObjInfo_if

This info record contains the name of the object and selects only objects, implementing the given interface.


template <class T>
struct ObjInfo_if
 {
  ....

  // data
 
  StrLen name;
  
  // print object
  
  template <class P>
  void print(P &out) const;
 };

print() prints the name.

ObjInfo_obj_if

This info record contains the name of object and the pointer to the object and selects only objects, implementing the given interface.


template <class T>
struct ObjInfo_obj_if
 {
  ....

  // data
 
  StrLen name;
  Extra extra;
  
  // methods
  
  ObjBase * getObj() const { return extra.obj; }
  
  T * getPtr() const { return extra.ptr; }
  
  // print object
  
  template <class P>
  void print(P &out) const;
 };

print() prints the name.