blob: 7f629dc5837d0437483b7ded5a4f3c29896c5d3e [file] [log] [blame]
Ilya Moiseenkod26e6822011-08-23 17:48:38 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2011 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: Ilya Moiseenko <iliamo@cs.ucla.edu>
19 */
20
21#ifndef _NDN_NAME_COMPONENTS_H_
22#define _NDN_NAME_COMPONENTS_H_
23
24#include "ns3/simple-ref-count.h"
25#include "ns3/attribute.h"
26#include "ns3/attribute-helper.h"
27
28#include <string>
29#include <algorithm>
30#include <list>
31#include "ns3/object.h"
32
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070033#include <boost/ref.hpp>
34
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070035namespace ns3 {
36
Ilya Moiseenko332add02011-12-24 17:21:25 -080037/**
38 * \ingroup ccnx
39 * \brief Hierarchical CCNX name
40 * A Name element represents a hierarchical name for CCNx content.
41 * It simply contains a sequence of Component elements.
42 * Each Component element contains a sequence of zero or more bytes.
43 * There are no restrictions on what byte sequences may be used.
44 * The Name element in an Interest is often referred to with the term name prefix or simply prefix.
45 */
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070046class CcnxNameComponents : public SimpleRefCount<CcnxNameComponents>
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070047{
48public:
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070049 /**
Ilya Moiseenko332add02011-12-24 17:21:25 -080050 * \brief Constructor
51 * Creates a prefix with zero components (can be looked as root "/")
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070052 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070053 CcnxNameComponents ();
Ilya Moiseenko332add02011-12-24 17:21:25 -080054
55 /**
56 * \brief Constructor
57 * Creates a prefix from a list of strings where every string represents a prefix component
58 * @param[in] components A list of strings
59 */
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070060 CcnxNameComponents (const std::list<boost::reference_wrapper<const std::string> > &components);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080061
Ilya Moiseenko332add02011-12-24 17:21:25 -080062 /**
63 * \brief Generic Add method
64 * Appends object of type T to the list of components
65 * @param[in] value The object to be appended
66 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080067 template<class T>
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070068 inline void
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080069 Add (const T &value);
70
Ilya Moiseenko332add02011-12-24 17:21:25 -080071 /**
72 * \brief Generic constructor operator
73 * The object of type T will be appended to the list of components
74 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080075 template<class T>
76 inline CcnxNameComponents&
77 operator () (const T &value);
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070078
Ilya Moiseenko332add02011-12-24 17:21:25 -080079 /**
80 * \brief Get a name
81 * Returns a list of components (strings)
82 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070083 const std::list<std::string> &
84 GetComponents () const;
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070085
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -080086 std::string
87 GetLastComponent () const;
88
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070089 /**
90 * \brief Get subcomponents of the name, starting with first component
Ilya Moiseenko332add02011-12-24 17:21:25 -080091 * @param[in] num Number of components to return. Valid value is in range [1, GetComponents ().size ()]
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070092 */
93 std::list<boost::reference_wrapper<const std::string> >
94 GetSubComponents (size_t num) const;
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070095
Ilya Moiseenko332add02011-12-24 17:21:25 -080096 /**
97 * \brief Print name
98 * @param[in] os Stream to print
99 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700100 void Print (std::ostream &os) const;
101
Ilya Moiseenko332add02011-12-24 17:21:25 -0800102 /**
103 * \brief Returns the size of CcnxNameComponents
104 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700105 inline size_t
106 size () const;
107
Ilya Moiseenko332add02011-12-24 17:21:25 -0800108 /**
109 * \brief Equality operator for CcnxNameComponents
110 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700111 inline bool
112 operator== (const CcnxNameComponents &prefix) const;
113
Ilya Moiseenko332add02011-12-24 17:21:25 -0800114 /**
115 * \brief Less than operator for CcnxNameComponents
116 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700117 inline bool
118 operator< (const CcnxNameComponents &prefix) const;
119
120private:
Ilya Moiseenko332add02011-12-24 17:21:25 -0800121 std::list<std::string> m_prefix; ///< \brief a list of strings (components)
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700122
Ilya Moiseenko332add02011-12-24 17:21:25 -0800123 typedef std::list<std::string>::iterator iterator;
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700124 typedef std::list<std::string>::const_iterator const_iterator;
125};
126
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700127/**
128 * \brief Print out name components separated by slashes, e.g., /first/second/third
129 */
130std::ostream &
131operator << (std::ostream &os, const CcnxNameComponents &components);
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700132
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700133/**
134 * \brief Read components from input and add them to components. Will read input stream till eof
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700135 * Substrings separated by slashes will become separate components
136 */
137std::istream &
138operator >> (std::istream &is, CcnxNameComponents &components);
Ilya Moiseenko332add02011-12-24 17:21:25 -0800139
140/**
141 * \brief Returns the size of CcnxNameComponents object
142 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700143size_t
144CcnxNameComponents::size () const
145{
146 return m_prefix.size ();
147}
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800148
Ilya Moiseenko332add02011-12-24 17:21:25 -0800149/**
150 * \brief Generic constructor operator
151 * The object of type T will be appended to the list of components
152 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800153template<class T>
154CcnxNameComponents&
155CcnxNameComponents::operator () (const T &value)
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700156{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800157 Add (value);
158 return *this;
159}
160
Ilya Moiseenko332add02011-12-24 17:21:25 -0800161/**
162 * \brief Generic Add method
163 * Appends object of type T to the list of components
164 * @param[in] value The object to be appended
165 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800166template<class T>
167void
168CcnxNameComponents::Add (const T &value)
169{
170 std::ostringstream os;
171 os << value;
172 m_prefix.push_back (os.str ());
173}
174
Ilya Moiseenko332add02011-12-24 17:21:25 -0800175/**
176 * \brief Equality operator for CcnxNameComponents
177 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700178bool
179CcnxNameComponents::operator== (const CcnxNameComponents &prefix) const
180{
181 if (m_prefix.size () != prefix.m_prefix.size ())
182 return false;
183
184 return std::equal (m_prefix.begin (), m_prefix.end (), prefix.m_prefix.begin ());
185}
186
Ilya Moiseenko332add02011-12-24 17:21:25 -0800187/**
188 * \brief Less than operator for CcnxNameComponents
189 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700190bool
191CcnxNameComponents::operator< (const CcnxNameComponents &prefix) const
192{
193 return std::lexicographical_compare (m_prefix.begin (), m_prefix.end (),
194 prefix.m_prefix.begin (), prefix.m_prefix.end ());
195}
196
197
198/**
199* \class ns3::ComponentsValue
200* \brief hold objects of type ns3:CcnxNameComponents
201*/
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700202ATTRIBUTE_HELPER_HEADER (CcnxNameComponents);
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700203} // namespace ns3
204
205#endif // _NDN_NAME_COMPONENTS_H_
206