blob: 0fa0b86d84b9cceb749a30e9933a9c85f7b62a9a [file] [log] [blame]
akmhoque66e66182014-02-21 17:56:03 -06001/* -*- 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>
Vince Lehman0a7da612014-10-29 14:39:29 -050020 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
akmhoque66e66182014-02-21 17:56:03 -060021 */
22
23#ifndef SYNC_NAME_INFO_H
24#define SYNC_NAME_INFO_H
25
Vince Lehman0a7da612014-10-29 14:39:29 -050026#include "sync-common.h"
27
akmhoque66e66182014-02-21 17:56:03 -060028#include <map>
29#include <string>
30#include "sync-digest.h"
31
32namespace Sync {
33
34/**
35 * @ingroup sync
36 * @brief Templated class for the leaf name
37 */
38class NameInfo
39{
40private:
Vince Lehman0a7da612014-10-29 14:39:29 -050041 typedef weak_ptr<const NameInfo> const_weak_ptr;
42
akmhoque66e66182014-02-21 17:56:03 -060043public:
44 virtual ~NameInfo () { };
45
46 /**
47 * @brief Get ID of the record (should be locally-unique, but not really necessary---this is be used for hashing purposes)
48 */
49 size_t
50 getHashId () const { return m_id; }
51
52 /**
53 * @brief Check if two names are equal
54 * @param info name to check with
55 */
56 virtual bool
57 operator == (const NameInfo &info) const = 0;
58
59 /**
60 * @brief Check if two names are in order
61 * @param info name to check with
62 */
63 virtual bool
64 operator < (const NameInfo &info) const = 0;
65
66 /**
67 * @brief Calculates digest of the name
68 */
69 const Digest &
70 getDigest () const { return m_digest; }
71
72 /**
73 * @brief Convert prefix to string
74 * @returns string representation of prefix
75 */
76 virtual std::string
77 toString () const = 0;
Vince Lehman0a7da612014-10-29 14:39:29 -050078
akmhoque66e66182014-02-21 17:56:03 -060079protected:
80 // actual stuff
81 size_t m_id; ///< @brief Identifies NameInfo throughout the library (for hash container, doesn't need to be strictly unique)
82 Digest m_digest;
83
84 // static stuff
85 typedef std::map<std::string, const_weak_ptr> NameMap;
86 static size_t m_ids;
87 static NameMap m_names;
88};
89
Vince Lehman0a7da612014-10-29 14:39:29 -050090typedef shared_ptr<NameInfo> NameInfoPtr;
91typedef shared_ptr<const NameInfo> NameInfoConstPtr;
akmhoque66e66182014-02-21 17:56:03 -060092
93inline std::ostream &
94operator << (std::ostream &os, const NameInfo &info)
95{
96 os << info.toString ();
97 return os;
98}
99
100} // Sync
101
102#endif // SYNC_NAME_INFO_H