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); |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 46 | |
| 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 Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 55 | |
| 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 Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 62 | |
| 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 | |
| 83 | private: |
| 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 Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 90 | /** |
| 91 | * \brief Print out name components separated by slashes, e.g., /first/second/third |
| 92 | */ |
| 93 | std::ostream & |
| 94 | operator << (std::ostream &os, const CcnxNameComponents &components); |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 95 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 96 | /** |
| 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 | */ |
| 103 | std::istream & |
| 104 | operator >> (std::istream &is, CcnxNameComponents &components); |
| 105 | |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 106 | size_t |
| 107 | CcnxNameComponents::size () const |
| 108 | { |
| 109 | return m_prefix.size (); |
| 110 | } |
| 111 | |
| 112 | void |
| 113 | CcnxNameComponents::Add (const std::string &s) |
| 114 | { |
| 115 | (*this) (s); |
| 116 | } |
| 117 | |
| 118 | bool |
| 119 | CcnxNameComponents::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 | |
| 127 | bool |
| 128 | CcnxNameComponents::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 Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 139 | ATTRIBUTE_HELPER_HEADER (CcnxNameComponents); |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 140 | } // namespace ns3 |
| 141 | |
| 142 | #endif // _NDN_NAME_COMPONENTS_H_ |
| 143 | |