akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 1 | /* -*- 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 Lehman | 0a7da61 | 2014-10-29 14:39:29 -0500 | [diff] [blame^] | 20 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | #ifndef SYNC_NAME_INFO_H |
| 24 | #define SYNC_NAME_INFO_H |
| 25 | |
Vince Lehman | 0a7da61 | 2014-10-29 14:39:29 -0500 | [diff] [blame^] | 26 | #include "sync-common.h" |
| 27 | |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 28 | #include <map> |
| 29 | #include <string> |
| 30 | #include "sync-digest.h" |
| 31 | |
| 32 | namespace Sync { |
| 33 | |
| 34 | /** |
| 35 | * @ingroup sync |
| 36 | * @brief Templated class for the leaf name |
| 37 | */ |
| 38 | class NameInfo |
| 39 | { |
| 40 | private: |
Vince Lehman | 0a7da61 | 2014-10-29 14:39:29 -0500 | [diff] [blame^] | 41 | typedef weak_ptr<const NameInfo> const_weak_ptr; |
| 42 | |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 43 | public: |
| 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 Lehman | 0a7da61 | 2014-10-29 14:39:29 -0500 | [diff] [blame^] | 78 | |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 79 | protected: |
| 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 Lehman | 0a7da61 | 2014-10-29 14:39:29 -0500 | [diff] [blame^] | 90 | typedef shared_ptr<NameInfo> NameInfoPtr; |
| 91 | typedef shared_ptr<const NameInfo> NameInfoConstPtr; |
akmhoque | 66e6618 | 2014-02-21 17:56:03 -0600 | [diff] [blame] | 92 | |
| 93 | inline std::ostream & |
| 94 | operator << (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 |