blob: 2932b6e9281291a24c481ac2899b7275d5ae978d [file] [log] [blame]
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -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 Afanasyev7a696fb2012-03-01 17:17:22 -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 Afanasyev7a696fb2012-03-01 17:17:22 -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 Afanasyev7a696fb2012-03-01 17:17:22 -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 Afanasyev7a696fb2012-03-01 17:17:22 -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 Afanasyev7a696fb2012-03-01 17:17:22 -080022 */
23
24#ifndef SYNC_NAME_INFO_H
25#define SYNC_NAME_INFO_H
26
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080027#include <boost/shared_ptr.hpp>
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080028#include <boost/weak_ptr.hpp>
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080029#include <map>
30#include <string>
Alexander Afanasyev017784c2012-03-02 11:44:13 -080031#include "sync-digest.h"
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080032
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080033namespace Sync {
34
35/**
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080036 * @ingroup sync
37 * @brief Templated class for the leaf name
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080038 */
39class NameInfo
40{
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080041private:
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080042 typedef boost::weak_ptr<const NameInfo> const_weak_ptr;
Yingdi Yu7c64e5c2014-04-30 14:06:37 -070043
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080044public:
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080045 virtual ~NameInfo () { };
46
47 /**
48 * @brief Get ID of the record (should be locally-unique, but not really necessary---this is be used for hashing purposes)
49 */
50 size_t
51 getHashId () const { return m_id; }
Alexander Afanasyeva5625322012-03-06 00:03:41 -080052
53 /**
54 * @brief Check if two names are equal
55 * @param info name to check with
56 */
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080057 virtual bool
58 operator == (const NameInfo &info) const = 0;
59
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080060 /**
Alexander Afanasyeva5625322012-03-06 00:03:41 -080061 * @brief Check if two names are in order
62 * @param info name to check with
63 */
64 virtual bool
65 operator < (const NameInfo &info) const = 0;
66
67 /**
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080068 * @brief Calculates digest of the name
69 */
Alexander Afanasyevcf9d65b2012-03-02 11:34:22 -080070 const Digest &
71 getDigest () const { return m_digest; }
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080072
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080073 /**
74 * @brief Convert prefix to string
75 * @returns string representation of prefix
76 */
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080077 virtual std::string
78 toString () const = 0;
Yingdi Yu7c64e5c2014-04-30 14:06:37 -070079
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080080protected:
81 // actual stuff
82 size_t m_id; ///< @brief Identifies NameInfo throughout the library (for hash container, doesn't need to be strictly unique)
Alexander Afanasyevcf9d65b2012-03-02 11:34:22 -080083 Digest m_digest;
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080084
85 // static stuff
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080086 typedef std::map<std::string, const_weak_ptr> NameMap;
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080087 static size_t m_ids;
88 static NameMap m_names;
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080089};
90
91typedef boost::shared_ptr<NameInfo> NameInfoPtr;
92typedef boost::shared_ptr<const NameInfo> NameInfoConstPtr;
93
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080094inline std::ostream &
95operator << (std::ostream &os, const NameInfo &info)
96{
97 os << info.toString ();
98 return os;
99}
Alexander Afanasyevb5547e32012-03-01 21:59:38 -0800100
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800101} // Sync
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800102
103#endif // SYNC_NAME_INFO_H