blob: 26bca6795a252bfcb1fc93959b59a1095ad26fd7 [file] [log] [blame]
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -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>
Chaoyi Bian3e1eb162012-04-03 16:59:32 -070019 * Chaoyi Bian <bcy@pku.edu.cn>
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080020 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
21 */
22
23#ifndef SYNC_NAME_INFO_H
24#define SYNC_NAME_INFO_H
25
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080026#include <boost/shared_ptr.hpp>
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080027#include <boost/weak_ptr.hpp>
28#include <boost/thread/mutex.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;
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080043
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;
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080079
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 Afanasyeva4ce9cf2012-03-06 14:29:58 -080089 static boost::mutex m_namesMutex;
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080090};
91
92typedef boost::shared_ptr<NameInfo> NameInfoPtr;
93typedef boost::shared_ptr<const NameInfo> NameInfoConstPtr;
94
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080095inline std::ostream &
96operator << (std::ostream &os, const NameInfo &info)
97{
98 os << info.toString ();
99 return os;
100}
Alexander Afanasyevb5547e32012-03-01 21:59:38 -0800101
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800102} // Sync
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800103
104#endif // SYNC_NAME_INFO_H