blob: 02e433647c22acd9fe3c04bd560f2336e68a5c3d [file] [log] [blame]
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -08001/* -*- 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 Afanasyeva4ce9cf2012-03-06 14:29:58 -080024#include "boost/thread/locks.hpp"
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -080025
26using namespace std;
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080027using namespace boost;
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -080028
29namespace Sync {
30
31
32NameInfoConstPtr
33StdNameInfo::FindOrCreate (const std::string &key)
34{
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080035 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 Afanasyev2fc2d672012-03-05 16:57:39 -080057
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080058 return ret;
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -080059}
60
61StdNameInfo::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;
Alexander Afanasyeva5858032012-03-09 15:55:10 -080066 m_digest.finalize ();
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080067
68 // std::cout << "StdNameInfo: " << name << " = " << m_id << "\n";
69}
70
71StdNameInfo::~StdNameInfo ()
72{
73 mutex::scoped_lock namesLock (m_namesMutex);
74
75 // cout << "Destructor for " << m_name << "\n";
76 m_names.erase (toString ());
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -080077}
78
79string
80StdNameInfo::toString () const
81{
82 return m_name;
83}
84
85bool
86StdNameInfo::operator == (const NameInfo &info) const
87{
Alexander Afanasyeva5625322012-03-06 00:03:41 -080088 return m_name == dynamic_cast<const StdNameInfo&> (info).m_name;
89}
90
91bool
92StdNameInfo::operator < (const NameInfo &info) const
93{
94 return m_name < dynamic_cast<const StdNameInfo&> (info).m_name;
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -080095}
96
97} // Sync