blob: ff93a7d172a27c984bd5150fa020ff1acf63f1c5 [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
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080050 static CcnxCharbufPtr Null;
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080051
Alexander Afanasyevd09871f2013-01-04 22:36:37 -080052 const unsigned char *
53 buf () const
54 { return m_buf->buf; }
55
56 size_t
57 length () const
58 { return m_buf->length; }
Alexander Afanasyev50547892013-01-19 22:03:45 -080059
Zhenkai Zhub0adefe2013-01-04 21:56:38 -080060private:
61 void init(ccn_charbuf *buf);
62
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080063protected:
64 ccn_charbuf *m_buf;
65};
66
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080067
68struct NameException:
69 virtual boost::exception, virtual exception {};
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080070
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080071class Name
72{
73public:
74 Name();
75 Name(const string &name);
76 Name(const vector<Bytes> &comps);
77 Name(const Name &other);
78 Name(const unsigned char *data, const ccn_indexbuf *comps);
Alexander Afanasyevd09871f2013-01-04 22:36:37 -080079 Name (const unsigned char *buf, const size_t length);
Alexander Afanasyev0995f322013-01-22 13:16:46 -080080 Name (const CcnxCharbuf &buf);
81 Name (const ccn_charbuf *buf);
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080082 virtual ~Name() {}
83
84 CcnxCharbufPtr
85 toCcnxCharbuf() const;
86
Alexander Afanasyevd09871f2013-01-04 22:36:37 -080087 operator CcnxCharbufPtr () const { return toCcnxCharbuf (); }
88
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080089 Name &
Alexander Afanasyev66f4c492013-01-20 23:32:50 -080090 appendComp(const Name &comp);
91
92 Name &
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080093 appendComp(const Bytes &comp);
94
95 Name &
96 appendComp(const string &compStr);
97
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -080098 Name &
Alexander Afanasyev50547892013-01-19 22:03:45 -080099 appendComp(const void *buf, size_t size);
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -0800100
101 /**
102 * Append int component
103 *
104 * Difference between this and appendComp call is that data is appended in network order
105 *
106 * Also, this function honors naming convention (%00 prefix is added)
107 */
108 Name &
109 appendComp(uint64_t number);
Alexander Afanasyev50547892013-01-19 22:03:45 -0800110
111 template<class T>
112 Name &
113 operator ()(const T &comp) { return appendComp (comp); }
114
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800115 Name &
116 operator ()(const void *buf, size_t size) { return appendComp (buf, size); }
117
Zhenkai Zhuf47109b2013-01-02 19:41:34 -0800118 int
119 size() const {return m_comps.size();}
120
121 Bytes
122 getComp(int index) const;
123
124 // return string format of the comp
125 // if all characters are printable, simply returns the string
126 // if not, print the bytes in hex string format
127 string
128 getCompAsString(int index) const;
129
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -0800130 uint64_t
131 getCompAsInt (int index) const;
132
Zhenkai Zhuf47109b2013-01-02 19:41:34 -0800133 Name
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800134 getPartialName(int start, int n = -1) const;
Zhenkai Zhuf47109b2013-01-02 19:41:34 -0800135
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800136 string
137 toString() const;
138
139 Name &
140 operator=(const Name &other);
141
142 bool
Zhenkai Zhu3b82d432013-01-03 22:48:40 -0800143 operator==(const string &str) const;
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800144
145 bool
Zhenkai Zhu3b82d432013-01-03 22:48:40 -0800146 operator!=(const string &str) const;
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800147
148 friend Name
149 operator+(const Name &n1, const Name &n2);
Zhenkai Zhuf47109b2013-01-02 19:41:34 -0800150
Alexander Afanasyev0995f322013-01-22 13:16:46 -0800151private:
Zhenkai Zhuf47109b2013-01-02 19:41:34 -0800152 vector<Bytes> m_comps;
153};
154
155ostream&
156operator <<(ostream &os, const Name &name);
157
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800158bool
159operator ==(const Name &n1, const Name &n2);
160
161bool
162operator !=(const Name &n1, const Name &n2);
163
164bool
165operator <(const Name &n1, const Name &n2);
166
167
Zhenkai Zhuf47109b2013-01-02 19:41:34 -0800168
169} // Ccnx
170#endif