blob: 2f7c165bc081d63a555bde778522ba4a099e2548 [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>
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080028#include <map>
29#include <string>
Alexander Afanasyev017784c2012-03-02 11:44:13 -080030#include "sync-digest.h"
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080031
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080032namespace Sync {
33
34/**
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080035 * @ingroup sync
36 * @brief Templated class for the leaf name
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080037 */
38class NameInfo
39{
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080040private:
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080041 typedef boost::weak_ptr<const NameInfo> const_weak_ptr;
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080042
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080043public:
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080044 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; }
Alexander Afanasyeva5625322012-03-06 00:03:41 -080051
52 /**
53 * @brief Check if two names are equal
54 * @param info name to check with
55 */
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080056 virtual bool
57 operator == (const NameInfo &info) const = 0;
58
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080059 /**
Alexander Afanasyeva5625322012-03-06 00:03:41 -080060 * @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 /**
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080067 * @brief Calculates digest of the name
68 */
Alexander Afanasyevcf9d65b2012-03-02 11:34:22 -080069 const Digest &
70 getDigest () const { return m_digest; }
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080071
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080072 /**
73 * @brief Convert prefix to string
74 * @returns string representation of prefix
75 */
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080076 virtual std::string
77 toString () const = 0;
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080078
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080079protected:
80 // actual stuff
81 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 -080082 Digest m_digest;
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080083
84 // static stuff
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080085 typedef std::map<std::string, const_weak_ptr> NameMap;
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080086 static size_t m_ids;
87 static NameMap m_names;
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -080088};
89
90typedef boost::shared_ptr<NameInfo> NameInfoPtr;
91typedef boost::shared_ptr<const NameInfo> NameInfoConstPtr;
92
Alexander Afanasyeva4ce9cf2012-03-06 14:29:58 -080093inline std::ostream &
94operator << (std::ostream &os, const NameInfo &info)
95{
96 os << info.toString ();
97 return os;
98}
Alexander Afanasyevb5547e32012-03-01 21:59:38 -080099
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800100} // Sync
Alexander Afanasyev7a696fb2012-03-01 17:17:22 -0800101
102#endif // SYNC_NAME_INFO_H