Vaucanson 1.4
|
00001 // smart_label.hxx: 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_HXX_ 00019 # define VCSN_AUTOMATA_CONCEPT_SMART_LABEL_HXX_ 00020 00021 namespace vcsn 00022 { 00023 00024 template <typename T> 00025 struct SmartLabel_ref_dec 00026 { 00027 SmartLabel_ref_dec (const SmartLabel<T>&) 00028 { } 00029 00030 int operator() (SmartLabel<T>& sl) 00031 { 00032 return sl.ref_dec (); 00033 } 00034 }; 00035 00036 template <typename T> 00037 struct SmartLabel_ref_inc 00038 { 00039 SmartLabel_ref_inc (const SmartLabel<T>&) 00040 { } 00041 00042 int operator() (SmartLabel<T>& sl) 00043 { 00044 return sl.ref_inc (); 00045 } 00046 }; 00047 00048 /*-----------` 00049 | SmartLabel | 00050 \-----------*/ 00051 00052 template <typename T> 00053 SmartLabel<T>::SmartLabel(const boost::shared_ptr<T>& p) 00054 : value_(p), 00055 ref_(1) 00056 { } 00057 00058 template <typename T> 00059 const T& 00060 SmartLabel<T>::value() const 00061 { 00062 return *value_; 00063 } 00064 00065 template <typename T> 00066 int 00067 SmartLabel<T>::ref() const 00068 { 00069 return ref_; 00070 } 00071 00072 template <typename T> 00073 int 00074 SmartLabel<T>::ref_dec() 00075 { 00076 return --ref_; 00077 } 00078 00079 template <typename T> 00080 int 00081 SmartLabel<T>::ref_inc() 00082 { 00083 return ++ref_; 00084 } 00085 00086 template <typename T> 00087 SmartLabel<T>::operator const T&() 00088 { 00089 return value(); 00090 } 00091 00092 00093 /*---------------------` 00094 | SmartLabelContainer | 00095 \--------------------*/ 00096 00097 template <typename T> 00098 typename SmartLabelContainer<T>::hlabel_t 00099 SmartLabelContainer<T>::insert (const T& l) 00100 { 00101 typename label_container_t::iterator i = data_.find (l); 00102 00103 if (i != data_.end ()) 00104 { 00105 data_.modify (i, SmartLabel_ref_inc<T> (*i)); 00106 return hlabel_t (&*i); 00107 } 00108 else 00109 { 00110 ::boost::shared_ptr<T> p(new T(l)); 00111 return hlabel_t (&*data_.insert (SmartLabel<T> (p)).first); 00112 } 00113 } 00114 00115 template <typename T> 00116 void 00117 SmartLabelContainer<T>::erase (const hlabel_t& h) 00118 { 00119 if (h.value()->ref () > 1) 00120 { 00121 typename label_container_t::iterator i = data_.find (h.value()->value()); 00122 data_.modify (i, SmartLabel_ref_dec<T> (*i)); 00123 } 00124 else 00125 data_.erase (h.value()->value()); 00126 } 00127 00128 template <typename T> 00129 typename SmartLabelContainer<T>::hlabel_t 00130 SmartLabelContainer<T>::update (const hlabel_t& h, const T& l) 00131 { 00132 erase (h); 00133 return insert (l); 00134 } 00135 00136 template <typename T> 00137 const T& 00138 SmartLabelContainer<T>::get_label (const hlabel_t& h) const 00139 { 00140 return h.value()->value(); 00141 } 00142 00143 template <typename T> 00144 typename SmartLabelContainer<T>::hlabel_t 00145 SmartLabelContainer<T>::get_hlabel (const T& l) const 00146 { 00147 return hlabel_t (&*data_.find (l)); 00148 } 00149 00150 } // End of namespace vcsn 00151 00152 #endif // ! VCSN_AUTOMATA_CONCEPT_SMART_LABEL_HXX_ //