blob: 8e3ca6e421db3db6bf20c2984c1908c88584a722 [file] [log] [blame]
Alexander Afanasyevd09871f2013-01-04 22:36:37 -08001/* -*- 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 Zhuf47109b2013-01-02 19:41:34 -080022#ifndef CCNX_NAME_H
23#define CCNX_NAME_H
24#include <boost/shared_ptr.hpp>
25#include "ccnx-common.h"
Alexander Afanasyev452645b2013-02-24 15:41:29 -080026#include "ccnx-charbuf.h"
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080027
28namespace Ccnx {
29
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080030struct NameException:
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -080031 virtual boost::exception, virtual std::exception {};
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080032
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080033class Name
34{
35public:
36 Name();
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -080037 Name(const std::string &name);
38 Name(const std::vector<Bytes> &comps);
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080039 Name(const Name &other);
40 Name(const unsigned char *data, const ccn_indexbuf *comps);
Alexander Afanasyeveb575e02013-01-26 17:14:51 -080041 Name (const void *buf, const size_t length);
Alexander Afanasyev0995f322013-01-22 13:16:46 -080042 Name (const CcnxCharbuf &buf);
43 Name (const ccn_charbuf *buf);
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080044 virtual ~Name() {}
45
46 CcnxCharbufPtr
47 toCcnxCharbuf() const;
48
Alexander Afanasyev452645b2013-02-24 15:41:29 -080049 CcnxCharbuf*
50 toCcnxCharbufRaw () const;
51
Alexander Afanasyevd09871f2013-01-04 22:36:37 -080052 operator CcnxCharbufPtr () const { return toCcnxCharbuf (); }
53
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080054 Name &
Alexander Afanasyev66f4c492013-01-20 23:32:50 -080055 appendComp(const Name &comp);
56
57 Name &
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080058 appendComp(const Bytes &comp);
59
60 Name &
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -080061 appendComp(const std::string &compStr);
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080062
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -080063 Name &
Alexander Afanasyev50547892013-01-19 22:03:45 -080064 appendComp(const void *buf, size_t size);
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -080065
66 /**
67 * Append int component
68 *
69 * Difference between this and appendComp call is that data is appended in network order
70 *
71 * Also, this function honors naming convention (%00 prefix is added)
72 */
73 Name &
74 appendComp(uint64_t number);
Alexander Afanasyev50547892013-01-19 22:03:45 -080075
76 template<class T>
77 Name &
78 operator ()(const T &comp) { return appendComp (comp); }
79
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080080 Name &
81 operator ()(const void *buf, size_t size) { return appendComp (buf, size); }
82
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080083 int
84 size() const {return m_comps.size();}
85
Alexander Afanasyevf9978f82013-01-23 16:30:31 -080086 const Bytes &
87 getComp (int index) const;
88
89 inline const Bytes &
90 getCompFromBack (int index) const;
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080091
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -080092 // return std::string format of the comp
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080093 // if all characters are printable, simply returns the string
94 // if not, print the bytes in hex string format
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -080095 std::string
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080096 getCompAsString(int index) const;
97
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -080098 uint64_t
99 getCompAsInt (int index) const;
100
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800101 inline std::string
102 getCompFromBackAsString(int index) const;
103
104 inline uint64_t
105 getCompFromBackAsInt (int index) const;
106
Zhenkai Zhuf47109b2013-01-02 19:41:34 -0800107 Name
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800108 getPartialName(int start, int n = -1) const;
Zhenkai Zhuf47109b2013-01-02 19:41:34 -0800109
Alexander Afanasyev08aa70a2013-01-22 22:16:25 -0800110 inline Name
111 getPartialNameFromBack(int start, int n = -1) const;
112
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800113 std::string
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800114 toString() const;
115
116 Name &
117 operator=(const Name &other);
118
119 bool
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800120 operator==(const std::string &str) const;
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800121
122 bool
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800123 operator!=(const std::string &str) const;
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800124
125 friend Name
126 operator+(const Name &n1, const Name &n2);
Zhenkai Zhuf47109b2013-01-02 19:41:34 -0800127
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800128private:
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800129 std::vector<Bytes> m_comps;
Zhenkai Zhuf47109b2013-01-02 19:41:34 -0800130};
131
Alexander Afanasyev923f2f12013-02-23 16:36:31 -0800132typedef boost::shared_ptr<Name> NamePtr;
133
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800134std::ostream&
135operator <<(std::ostream &os, const Name &name);
Zhenkai Zhuf47109b2013-01-02 19:41:34 -0800136
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800137bool
138operator ==(const Name &n1, const Name &n2);
139
140bool
141operator !=(const Name &n1, const Name &n2);
142
143bool
144operator <(const Name &n1, const Name &n2);
145
146
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800147std::string
148Name::getCompFromBackAsString(int index) const
149{
150 return getCompAsString (m_comps.size () - 1 - index);
151}
152
153uint64_t
154Name::getCompFromBackAsInt (int index) const
155{
156 return getCompAsInt (m_comps.size () - 1 - index);
157}
158
Alexander Afanasyev08aa70a2013-01-22 22:16:25 -0800159Name
160Name::getPartialNameFromBack(int start, int n/* = -1*/) const
161{
162 return getPartialName (m_comps.size () - 1 - start, n);
163}
164
Alexander Afanasyevf9978f82013-01-23 16:30:31 -0800165const Bytes &
166Name::getCompFromBack (int index) const
167{
168 return getComp (m_comps.size () - 1 - index);
169}
170
Zhenkai Zhuf47109b2013-01-02 19:41:34 -0800171
172} // Ccnx
173#endif