Vaucanson 1.4
smart_label.hh
00001 // smart_label.hh: this file is part of the Vaucanson project.
00002 //
00003 // Vaucanson, a generic library for finite state machines.
00004 //
00005 // Copyright (C) 2007 The Vaucanson Group.
00006 //
00007 // This program is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2
00010 // of the License, or (at your option) any later version.
00011 //
00012 // The complete GNU General Public Licence Notice can be found as the
00013 // `COPYING' file in the root directory.
00014 //
00015 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
00016 //
00017 
00018 #ifndef VCSN_AUTOMATA_CONCEPT_SMART_LABEL_HH_
00019 # define VCSN_AUTOMATA_CONCEPT_SMART_LABEL_HH_
00020 
00021 # include <boost/multi_index_container.hpp>
00022 # include <boost/multi_index/member.hpp>
00023 # include <boost/multi_index/mem_fun.hpp>
00024 # include <boost/multi_index/hashed_index.hpp>
00025 # include <boost/shared_ptr.hpp>
00026 
00027 # include <vaucanson/automata/concept/handlers.hh>
00028 # include <vaucanson/misc/hash.hh>
00029 
00030 namespace vcsn
00031 {
00032   struct label_h
00033   {};
00034 
00035   template <typename T> // T = Graph::label_t
00036   class SmartLabel
00037   {
00038     public:
00039       typedef T value_type;
00040 
00041       explicit SmartLabel(const ::boost::shared_ptr<T>& l);
00042 
00043       const T& value () const;
00044 
00045       // Handle the reference count
00046       int ref () const;
00047       int ref_dec ();
00048       int ref_inc ();
00049 
00050       operator const T& ();
00051 
00052    private:
00053       ::boost::shared_ptr<T> value_;
00054       int ref_;
00055   };
00056 
00057   template <typename T> // T = Graph::label_t
00058   class SmartLabelContainer
00059   {
00060     public:
00061       typedef T value_type;
00062 
00063     private:
00064       struct label {};
00065       typedef ::boost::multi_index_container<
00066           SmartLabel<T>,
00067           ::boost::multi_index::indexed_by<
00068             ::boost::multi_index::hashed_unique<
00069               ::boost::multi_index::tag<label>,
00070               BOOST_MULTI_INDEX_CONST_MEM_FUN (SmartLabel<T>, const T&, value),
00071               misc::hash_label<T>
00072             >
00073           >
00074         > label_container_t;
00075 
00076       label_container_t data_;
00077 
00078     public:
00079       typedef handler<label_h, const SmartLabel<T>*> hlabel_t;
00080 
00081       hlabel_t insert (const T&);
00082       void erase (const hlabel_t&);
00083       hlabel_t update (const hlabel_t&, const T&);
00084 
00085       const T& get_label (const hlabel_t&) const;
00086       hlabel_t get_hlabel (const T&) const;
00087   };
00088 
00089 } // End of namespace vcsn
00090 
00091 # if !defined VCSN_USE_INTERFACE_ONLY || defined VCSN_USE_LIB
00092 #  include <vaucanson/automata/concept/smart_label.hxx>
00093 # endif // !VCSN_USE_INTERFACE_ONLY || VCSN_USE_LIB
00094 
00095 #endif // !VCSN_AUTOMATA_CONCEPT_SMART_LABEL_HH_ //