blob: 58ca927b01092e2c6a2d54f62d0abbc470505b62 [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
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070037class CcnxNameComponents : public SimpleRefCount<CcnxNameComponents>
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070038{
39public:
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070040 /**
41 * \brief Creates a prefix with zero components (can be looked as root "/")
42 */
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070043 CcnxNameComponents ();
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070044 // CcnxNameComponents (const std::string &s);
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070045 CcnxNameComponents (const std::list<boost::reference_wrapper<const std::string> > &components);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080046
47 template<class T>
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070048 inline void
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080049 Add (const T &value);
50
51 template<class T>
52 inline CcnxNameComponents&
53 operator () (const T &value);
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070054
55 const std::list<std::string> &
56 GetComponents () const;
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070057
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -080058 std::string
59 GetLastComponent () const;
60
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070061 /**
62 * \brief Get subcomponents of the name, starting with first component
63 * \param num Number of components to return. Valid value is in range [1, GetComponents ().size ()]
64 */
65 std::list<boost::reference_wrapper<const std::string> >
66 GetSubComponents (size_t num) const;
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070067
68 // virtual uint32_t
69 // GetSerializedSize (void) const;
70
71 // virtual void
72 // Serialize (Buffer::Iterator start) const;
73
74 // virtual uint32_t
75 // Deserialize (Buffer::Iterator start);
76
77 void Print (std::ostream &os) const;
78
79 inline size_t
80 size () const;
81
82 inline bool
83 operator== (const CcnxNameComponents &prefix) const;
84
85 inline bool
86 operator< (const CcnxNameComponents &prefix) const;
87
88private:
89 std::list<std::string> m_prefix;
90
91 typedef std::list<std::string>::iterator iterator;
92 typedef std::list<std::string>::const_iterator const_iterator;
93};
94
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070095/**
96 * \brief Print out name components separated by slashes, e.g., /first/second/third
97 */
98std::ostream &
99operator << (std::ostream &os, const CcnxNameComponents &components);
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700100
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700101/**
102 * \brief Read components from input and add them to components. Will read input stream till eof
103 *
104 * \todo Check that NS-3 doesn't give unlimited input streams... Otherwise it would be disaster
105 *
106 * Substrings separated by slashes will become separate components
107 */
108std::istream &
109operator >> (std::istream &is, CcnxNameComponents &components);
110
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700111size_t
112CcnxNameComponents::size () const
113{
114 return m_prefix.size ();
115}
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800116
117template<class T>
118CcnxNameComponents&
119CcnxNameComponents::operator () (const T &value)
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700120{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800121 Add (value);
122 return *this;
123}
124
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800125// template<>
126// void
127// CcnxNameComponents::Add (const std::string &string)
128// {
129// m_prefix.push_back (string);
130// }
131
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800132template<class T>
133void
134CcnxNameComponents::Add (const T &value)
135{
136 std::ostringstream os;
137 os << value;
138 m_prefix.push_back (os.str ());
139}
140
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700141bool
142CcnxNameComponents::operator== (const CcnxNameComponents &prefix) const
143{
144 if (m_prefix.size () != prefix.m_prefix.size ())
145 return false;
146
147 return std::equal (m_prefix.begin (), m_prefix.end (), prefix.m_prefix.begin ());
148}
149
150bool
151CcnxNameComponents::operator< (const CcnxNameComponents &prefix) const
152{
153 return std::lexicographical_compare (m_prefix.begin (), m_prefix.end (),
154 prefix.m_prefix.begin (), prefix.m_prefix.end ());
155}
156
157
158/**
159* \class ns3::ComponentsValue
160* \brief hold objects of type ns3:CcnxNameComponents
161*/
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700162ATTRIBUTE_HELPER_HEADER (CcnxNameComponents);
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700163} // namespace ns3
164
165#endif // _NDN_NAME_COMPONENTS_H_
166