Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 33 | #include <boost/ref.hpp> |
| 34 | |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 35 | namespace ns3 { |
| 36 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 37 | class CcnxNameComponents : public SimpleRefCount<CcnxNameComponents> |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 38 | { |
| 39 | public: |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 40 | /** |
| 41 | * \brief Creates a prefix with zero components (can be looked as root "/") |
| 42 | */ |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 43 | CcnxNameComponents (); |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 44 | // CcnxNameComponents (const std::string &s); |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 45 | CcnxNameComponents (const std::list<boost::reference_wrapper<const std::string> > &components); |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 46 | |
| 47 | template<class T> |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 48 | inline void |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 49 | Add (const T &value); |
| 50 | |
| 51 | template<class T> |
| 52 | inline CcnxNameComponents& |
| 53 | operator () (const T &value); |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 54 | |
| 55 | const std::list<std::string> & |
| 56 | GetComponents () const; |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 57 | |
| 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 Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 64 | |
| 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 | |
| 85 | private: |
| 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 Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 92 | /** |
| 93 | * \brief Print out name components separated by slashes, e.g., /first/second/third |
| 94 | */ |
| 95 | std::ostream & |
| 96 | operator << (std::ostream &os, const CcnxNameComponents &components); |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 97 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 98 | /** |
| 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 | */ |
| 105 | std::istream & |
| 106 | operator >> (std::istream &is, CcnxNameComponents &components); |
| 107 | |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 108 | size_t |
| 109 | CcnxNameComponents::size () const |
| 110 | { |
| 111 | return m_prefix.size (); |
| 112 | } |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 113 | |
| 114 | template<class T> |
| 115 | CcnxNameComponents& |
| 116 | CcnxNameComponents::operator () (const T &value) |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 117 | { |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 118 | Add (value); |
| 119 | return *this; |
| 120 | } |
| 121 | |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 122 | // template<> |
| 123 | // void |
| 124 | // CcnxNameComponents::Add (const std::string &string) |
| 125 | // { |
| 126 | // m_prefix.push_back (string); |
| 127 | // } |
| 128 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 129 | template<class T> |
| 130 | void |
| 131 | CcnxNameComponents::Add (const T &value) |
| 132 | { |
| 133 | std::ostringstream os; |
| 134 | os << value; |
| 135 | m_prefix.push_back (os.str ()); |
| 136 | } |
| 137 | |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 138 | bool |
| 139 | CcnxNameComponents::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 | |
| 147 | bool |
| 148 | CcnxNameComponents::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 Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 159 | ATTRIBUTE_HELPER_HEADER (CcnxNameComponents); |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 160 | } // namespace ns3 |
| 161 | |
| 162 | #endif // _NDN_NAME_COMPONENTS_H_ |
| 163 | |