NS-3 based Named Data Networking (NDN) simulator
ndnSIM: NDN, CCN, CCNx, content centric networks
API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
name.h
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2 /*
3  * Copyright (c) 2013, Regents of the University of California
4  * Alexander Afanasyev
5  * Zhenkai Zhu
6  *
7  * BSD license, See the LICENSE file for more information
8  *
9  * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
10  * Zhenkai Zhu <zhenkai@cs.ucla.edu>
11  */
12 
13 #ifndef NDN_NAME_H
14 #define NDN_NAME_H
15 
16 #include "ns3/ndn-common.h"
17 #include "ns3/simple-ref-count.h"
18 #include "ns3/attribute.h"
19 #include "ns3/attribute-helper.h"
20 
21 #include "name-component.h"
22 
23 NDN_NAMESPACE_BEGIN
24 
29 class Name : public SimpleRefCount<Name>
30 {
31 public:
32  typedef std::vector<name::Component>::iterator iterator;
33  typedef std::vector<name::Component>::const_iterator const_iterator;
34  typedef std::vector<name::Component>::reverse_iterator reverse_iterator;
35  typedef std::vector<name::Component>::const_reverse_iterator const_reverse_iterator;
36  typedef std::vector<name::Component>::reference reference;
37  typedef std::vector<name::Component>::const_reference const_reference;
38 
40 
42  // CONSTRUCTORS //
44 
48  Name ();
49 
55  Name (const Name &other);
56 
62  Name (const std::string &url);
63 
70  template<class Iterator>
71  Name (Iterator begin, Iterator end);
72 
76  Name &
77  operator= (const Name &other);
78 
79 
81  // SETTERS //
83 
90  inline Name &
91  append (const name::Component &comp);
92 
101  inline Name &
102  appendBySwap (name::Component &comp);
103 
111  template<class Iterator>
112  inline Name &
113  append (Iterator begin, Iterator end);
114 
121  inline Name &
122  append (const Name &comp);
123 
133  inline Name &
134  append (const std::string &compStr);
135 
143  inline Name &
144  append (const void *buf, size_t size);
145 
157  inline Name &
158  appendNumber (uint64_t number);
159 
177  inline Name &
178  appendNumberWithMarker (uint64_t number, unsigned char marker);
179 
185  inline Name &
186  appendSeqNum (uint64_t seqno);
187 
193  inline Name &
194  appendControlNum (uint64_t control);
195 
201  inline Name &
202  appendBlkId (uint64_t blkid);
203 
211  Name &
212  appendVersion (uint64_t version = Name::nversion);
213 
215  // GETTERS //
217 
222  inline size_t
223  size () const;
224 
233  const name::Component &
234  get (int index) const;
235 
244  get (int index);
245 
249  inline Name::const_iterator
250  begin () const;
251 
252  inline Name::iterator
253  begin ();
254 
255  inline Name::const_iterator
256  end () const;
257 
258  inline Name::iterator
259  end ();
260 
261  inline Name::const_reverse_iterator
262  rbegin () const;
263 
264  inline Name::reverse_iterator
265  rbegin ();
266 
267  inline Name::const_reverse_iterator
268  rend () const;
269 
270  inline Name::reverse_iterator
271  rend ();
272 
273 
277 
283  Name
284  getSubName (size_t pos = 0, size_t len = npos) const;
285 
291  inline Name
292  getPrefix (size_t len, size_t skip = 0) const;
293 
299  inline Name
300  getPostfix (size_t len, size_t skip = 0) const;
301 
305  std::string
306  toUri () const;
307 
312  void
313  toUri (std::ostream &os) const;
314 
316  // Helpers and compatibility wrappers
318 
325  int
326  compare (const Name &name) const;
327 
331  inline bool
332  operator == (const Name &name) const;
333 
337  inline bool
338  operator != (const Name &name) const;
339 
343  inline bool
344  operator <= (const Name &name) const;
345 
349  inline bool
350  operator < (const Name &name) const;
351 
355  inline bool
356  operator >= (const Name &name) const;
357 
361  inline bool
362  operator > (const Name &name) const;
363 
368  inline name::Component &
369  operator [] (int index);
370 
375  inline const name::Component &
376  operator [] (int index) const;
377 
381  Name
382  operator + (const Name &name) const;
383 
387  template<class T>
388  inline void
389  push_back (const T &comp);
390 
391 public:
392  // Data Members (public):
394  const static size_t npos = static_cast<size_t> (-1);
395  const static uint64_t nversion = static_cast<uint64_t> (-1);
396 
397 private:
398  std::vector<name::Component> m_comps;
399 };
400 
401 inline std::ostream &
402 operator << (std::ostream &os, const Name &name)
403 {
404  name.toUri (os);
405  return os;
406 }
407 
408 inline std::istream &
409 operator >> (std::istream &is, Name &name)
410 {
411  std::string line;
412  is >> line;
413  name = Name (line);
414 
415  return is;
416 }
417 
419 // Definition of inline methods
421 
422 template<class Iterator>
423 Name::Name (Iterator begin, Iterator end)
424 {
425  append (begin, end);
426 }
427 
428 inline Name &
429 Name::append (const name::Component &comp)
430 {
431  if (comp.size () != 0)
432  m_comps.push_back (comp);
433  return *this;
434 }
435 
436 inline Name &
437 Name::appendBySwap (name::Component &comp)
438 {
439  if (comp.size () != 0)
440  {
441  Name::iterator newComp = m_comps.insert (end (), name::Component ());
442  newComp->swap (comp);
443  }
444  return *this;
445 }
446 
447 template<class Iterator>
448 inline Name &
449 Name::append (Iterator begin, Iterator end)
450 {
451  for (Iterator i = begin; i != end; i++)
452  {
453  append (*i);
454  }
455  return *this;
456 }
457 
458 Name &
459 Name::append (const Name &comp)
460 {
461  if (this == &comp)
462  {
463  // have to double-copy if the object is self, otherwise results very frustrating (because we use vector...)
464  return append (Name (comp.begin (), comp.end ()));
465  }
466  return append (comp.begin (), comp.end ());
467 }
468 
469 Name &
470 Name::append (const std::string &compStr)
471 {
472  name::Component comp (compStr);
473  return appendBySwap (comp);
474 }
475 
476 Name &
477 Name::append (const void *buf, size_t size)
478 {
479  name::Component comp (buf, size);
480  return appendBySwap (comp);
481 }
482 
483 Name &
484 Name::appendNumber (uint64_t number)
485 {
486  name::Component comp;
487  return appendBySwap (comp.fromNumber (number));
488 }
489 
490 Name &
491 Name::appendNumberWithMarker (uint64_t number, unsigned char marker)
492 {
493  name::Component comp;
494  return appendBySwap (comp.fromNumberWithMarker (number, marker));
495 }
496 
497 inline Name &
498 Name::appendSeqNum (uint64_t seqno)
499 {
500  return appendNumberWithMarker (seqno, 0x00);
501 }
502 
503 inline Name &
504 Name::appendControlNum (uint64_t control)
505 {
506  return appendNumberWithMarker (control, 0xC1);
507 }
508 
509 inline Name &
510 Name::appendBlkId (uint64_t blkid)
511 {
512  return appendNumberWithMarker (blkid, 0xFB);
513 }
514 
515 inline size_t
516 Name::size () const
517 {
518  return m_comps.size ();
519 }
520 
524 inline Name::const_iterator
525 Name::begin () const
526 {
527  return m_comps.begin ();
528 }
529 
530 inline Name::iterator
531 Name::begin ()
532 {
533  return m_comps.begin ();
534 }
535 
536 inline Name::const_iterator
537 Name::end () const
538 {
539  return m_comps.end ();
540 }
541 
542 inline Name::iterator
543 Name::end ()
544 {
545  return m_comps.end ();
546 }
547 
548 inline Name::const_reverse_iterator
549 Name::rbegin () const
550 {
551  return m_comps.rbegin ();
552 }
553 
554 inline Name::reverse_iterator
555 Name::rbegin ()
556 {
557  return m_comps.rbegin ();
558 }
559 
560 inline Name::const_reverse_iterator
561 Name::rend () const
562 {
563  return m_comps.rend ();
564 }
565 
566 
567 inline Name::reverse_iterator
568 Name::rend ()
569 {
570  return m_comps.rend ();
571 }
572 
573 
575 
576 
577 inline Name
578 Name::getPrefix (size_t len, size_t skip/* = 0*/) const
579 {
580  return getSubName (skip, len);
581 }
582 
583 inline Name
584 Name::getPostfix (size_t len, size_t skip/* = 0*/) const
585 {
586  return getSubName (size () - len - skip, len);
587 }
588 
589 
590 template<class T>
591 inline void
592 Name::push_back (const T &comp)
593 {
594  append (comp);
595 }
596 
597 inline bool
598 Name::operator ==(const Name &name) const
599 {
600  return (compare (name) == 0);
601 }
602 
603 inline bool
604 Name::operator !=(const Name &name) const
605 {
606  return (compare (name) != 0);
607 }
608 
609 inline bool
610 Name::operator <= (const Name &name) const
611 {
612  return (compare (name) <= 0);
613 }
614 
615 inline bool
616 Name::operator < (const Name &name) const
617 {
618  return (compare (name) < 0);
619 }
620 
621 inline bool
622 Name::operator >= (const Name &name) const
623 {
624  return (compare (name) >= 0);
625 }
626 
627 inline bool
628 Name::operator > (const Name &name) const
629 {
630  return (compare (name) > 0);
631 }
632 
633 inline name::Component &
634 Name::operator [] (int index)
635 {
636  return get (index);
637 }
638 
639 inline const name::Component &
640 Name::operator [] (int index) const
641 {
642  return get (index);
643 }
644 
645 ATTRIBUTE_HELPER_HEADER (Name);
646 
647 NDN_NAMESPACE_END
648 
649 #endif
Class for NDN Name.
Definition: name.h:29
std::string toUri() const
Get text representation of the name (URI)
Definition: name.cc:221
Name::const_iterator end() const
End iterator (const)
Definition: name.h:537
Name::const_iterator begin() const
Begin iterator (const)
Definition: name.h:525
Component & fromNumber(uint64_t number)
Create network-ordered numeric component.
boost::error_info< struct tag_pos, int > pos
Report of the position of the error (error-specific meaning)
Definition: error.h:105
Class to representing binary blob of NDN name component.
Component & fromNumberWithMarker(uint64_t number, unsigned char marker)
Create network-ordered numeric component to the name with marker.
void push_back(const T &comp)
A wrapper for append method.
Definition: name.h:592