blob: fdffe9112b987c5218231d09b41058d29f7114f6 [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
58 /**
59 * \brief Get subcomponents of the name, starting with first component
60 * \param num Number of components to return. Valid value is in range [1, GetComponents ().size ()]
61 */
62 std::list<boost::reference_wrapper<const std::string> >
63 GetSubComponents (size_t num) const;
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070064
65 // virtual uint32_t
66 // GetSerializedSize (void) const;
67
68 // virtual void
69 // Serialize (Buffer::Iterator start) const;
70
71 // virtual uint32_t
72 // Deserialize (Buffer::Iterator start);
73
74 void Print (std::ostream &os) const;
75
76 inline size_t
77 size () const;
78
79 inline bool
80 operator== (const CcnxNameComponents &prefix) const;
81
82 inline bool
83 operator< (const CcnxNameComponents &prefix) const;
84
85private:
86 std::list<std::string> m_prefix;
87
88 typedef std::list<std::string>::iterator iterator;
89 typedef std::list<std::string>::const_iterator const_iterator;
90};
91
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070092/**
93 * \brief Print out name components separated by slashes, e.g., /first/second/third
94 */
95std::ostream &
96operator << (std::ostream &os, const CcnxNameComponents &components);
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070097
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070098/**
99 * \brief Read components from input and add them to components. Will read input stream till eof
100 *
101 * \todo Check that NS-3 doesn't give unlimited input streams... Otherwise it would be disaster
102 *
103 * Substrings separated by slashes will become separate components
104 */
105std::istream &
106operator >> (std::istream &is, CcnxNameComponents &components);
107
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700108size_t
109CcnxNameComponents::size () const
110{
111 return m_prefix.size ();
112}
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800113
114template<class T>
115CcnxNameComponents&
116CcnxNameComponents::operator () (const T &value)
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700117{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800118 Add (value);
119 return *this;
120}
121
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800122// template<>
123// void
124// CcnxNameComponents::Add (const std::string &string)
125// {
126// m_prefix.push_back (string);
127// }
128
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800129template<class T>
130void
131CcnxNameComponents::Add (const T &value)
132{
133 std::ostringstream os;
134 os << value;
135 m_prefix.push_back (os.str ());
136}
137
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700138bool
139CcnxNameComponents::operator== (const CcnxNameComponents &prefix) const
140{
141 if (m_prefix.size () != prefix.m_prefix.size ())
142 return false;
143
144 return std::equal (m_prefix.begin (), m_prefix.end (), prefix.m_prefix.begin ());
145}
146
147bool
148CcnxNameComponents::operator< (const CcnxNameComponents &prefix) const
149{
150 return std::lexicographical_compare (m_prefix.begin (), m_prefix.end (),
151 prefix.m_prefix.begin (), prefix.m_prefix.end ());
152}
153
154
155/**
156* \class ns3::ComponentsValue
157* \brief hold objects of type ns3:CcnxNameComponents
158*/
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700159ATTRIBUTE_HELPER_HEADER (CcnxNameComponents);
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700160} // namespace ns3
161
162#endif // _NDN_NAME_COMPONENTS_H_
163