Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2012 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 19 | * 卞超轶 Chaoyi Bian <bcy@pku.edu.cn> |
| 20 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 21 | */ |
| 22 | |
| 23 | #include "sync-std-name-info.h" |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 24 | #include "boost/thread/locks.hpp" |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 25 | |
| 26 | using namespace std; |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 27 | using namespace boost; |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 28 | |
| 29 | namespace Sync { |
| 30 | |
| 31 | |
| 32 | NameInfoConstPtr |
| 33 | StdNameInfo::FindOrCreate (const std::string &key) |
| 34 | { |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 35 | mutex::scoped_lock namesLock (m_namesMutex); |
| 36 | |
| 37 | // std::cout << "FindOrCreate: " << m_names.size () << "\n"; |
| 38 | |
| 39 | NameInfoConstPtr ret; |
| 40 | |
| 41 | NameMap::iterator item = m_names.find (key); |
| 42 | if (item != m_names.end ()) |
| 43 | { |
| 44 | ret = item->second.lock (); |
| 45 | BOOST_ASSERT (ret != 0); |
| 46 | } |
| 47 | else |
| 48 | { |
| 49 | ret = NameInfoPtr (new StdNameInfo (key)); |
| 50 | weak_ptr<const NameInfo> value (ret); |
| 51 | pair<NameMap::iterator,bool> inserted = |
| 52 | m_names.insert (make_pair (key, value)); |
| 53 | |
| 54 | BOOST_ASSERT (inserted.second); // previous call has to insert value |
| 55 | item = inserted.first; |
| 56 | } |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 57 | |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 58 | return ret; |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | StdNameInfo::StdNameInfo (const std::string &name) |
| 62 | : m_name (name) |
| 63 | { |
| 64 | m_id = m_ids ++; // set ID for a newly inserted element |
| 65 | m_digest << name; |
| 66 | m_digest.getHash (); // finalize digest |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 67 | |
| 68 | // std::cout << "StdNameInfo: " << name << " = " << m_id << "\n"; |
| 69 | } |
| 70 | |
| 71 | StdNameInfo::~StdNameInfo () |
| 72 | { |
| 73 | mutex::scoped_lock namesLock (m_namesMutex); |
| 74 | |
| 75 | // cout << "Destructor for " << m_name << "\n"; |
| 76 | m_names.erase (toString ()); |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | string |
| 80 | StdNameInfo::toString () const |
| 81 | { |
| 82 | return m_name; |
| 83 | } |
| 84 | |
| 85 | bool |
| 86 | StdNameInfo::operator == (const NameInfo &info) const |
| 87 | { |
Alexander Afanasyev | a562532 | 2012-03-06 00:03:41 -0800 | [diff] [blame] | 88 | return m_name == dynamic_cast<const StdNameInfo&> (info).m_name; |
| 89 | } |
| 90 | |
| 91 | bool |
| 92 | StdNameInfo::operator < (const NameInfo &info) const |
| 93 | { |
| 94 | return m_name < dynamic_cast<const StdNameInfo&> (info).m_name; |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | } // Sync |