blob: bc4e80fba45f887fd578ea09e18cccf7f2eefa56 [file] [log] [blame]
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Alexander Afanasyev8722d872014-07-02 13:00:29 -07003 * Copyright (c) 2012-2014 University of California, Los Angeles
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -08004 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -07005 * This file is part of ChronoSync, synchronization library for distributed realtime
6 * applications for NDN.
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -08007 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -07008 * 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 Afanasyev2fc2d672012-03-05 16:57:39 -080011 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -070012 * 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 Afanasyev2fc2d672012-03-05 16:57:39 -080015 *
Alexander Afanasyev8722d872014-07-02 13:00:29 -070016 * 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 Afanasyev2fc2d672012-03-05 16:57:39 -080022 */
23
24#include "sync-std-name-info.h"
25
Alexander Afanasyevd95c2312013-11-07 13:45:34 -080026// using 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 // std::cout << "FindOrCreate: " << m_names.size () << "\n";
Yingdi Yu7c64e5c2014-04-30 14:06:37 -070036
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080037 NameInfoConstPtr ret;
Yingdi Yu7c64e5c2014-04-30 14:06:37 -070038
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080039 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 Afanasyevd95c2312013-11-07 13:45:34 -080049 std::pair<NameMap::iterator,bool> inserted =
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080050 m_names.insert (make_pair (key, value));
Yingdi Yu7c64e5c2014-04-30 14:06:37 -070051
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080052 BOOST_ASSERT (inserted.second); // previous call has to insert value
53 item = inserted.first;
54 }
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -080055
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080056 return ret;
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -080057}
58
59StdNameInfo::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 Afanasyeva5858032012-03-09 15:55:10 -080064 m_digest.finalize ();
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080065
66 // std::cout << "StdNameInfo: " << name << " = " << m_id << "\n";
67}
68
69StdNameInfo::~StdNameInfo ()
70{
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080071 // cout << "Destructor for " << m_name << "\n";
72 m_names.erase (toString ());
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -080073}
74
Alexander Afanasyevd95c2312013-11-07 13:45:34 -080075std::string
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -080076StdNameInfo::toString () const
77{
78 return m_name;
79}
80
81bool
82StdNameInfo::operator == (const NameInfo &info) const
83{
Alexander Afanasyeva5625322012-03-06 00:03:41 -080084 return m_name == dynamic_cast<const StdNameInfo&> (info).m_name;
85}
86
87bool
88StdNameInfo::operator < (const NameInfo &info) const
89{
90 return m_name < dynamic_cast<const StdNameInfo&> (info).m_name;
Alexander Afanasyev2fc2d672012-03-05 16:57:39 -080091}
92
93} // Sync