blob: 7e2bb710de9662c2d2849db33c854199b22a955a [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 ();
44 CcnxNameComponents (const std::string &s);
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070045 CcnxNameComponents (const std::list<boost::reference_wrapper<const std::string> > &components);
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070046
47 inline void
48 Add (const std::string &s);
49
50 CcnxNameComponents&
51 operator () (const std::string &s);
52
53 const std::list<std::string> &
54 GetComponents () const;
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070055
56 /**
57 * \brief Get subcomponents of the name, starting with first component
58 * \param num Number of components to return. Valid value is in range [1, GetComponents ().size ()]
59 */
60 std::list<boost::reference_wrapper<const std::string> >
61 GetSubComponents (size_t num) const;
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070062
63 // virtual uint32_t
64 // GetSerializedSize (void) const;
65
66 // virtual void
67 // Serialize (Buffer::Iterator start) const;
68
69 // virtual uint32_t
70 // Deserialize (Buffer::Iterator start);
71
72 void Print (std::ostream &os) const;
73
74 inline size_t
75 size () const;
76
77 inline bool
78 operator== (const CcnxNameComponents &prefix) const;
79
80 inline bool
81 operator< (const CcnxNameComponents &prefix) const;
82
83private:
84 std::list<std::string> m_prefix;
85
86 typedef std::list<std::string>::iterator iterator;
87 typedef std::list<std::string>::const_iterator const_iterator;
88};
89
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070090/**
91 * \brief Print out name components separated by slashes, e.g., /first/second/third
92 */
93std::ostream &
94operator << (std::ostream &os, const CcnxNameComponents &components);
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070095
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070096/**
97 * \brief Read components from input and add them to components. Will read input stream till eof
98 *
99 * \todo Check that NS-3 doesn't give unlimited input streams... Otherwise it would be disaster
100 *
101 * Substrings separated by slashes will become separate components
102 */
103std::istream &
104operator >> (std::istream &is, CcnxNameComponents &components);
105
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700106size_t
107CcnxNameComponents::size () const
108{
109 return m_prefix.size ();
110}
111
112void
113CcnxNameComponents::Add (const std::string &s)
114{
115 (*this) (s);
116}
117
118bool
119CcnxNameComponents::operator== (const CcnxNameComponents &prefix) const
120{
121 if (m_prefix.size () != prefix.m_prefix.size ())
122 return false;
123
124 return std::equal (m_prefix.begin (), m_prefix.end (), prefix.m_prefix.begin ());
125}
126
127bool
128CcnxNameComponents::operator< (const CcnxNameComponents &prefix) const
129{
130 return std::lexicographical_compare (m_prefix.begin (), m_prefix.end (),
131 prefix.m_prefix.begin (), prefix.m_prefix.end ());
132}
133
134
135/**
136* \class ns3::ComponentsValue
137* \brief hold objects of type ns3:CcnxNameComponents
138*/
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700139ATTRIBUTE_HELPER_HEADER (CcnxNameComponents);
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700140} // namespace ns3
141
142#endif // _NDN_NAME_COMPONENTS_H_
143