Boost logo

Boost :

From: JOAQUIN LOPEZ MU?Z (joaquin_at_[hidden])
Date: 2008-01-28 17:13:15

"vicente.botet" ha escrito:

> Maybe this should be managed orthogonally but, can the current
> managewith persistent flyweights?
> Which policies will we reused and which ones added?


Here is a basic example of how to make a persistent factory
(not to be confounded with serializaing flyweight objects, which
is an entirely different matter). The factory below is based
on a std::set and loads and saves its contents upon

  template<typename Entry,typename Value,const char* Filename>
  class serialized_set_factory_class:
    public boost::flyweights::factory_marker
    typedef std::set<Entry,std::less<Value> > container_type;
    typedef typename container_type::iterator handle_type;
    typedef typename container_type::value_type entry_type;

      std::ifstream ifs(Filename);
        boost::archive::text_iarchive ia(ifs);
        typename container_type::size_type s=0;
        for(typename container_type::size_type n=s;n--;){
          Value v;
        std::clog<<"loaded "<<s<<" entries from "<<Filename<<"\n";
      std::ofstream ofs(Filename);
      boost::archive::text_oarchive oa(ofs);
      typename container_type::size_type s=cont.size();
      for(typename container_type::iterator it=cont.begin
        oa<<static_cast<const Value&>(*it);
      std::clog<<"saved "<<s<<" entries to "<<Filename<<"\n";

    handle_type insert(const entry_type& x)
      return cont.insert(x).first;

    void erase(handle_type h)

    const entry_type& entry(handle_type h){return *h;}

    container_type cont;

  template<const char* Filename>
  struct serialized_set_factory:boost::flyweights::factory_marker
    template<typename Entry,typename Value>
    struct apply
      typedef serialized_set_factory_class<
> type;

We can use it just like this:

  extern const char filename[]="";
  typedef flyweight<
> fw_t;

Note that we're using no_tracking: if the normal refcounted
tracking were used, there would be no elements remaining in the
factory by the time it is destroyed, and hence nothing to

The attached snippet exercises these ideas. It compiles and
runs OK with GCC 3.4.4 under Cygwin. You need to link

Hope this addresses your concerns about the potential of
the lib to accomodate persistent factories.

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo

Boost list run by bdawes at, gregod at, cpdaniel at, john at