Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 3 | * Copyright (c) 2012-2014 University of California, Los Angeles |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 5 | * This file is part of ChronoSync, synchronization library for distributed realtime |
| 6 | * applications for NDN. |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 7 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 8 | * ChronoSync is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 10 | * version 3 of the License, or (at your option) any later version. |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 11 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 12 | * ChronoSync is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 15 | * |
Alexander Afanasyev | 8722d87 | 2014-07-02 13:00:29 -0700 | [diff] [blame] | 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ChronoSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * @author Zhenkai Zhu <http://irl.cs.ucla.edu/~zhenkai/> |
| 20 | * @author Chaoyi Bian <bcy@pku.edu.cn> |
| 21 | * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html> |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | #include "sync-std-name-info.h" |
| 25 | |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 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 | // std::cout << "FindOrCreate: " << m_names.size () << "\n"; |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 36 | |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 37 | NameInfoConstPtr ret; |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 38 | |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 39 | NameMap::iterator item = m_names.find (key); |
| 40 | if (item != m_names.end ()) |
| 41 | { |
| 42 | ret = item->second.lock (); |
| 43 | BOOST_ASSERT (ret != 0); |
| 44 | } |
| 45 | else |
| 46 | { |
| 47 | ret = NameInfoPtr (new StdNameInfo (key)); |
| 48 | weak_ptr<const NameInfo> value (ret); |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 49 | std::pair<NameMap::iterator,bool> inserted = |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 50 | m_names.insert (make_pair (key, value)); |
Yingdi Yu | 7c64e5c | 2014-04-30 14:06:37 -0700 | [diff] [blame] | 51 | |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 52 | BOOST_ASSERT (inserted.second); // previous call has to insert value |
| 53 | item = inserted.first; |
| 54 | } |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 55 | |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 56 | return ret; |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | StdNameInfo::StdNameInfo (const std::string &name) |
| 60 | : m_name (name) |
| 61 | { |
| 62 | m_id = m_ids ++; // set ID for a newly inserted element |
| 63 | m_digest << name; |
Alexander Afanasyev | a585803 | 2012-03-09 15:55:10 -0800 | [diff] [blame] | 64 | m_digest.finalize (); |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 65 | |
| 66 | // std::cout << "StdNameInfo: " << name << " = " << m_id << "\n"; |
| 67 | } |
| 68 | |
| 69 | StdNameInfo::~StdNameInfo () |
| 70 | { |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 71 | // cout << "Destructor for " << m_name << "\n"; |
| 72 | m_names.erase (toString ()); |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 73 | } |
| 74 | |
Alexander Afanasyev | d95c231 | 2013-11-07 13:45:34 -0800 | [diff] [blame] | 75 | std::string |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 76 | StdNameInfo::toString () const |
| 77 | { |
| 78 | return m_name; |
| 79 | } |
| 80 | |
| 81 | bool |
| 82 | StdNameInfo::operator == (const NameInfo &info) const |
| 83 | { |
Alexander Afanasyev | a562532 | 2012-03-06 00:03:41 -0800 | [diff] [blame] | 84 | return m_name == dynamic_cast<const StdNameInfo&> (info).m_name; |
| 85 | } |
| 86 | |
| 87 | bool |
| 88 | StdNameInfo::operator < (const NameInfo &info) const |
| 89 | { |
| 90 | return m_name < dynamic_cast<const StdNameInfo&> (info).m_name; |
Alexander Afanasyev | 2fc2d67 | 2012-03-05 16:57:39 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | } // Sync |