Boost logo

Boost :

From: Philippe A. Bouchard (philippe_at_[hidden])
Date: 2003-09-29 16:54:07


Douglas Gregor wrote:

[...]

> I got lost along the way. From the documentation, I understand how to
> allocate an object to be placed in a shifted_ptr, and I infer that
> the semantics are a little like shared_ptr's semantics, but are they?
> More specifically:
>
> - What are the semantics for the various collector_traits options? I
> see rc_collector, gc_collector, and os_collector: what the semantics
> of these three collectors, and how do they compare to each other? To
> shared_ptr?

Those semantics are defined at compile-time for a given class, once they are
defined it is possible to override them with the second template argument of
shifted_ptr.

I felt it was better designed this way because it would be hidden to the
user for a given library.

Ex.:

sp.hpp:
#include <shifted_ptr.hpp>

template <typename T>
  struct neuron
  {
   T i;
   list< shifted_ptr<neuron> > p;
  };

template <typename T>
  struct collector_traits< neuron<T> >
  {
   typedef os_collector type;
  };

// The following defaults to gc_collector.
template <typename T>
  struct tree
  {
   tree<T> child;
   shifted_ptr<T> element;
  };

// The user here knows nothing about os_collector & gc_collector. He
doesn't care about efficienty problems, he just want it to be fast.
main.cpp:
#include "sp.hpp"
#include <string>

using namespace std;
using namespace boost;

int main()
{
  shifted_ptr< neuron<string> > p = new (so) neuron<string>();

  // You built some sort of neural net here without know it's os_collector
dependencies:
  for (int i = ...; ; )
  {
    p->p.push_back(new (so) neuron<string>());
  }

  // You built a tree without worrying about it's gc_collector dependencies:
  tree<string> t;

  t.element = new (so) string("filename1.txt");
  ...
}

This design is different from shared_ptr in that this need is based on a
trait, but the name of the pointer (i.e. shifted_ptr) remains the same. It
would be possible also to add another wk_collector (weak pointer). This
design is another proposal at the same time.

> - Can I upcast/downcast a shifted_ptr? I'm looking for
> something like static_pointer_cast/dynamic_pointer_cast but I don't
> see them.

There is missing documentation on this, but yes this function can easily be
implemented.

> - Can I float between raw pointers and shifted_ptrs for a
> particular object? This would be suicide with shared_ptr, but isn't
> so with many garbage-collected pointers or when the reference count
> is internal to the object.

It would be possible to cast a shifted_ptr to a raw pointer type but not the
inverse since it would be error-prone.

[...]

> - A clear description of the semantics of each operation. Doxygen
> does well enough [1] at presenting the interface, but with the
> description of functions or classes is as short as "Affector", it
> doesn't help.

Ok, Doxygen is a failure... I'll use Microsoft Word so I cannot be mistaken.
The documentation will be rewritten in its objectivity. It is the first
time I am doing a review remotely, my apologies again.

Thanks for the analysis it is really appreciated,

Philippe


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk