Alexander Afanasyev | d09871f | 2013-01-04 22:36:37 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2012-2013 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 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 20 | */ |
| 21 | |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 22 | #ifndef CCNX_NAME_H |
| 23 | #define CCNX_NAME_H |
| 24 | #include <boost/shared_ptr.hpp> |
| 25 | #include "ccnx-common.h" |
| 26 | |
| 27 | namespace Ccnx { |
| 28 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 29 | class CcnxCharbuf; |
| 30 | typedef boost::shared_ptr<CcnxCharbuf> CcnxCharbufPtr; |
| 31 | |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 32 | // This class is mostly used in CcnxWrapper; users should not be directly using this class |
| 33 | // The main purpose of this class to is avoid manually create and destroy charbuf everytime |
| 34 | class CcnxCharbuf |
| 35 | { |
| 36 | public: |
| 37 | CcnxCharbuf(); |
| 38 | CcnxCharbuf(ccn_charbuf *buf); |
Zhenkai Zhu | b0adefe | 2013-01-04 21:56:38 -0800 | [diff] [blame] | 39 | CcnxCharbuf(const CcnxCharbuf &other); |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 40 | ~CcnxCharbuf(); |
| 41 | |
| 42 | // expose internal data structure, use with caution!! |
| 43 | ccn_charbuf * |
| 44 | getBuf() { return m_buf; } |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 45 | static CcnxCharbufPtr Null; |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 46 | |
Alexander Afanasyev | d09871f | 2013-01-04 22:36:37 -0800 | [diff] [blame] | 47 | const unsigned char * |
| 48 | buf () const |
| 49 | { return m_buf->buf; } |
| 50 | |
| 51 | size_t |
| 52 | length () const |
| 53 | { return m_buf->length; } |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 54 | |
Zhenkai Zhu | b0adefe | 2013-01-04 21:56:38 -0800 | [diff] [blame] | 55 | private: |
| 56 | void init(ccn_charbuf *buf); |
| 57 | |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 58 | protected: |
| 59 | ccn_charbuf *m_buf; |
| 60 | }; |
| 61 | |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 62 | |
| 63 | struct NameException: |
| 64 | virtual boost::exception, virtual exception {}; |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 65 | |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 66 | class Name |
| 67 | { |
| 68 | public: |
| 69 | Name(); |
| 70 | Name(const string &name); |
| 71 | Name(const vector<Bytes> &comps); |
| 72 | Name(const Name &other); |
| 73 | Name(const unsigned char *data, const ccn_indexbuf *comps); |
Alexander Afanasyev | d09871f | 2013-01-04 22:36:37 -0800 | [diff] [blame] | 74 | Name (const unsigned char *buf, const size_t length); |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 75 | virtual ~Name() {} |
| 76 | |
| 77 | CcnxCharbufPtr |
| 78 | toCcnxCharbuf() const; |
| 79 | |
Alexander Afanasyev | d09871f | 2013-01-04 22:36:37 -0800 | [diff] [blame] | 80 | operator CcnxCharbufPtr () const { return toCcnxCharbuf (); } |
| 81 | |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 82 | Name & |
Alexander Afanasyev | 66f4c49 | 2013-01-20 23:32:50 -0800 | [diff] [blame] | 83 | appendComp(const Name &comp); |
| 84 | |
| 85 | Name & |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 86 | appendComp(const Bytes &comp); |
| 87 | |
| 88 | Name & |
| 89 | appendComp(const string &compStr); |
| 90 | |
Alexander Afanasyev | c9eb68f | 2013-01-07 13:40:00 -0800 | [diff] [blame] | 91 | Name & |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 92 | appendComp(const void *buf, size_t size); |
Alexander Afanasyev | c9eb68f | 2013-01-07 13:40:00 -0800 | [diff] [blame] | 93 | |
| 94 | /** |
| 95 | * Append int component |
| 96 | * |
| 97 | * Difference between this and appendComp call is that data is appended in network order |
| 98 | * |
| 99 | * Also, this function honors naming convention (%00 prefix is added) |
| 100 | */ |
| 101 | Name & |
| 102 | appendComp(uint64_t number); |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 103 | |
| 104 | template<class T> |
| 105 | Name & |
| 106 | operator ()(const T &comp) { return appendComp (comp); } |
| 107 | |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 108 | Name & |
| 109 | operator ()(const void *buf, size_t size) { return appendComp (buf, size); } |
| 110 | |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 111 | int |
| 112 | size() const {return m_comps.size();} |
| 113 | |
| 114 | Bytes |
| 115 | getComp(int index) const; |
| 116 | |
| 117 | // return string format of the comp |
| 118 | // if all characters are printable, simply returns the string |
| 119 | // if not, print the bytes in hex string format |
| 120 | string |
| 121 | getCompAsString(int index) const; |
| 122 | |
Alexander Afanasyev | c9eb68f | 2013-01-07 13:40:00 -0800 | [diff] [blame] | 123 | uint64_t |
| 124 | getCompAsInt (int index) const; |
| 125 | |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 126 | Name |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 127 | getPartialName(int start, int n = -1) const; |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 128 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 129 | string |
| 130 | toString() const; |
| 131 | |
| 132 | Name & |
| 133 | operator=(const Name &other); |
| 134 | |
| 135 | bool |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 136 | operator==(const string &str) const; |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 137 | |
| 138 | bool |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 139 | operator!=(const string &str) const; |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 140 | |
| 141 | friend Name |
| 142 | operator+(const Name &n1, const Name &n2); |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 143 | |
| 144 | protected: |
| 145 | vector<Bytes> m_comps; |
| 146 | }; |
| 147 | |
| 148 | ostream& |
| 149 | operator <<(ostream &os, const Name &name); |
| 150 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 151 | bool |
| 152 | operator ==(const Name &n1, const Name &n2); |
| 153 | |
| 154 | bool |
| 155 | operator !=(const Name &n1, const Name &n2); |
| 156 | |
| 157 | bool |
| 158 | operator <(const Name &n1, const Name &n2); |
| 159 | |
| 160 | |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 161 | |
| 162 | } // Ccnx |
| 163 | #endif |