Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [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> |
Chaoyi Bian | 3e1eb16 | 2012-04-03 16:59:32 -0700 | [diff] [blame^] | 19 | * Chaoyi Bian <bcy@pku.edu.cn> |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 20 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 21 | */ |
| 22 | |
| 23 | #ifndef SYNC_NAME_INFO_H |
| 24 | #define SYNC_NAME_INFO_H |
| 25 | |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 26 | #include <boost/shared_ptr.hpp> |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 27 | #include <boost/weak_ptr.hpp> |
| 28 | #include <boost/thread/mutex.hpp> |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 29 | #include <map> |
| 30 | #include <string> |
Alexander Afanasyev | 017784c | 2012-03-02 11:44:13 -0800 | [diff] [blame] | 31 | #include "sync-digest.h" |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 32 | |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 33 | namespace Sync { |
| 34 | |
| 35 | /** |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 36 | * @ingroup sync |
| 37 | * @brief Templated class for the leaf name |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 38 | */ |
| 39 | class NameInfo |
| 40 | { |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 41 | private: |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 42 | typedef boost::weak_ptr<const NameInfo> const_weak_ptr; |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 43 | |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 44 | public: |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 45 | 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 Afanasyev | a562532 | 2012-03-06 00:03:41 -0800 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * @brief Check if two names are equal |
| 55 | * @param info name to check with |
| 56 | */ |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 57 | virtual bool |
| 58 | operator == (const NameInfo &info) const = 0; |
| 59 | |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 60 | /** |
Alexander Afanasyev | a562532 | 2012-03-06 00:03:41 -0800 | [diff] [blame] | 61 | * @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 Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 68 | * @brief Calculates digest of the name |
| 69 | */ |
Alexander Afanasyev | cf9d65b | 2012-03-02 11:34:22 -0800 | [diff] [blame] | 70 | const Digest & |
| 71 | getDigest () const { return m_digest; } |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 72 | |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 73 | /** |
| 74 | * @brief Convert prefix to string |
| 75 | * @returns string representation of prefix |
| 76 | */ |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 77 | virtual std::string |
| 78 | toString () const = 0; |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 79 | |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 80 | protected: |
| 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 Afanasyev | cf9d65b | 2012-03-02 11:34:22 -0800 | [diff] [blame] | 83 | Digest m_digest; |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 84 | |
| 85 | // static stuff |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 86 | typedef std::map<std::string, const_weak_ptr> NameMap; |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 87 | static size_t m_ids; |
| 88 | static NameMap m_names; |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 89 | static boost::mutex m_namesMutex; |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | typedef boost::shared_ptr<NameInfo> NameInfoPtr; |
| 93 | typedef boost::shared_ptr<const NameInfo> NameInfoConstPtr; |
| 94 | |
Alexander Afanasyev | a4ce9cf | 2012-03-06 14:29:58 -0800 | [diff] [blame] | 95 | inline std::ostream & |
| 96 | operator << (std::ostream &os, const NameInfo &info) |
| 97 | { |
| 98 | os << info.toString (); |
| 99 | return os; |
| 100 | } |
Alexander Afanasyev | b5547e3 | 2012-03-01 21:59:38 -0800 | [diff] [blame] | 101 | |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 102 | } // Sync |
Alexander Afanasyev | 7a696fb | 2012-03-01 17:17:22 -0800 | [diff] [blame] | 103 | |
| 104 | #endif // SYNC_NAME_INFO_H |