Vaucanson  1.4.1
smart_label.hh
1 // smart_label.hh: this file is part of the Vaucanson project.
2 //
3 // Vaucanson, a generic library for finite state machines.
4 //
5 // Copyright (C) 2007 The Vaucanson Group.
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
11 //
12 // The complete GNU General Public Licence Notice can be found as the
13 // `COPYING' file in the root directory.
14 //
15 // The Vaucanson Group consists of people listed in the `AUTHORS' file.
16 //
17 
18 #ifndef VCSN_AUTOMATA_CONCEPT_SMART_LABEL_HH_
19 # define VCSN_AUTOMATA_CONCEPT_SMART_LABEL_HH_
20 
21 # include <boost/multi_index_container.hpp>
22 # include <boost/multi_index/member.hpp>
23 # include <boost/multi_index/mem_fun.hpp>
24 # include <boost/multi_index/hashed_index.hpp>
25 # include <boost/shared_ptr.hpp>
26 
27 # include <vaucanson/automata/concept/handlers.hh>
28 # include <vaucanson/misc/hash.hh>
29 
30 namespace vcsn
31 {
32  struct label_h
33  {};
34 
35  template <typename T> // T = Graph::label_t
36  class SmartLabel
37  {
38  public:
39  typedef T value_type;
40 
41  explicit SmartLabel(const ::boost::shared_ptr<T>& l);
42 
43  const T& value () const;
44 
45  // Handle the reference count
46  int ref () const;
47  int ref_dec ();
48  int ref_inc ();
49 
50  operator const T& ();
51 
52  private:
53  ::boost::shared_ptr<T> value_;
54  int ref_;
55  };
56 
57  template <typename T> // T = Graph::label_t
58  class SmartLabelContainer
59  {
60  public:
61  typedef T value_type;
62 
63  private:
64  struct label {};
65  typedef ::boost::multi_index_container<
66  SmartLabel<T>,
67  ::boost::multi_index::indexed_by<
68  ::boost::multi_index::hashed_unique<
69  ::boost::multi_index::tag<label>,
70  BOOST_MULTI_INDEX_CONST_MEM_FUN (SmartLabel<T>, const T&, value),
71  misc::hash_label<T>
72  >
73  >
74  > label_container_t;
75 
76  label_container_t data_;
77 
78  public:
79  typedef handler<label_h, const SmartLabel<T>*> hlabel_t;
80 
81  hlabel_t insert (const T&);
82  void erase (const hlabel_t&);
83  hlabel_t update (const hlabel_t&, const T&);
84 
85  const T& get_label (const hlabel_t&) const;
86  hlabel_t get_hlabel (const T&) const;
87  };
88 
89 } // End of namespace vcsn
90 
91 # if !defined VCSN_USE_INTERFACE_ONLY || defined VCSN_USE_LIB
92 # include <vaucanson/automata/concept/smart_label.hxx>
93 # endif // !VCSN_USE_INTERFACE_ONLY || VCSN_USE_LIB
94 
95 #endif // !VCSN_AUTOMATA_CONCEPT_SMART_LABEL_HH_ //