blob: 72f790a5cd65f163243c0f1a2df086efcceb8b3d [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"
26
27namespace Ccnx {
28
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080029class CcnxCharbuf;
30typedef boost::shared_ptr<CcnxCharbuf> CcnxCharbufPtr;
31
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080032// 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
34class CcnxCharbuf
35{
36public:
37 CcnxCharbuf();
38 CcnxCharbuf(ccn_charbuf *buf);
Zhenkai Zhub0adefe2013-01-04 21:56:38 -080039 CcnxCharbuf(const CcnxCharbuf &other);
Alexander Afanasyev0995f322013-01-22 13:16:46 -080040 CcnxCharbuf(const void *buf, size_t length);
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080041 ~CcnxCharbuf();
42
43 // expose internal data structure, use with caution!!
44 ccn_charbuf *
45 getBuf() { return m_buf; }
Alexander Afanasyev0995f322013-01-22 13:16:46 -080046
47 const ccn_charbuf *
48 getBuf() const { return m_buf; }
49
Alexander Afanasyevd09871f2013-01-04 22:36:37 -080050 const unsigned char *
51 buf () const
52 { return m_buf->buf; }
53
54 size_t
55 length () const
56 { return m_buf->length; }
Alexander Afanasyev50547892013-01-19 22:03:45 -080057
Zhenkai Zhub0adefe2013-01-04 21:56:38 -080058private:
59 void init(ccn_charbuf *buf);
60
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080061protected:
62 ccn_charbuf *m_buf;
63};
64
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080065
66struct NameException:
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -080067 virtual boost::exception, virtual std::exception {};
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080068
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080069class Name
70{
71public:
72 Name();
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -080073 Name(const std::string &name);
74 Name(const std::vector<Bytes> &comps);
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080075 Name(const Name &other);
76 Name(const unsigned char *data, const ccn_indexbuf *comps);
Alexander Afanasyevd09871f2013-01-04 22:36:37 -080077 Name (const unsigned char *buf, const size_t length);
Alexander Afanasyev0995f322013-01-22 13:16:46 -080078 Name (const CcnxCharbuf &buf);
79 Name (const ccn_charbuf *buf);
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080080 virtual ~Name() {}
81
82 CcnxCharbufPtr
83 toCcnxCharbuf() const;
84
Alexander Afanasyevd09871f2013-01-04 22:36:37 -080085 operator CcnxCharbufPtr () const { return toCcnxCharbuf (); }
86
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080087 Name &
Alexander Afanasyev66f4c492013-01-20 23:32:50 -080088 appendComp(const Name &comp);
89
90 Name &
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080091 appendComp(const Bytes &comp);
92
93 Name &
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -080094 appendComp(const std::string &compStr);
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080095
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -080096 Name &
Alexander Afanasyev50547892013-01-19 22:03:45 -080097 appendComp(const void *buf, size_t size);
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -080098
99 /**
100 * Append int component
101 *
102 * Difference between this and appendComp call is that data is appended in network order
103 *
104 * Also, this function honors naming convention (%00 prefix is added)
105 */
106 Name &
107 appendComp(uint64_t number);
Alexander Afanasyev50547892013-01-19 22:03:45 -0800108
109 template<class T>
110 Name &
111 operator ()(const T &comp) { return appendComp (comp); }
112
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800113 Name &
114 operator ()(const void *buf, size_t size) { return appendComp (buf, size); }
115
Zhenkai Zhuf47109b2013-01-02 19:41:34 -0800116 int
117 size() const {return m_comps.size();}
118
119 Bytes
120 getComp(int index) const;
121
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800122 // return std::string format of the comp
Zhenkai Zhuf47109b2013-01-02 19:41:34 -0800123 // if all characters are printable, simply returns the string
124 // if not, print the bytes in hex string format
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800125 std::string
Zhenkai Zhuf47109b2013-01-02 19:41:34 -0800126 getCompAsString(int index) const;
127
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -0800128 uint64_t
129 getCompAsInt (int index) const;
130
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800131 inline std::string
132 getCompFromBackAsString(int index) const;
133
134 inline uint64_t
135 getCompFromBackAsInt (int index) const;
136
Zhenkai Zhuf47109b2013-01-02 19:41:34 -0800137 Name
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800138 getPartialName(int start, int n = -1) const;
Zhenkai Zhuf47109b2013-01-02 19:41:34 -0800139
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800140 std::string
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800141 toString() const;
142
143 Name &
144 operator=(const Name &other);
145
146 bool
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800147 operator==(const std::string &str) const;
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800148
149 bool
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800150 operator!=(const std::string &str) const;
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800151
152 friend Name
153 operator+(const Name &n1, const Name &n2);
Zhenkai Zhuf47109b2013-01-02 19:41:34 -0800154
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800155private:
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800156 std::vector<Bytes> m_comps;
Zhenkai Zhuf47109b2013-01-02 19:41:34 -0800157};
158
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800159std::ostream&
160operator <<(std::ostream &os, const Name &name);
Zhenkai Zhuf47109b2013-01-02 19:41:34 -0800161
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800162bool
163operator ==(const Name &n1, const Name &n2);
164
165bool
166operator !=(const Name &n1, const Name &n2);
167
168bool
169operator <(const Name &n1, const Name &n2);
170
171
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -0800172std::string
173Name::getCompFromBackAsString(int index) const
174{
175 return getCompAsString (m_comps.size () - 1 - index);
176}
177
178uint64_t
179Name::getCompFromBackAsInt (int index) const
180{
181 return getCompAsInt (m_comps.size () - 1 - index);
182}
183
Zhenkai Zhuf47109b2013-01-02 19:41:34 -0800184
185} // Ccnx
186#endif