Boost logo

Boost :

Subject: [boost] [cpo-proposal] presentation of the idea
From: Santiago Tapia (santiago.tapia_at_[hidden])
Date: 2013-08-12 20:07:15


Hi everyone,

I would like to make a proposal for a new library. I only have a draft
without documentation but I would like to know people’s opinion
about the idea before making a formal proposal.

The library will be named CPO (containers for polymorphic objects)
and it will provide containers to store dynamic allocated objects from
a class hierarchy. I have drafted one container and I have two more
 in process. The cpo library could be an alternative solution for
boost pointer container library or some other solutions that use pointers.
In order to understand the idea, I think that the following example of the
use of the library might be better than a long explanation:

#include <boost/cpo/classifier.hpp>
#include <iostream>

using namespace boost::cpo;

class base
{
public:
  virtual int do_something(int a) const = 0;
};

class derived_A : public base
{
public:
  derived_A(int b) : data(b) {}
  virtual int do_something(int a) const
  {
     return a + data;
  }
protected:
  int data;
};

class derived_B : public base
{
public:
  derived_B(int b) : data(b) {}
  virtual int do_something(int a) const
  {
     return a - data;
  }
protected:
  int data;
};

int main(int argc, char** argv)
{
  classifier<base, std::allocator> collection;

  collection.insert( derived_A(21) );
  collection.insert( derived_B(40) );

  classifier<base, std::allocator>::iterator i, e = collection.end();

  for ( i = collection.begin(); i != e; ++i )
  {
    const base& x = *i;
    std::cout << x.do_something(6) << std::endl;
  }

  return 0;
}

----
Program output:
27
-34
----
Some details and advantages must be remarked in the provided
example:
- There are not any pointers in the client code (the example).
- The container's name is classifier because it classifies objects
internally using the RTTI.
- The container classifier is _not_ a sequence.
- Any iterator of the classifier dereferences to the base class.
- The allocator argument in the classifier will be applied to every object
added to the classifier. Thus, the allocator is provided without its
argument. The std::allocator is the default argument.
- The classifier uses continuous storage in memory for objects of the
same type, therefore it is expected to improve cache efficiency with respect
to solutions that use pointers (and new to allocate objects), especially if the
client code uses some cache-aware allocator.
I think that the idea will be interesting and useful.
Although I have registered a project in sourceforge, classifier_container,
I must insist: the library is under development and without documentation.
I look forward to your comments and I thank you for them in advance.
	
Best Regards,
Santiago Tapia
CITEF
CENTRO DE INVESTIGACIÓN EN TECNOLOGÍAS FERROVIARIAS | RAILWAY TECHNOLOGY
RESEARCH CENTRE
UNIVERSIDAD POLITÉCNICA DE MADRID
ESCUELA TÉCNICA SUPERIOR DE INGENIEROS INDUSTRIALES
Santiago Tapia Fernández
José Gutiérrez Abascal, 2 | 28006 Madrid
TEL + 34 91 336 4267 | FAX + 34 91 561 8618
E-MAIL stapia_at_[hidden];
www.citef.industriales.upm.es

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