[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-freeqt
Subject:    [freeqt] documentation on shared object substructure
From:       Christian Boos <boos () arthur ! u-strasbg ! fr>
Date:       1998-11-04 13:32:55
[Download RAW message or body]

Preston Brown writes:
 > Hello Harmony Programmers:
 > 
 > Which is the best class to study to understand the shared object
 > substructure that has been implemented?  Or is there a document of some
 > sort?

Look at file:/usr/lib/qt/html/shclass.html, the documentation from our
friends at TrollTech :)
There is a good explanation  of implicit vs. explicit sharing.

Current implementation
----------------------

The shared object support in Harmony is somewhat clunky for now:

* There is the HRefc<T> template class (src/hrefc.h) which wraps a 
  reference counter around an object of the T type.
  The functionalities are: 
   - conditional auto delete when the refcount 
     drops to 0 (HRefc::delref).
   - private copy of the referenced value (HRefc::safe_detach)

* There is also the QRefCount class, which is similar to the previous one, 
  without the embedded T value (and without the detach method).
  
  The QSharedPtr<T> template class is a wrapper for a T pointer, and
  the T class is expected to be QRefCount or a derived class.
  - QSharedPtr::deepCopy is the equivalent of safe_detach, and
  - QSharedPtr::detach_nocopy is like deepCopy except that a new 
    T value is created using the default constructor instead of the
    copy constructor.


Here is a few critics:

- The two classes are there to support sharing of two kind of 
  data: values and pointer to values. But that's not very easy to
  figure out.

- They are used for implementations details, not for Qt compatibility, 
  and therefore should not use the Q* prefix. 


Future implementation
---------------------

Now, those who have looked at my Relay++ library know that I have a
more thought out solution for that: the SharedResource class. For now, 
it follows the Tcl/Tk sharing model
(ie. preserve/eventuallyFree/release), but I will also add the Qt
model (shallow copy with operator = (), deep copy with copy (),
and detach () which is a conditional copy), 
and the Harmony usage (detach_nocopy ()) if needed.

This class is carefully designed to allow the following features:

- The reference count is embedded in the object  (this is similar to 
  the QRefCount idea).

- The references can be pointer wrappers, SharedPointer<T> (this is
  similar to the QSharedPtr class).

- The references can also be external regular pointers, and the sharing is
  done manually. This provides some speed advantages in some  situations.
  For instance, when a reference is "moved" from one container to
  another, the reference count doesn't need to change.


- Shared objects can be heap allocated or stack based.

  This is important, since this allow the creation of
  "transient" shared objects on the stack whenever useful.
  When the reference count drop to 0, the 'delete' is only issued
  for heap-based objects, not for stack-based objects.

- Shared objects can be composed

  This is very important for Harmony, as it is a cool advantage over Qt:
  a composite widget can be implemented as a composite object (either 
  using multiple inheritance or embedded objects). Now, the only
  way is to have a collection of pointers. This implies an intensive
  usage of the new operator...

  See how this could look on this code sample (freely adapted from kzip)

  ------------------------------------------------------------------

  class AddOptionsDlg : public QDialog {
			Q_OBJECT
  public:
			AddOptionsDlg( QWidget *parent=0, char *name="" );
			bool onlyUpdate();
			bool storeFullPath();
  private:
			QPushButton ok;
			QPushButton cancel;
			QGroupBox gb;
			QCheckBox updatecb;
			QCheckBox fullcb;
  };
  
  
  AddOptionsDlg::AddOptionsDlg( QWidget *parent, char *name )
    : // "is a"
      QDialog( parent, name, TRUE ), 
      // "has a"
      gb( klocale.translate( "Add File Options" ), this ), 
      ok( klocale.translate("Ok"), this ),
      cancel( klocale.translate("Cancel"), this ),
      fullcb( klocale.translate("Store Full Path"), this ),
      updatecb( klocale.translate("Only Add Newer Files") , this )
  {
    gb.setAlignment( AlignLeft );
    gb.setGeometry( 10, 10, 240, 120 );
  
    ok.setGeometry( 90, 140, 70, 30 );
    connect( ok, Signal(QButton, clicked), Slot(QDialog, accept) );
  
    cancel.setGeometry( 170, 140, 70, 30 );
    connect( cancel, Signal(QButton, clicked), Slot(QDialog, reject) );
  
    fullcb.setGeometry( 30, 30, 130, 30 );
			
    updatecb.setGeometry( 30, 70, 150, 30 );
  }
  
  bool AddOptionsDlg::onlyUpdate()
  {
    return updatecb.isChecked();
  }
  
  bool AddOptionsDlg::storeFullPath()
  {
    return fullcb.isChecked();
  }


-- Christian

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic