AttachmentHost

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

AttachmentHost is a helper class. It implements the attachment/detachment functionality.

AttachmentHost stores the pointer of the type T. Any pointer of this type can be attached to the host by the method attach(). If some pointer is attached, it can be detached by the method detach(). To retrieve the attached pointer the inner class Hook is used. In the constructor it "locks" the pointer and releases it in the destructor. Hook implements the Object Pointer Interface. The method detach() is delayed while the pointer is locked.


template <class T>
class AttachmentHost : NoCopy
 {
   ....
      
  public: 
   
   explicit AttachmentHost(StrLen class_name);
   
   AttachmentHost(StrLen class_name,TextLabel name);
   
   ~AttachmentHost();
   
   void attach(T *obj);
   
   void detach();
   
   class Hook : NoCopy
    {
      T *obj;
      AntiSem *asem;
      
     public:
      
      explicit Hook(AttachmentHost<T> &host);
      
      ~Hook();
      
      // std move

      Hook(Hook &&hook)
       : obj(Replace_null(hook.obj)),
         asem(Replace_null(hook.asem))
       {
       }

      // object ptr
      
      T * operator + () const { return obj; }
      
      bool operator ! () const { return !obj; }
     
      T * getPtr() const { return obj; }
      
      T & operator * () const { return *obj; }
      
      T * operator -> () const { return obj; }
    };
 };

AttachmentHost constructor accepts one or two arguments. The argument class_name is used in error messages and should be a name of owning class. The argument name is used to name internal synchronization objects.

attach() attaches the pointer to the host. If there is an attached pointer an exception is thrown. The argument must not be null.

detach() detaches the attached pointer, if any.

These two methods are typically used in pair in constructor/destructor manner. If the first method is failed, the second must not be called.

Hook constructor retrieves the attached pointer from the host.