Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #ifndef NFD_FW_FACE_TABLE_HPP |
| 8 | #define NFD_FW_FACE_TABLE_HPP |
| 9 | |
| 10 | #include "face/face.hpp" |
| 11 | #include "core/map-value-iterator.hpp" |
| 12 | |
| 13 | namespace nfd |
| 14 | { |
| 15 | |
| 16 | class Forwarder; |
| 17 | |
| 18 | /** \brief container of all Faces |
| 19 | */ |
| 20 | class FaceTable |
| 21 | { |
| 22 | public: |
| 23 | explicit |
| 24 | FaceTable(Forwarder& forwarder); |
| 25 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 26 | VIRTUAL_WITH_TESTS |
| 27 | ~FaceTable(); |
| 28 | |
| 29 | VIRTUAL_WITH_TESTS void |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 30 | add(shared_ptr<Face> face); |
| 31 | |
Steve DiBenedetto | abe9e97 | 2014-02-20 15:37:04 -0700 | [diff] [blame] | 32 | VIRTUAL_WITH_TESTS shared_ptr<Face> |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 33 | get(FaceId id) const; |
| 34 | |
| 35 | size_t |
| 36 | size() const; |
| 37 | |
| 38 | public: // enumeration |
| 39 | typedef std::map<FaceId, shared_ptr<Face> > FaceMap; |
| 40 | |
Alexander Afanasyev | 7b7dfdd | 2014-03-21 13:57:54 -0700 | [diff] [blame] | 41 | /** \brief ForwardIterator for shared_ptr<Face> |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 42 | */ |
| 43 | typedef MapValueIterator<FaceMap> const_iterator; |
| 44 | |
Alexander Afanasyev | 7b7dfdd | 2014-03-21 13:57:54 -0700 | [diff] [blame] | 45 | /** \brief ReverseIterator for shared_ptr<Face> |
| 46 | */ |
| 47 | typedef MapValueReverseIterator<FaceMap> const_reverse_iterator; |
| 48 | |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 49 | const_iterator |
| 50 | begin() const; |
| 51 | |
| 52 | const_iterator |
| 53 | end() const; |
| 54 | |
Alexander Afanasyev | 7b7dfdd | 2014-03-21 13:57:54 -0700 | [diff] [blame] | 55 | const_reverse_iterator |
| 56 | rbegin() const; |
| 57 | |
| 58 | const_reverse_iterator |
| 59 | rend() const; |
| 60 | |
Junxiao Shi | bd392bf | 2014-03-17 15:54:11 -0700 | [diff] [blame] | 61 | public: // events |
| 62 | /** \brief fires after a Face is added |
| 63 | */ |
| 64 | EventEmitter<shared_ptr<Face> > onAdd; |
| 65 | |
| 66 | /** \brief fires before a Face is removed |
| 67 | * |
| 68 | * FaceId is valid when this event is fired |
| 69 | */ |
| 70 | EventEmitter<shared_ptr<Face> > onRemove; |
| 71 | |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 72 | private: |
Junxiao Shi | c542b2b | 2014-03-16 21:45:52 -0700 | [diff] [blame] | 73 | // remove is private because it's a subscriber of face.onFail event. |
| 74 | // face->close() closes a face and would trigger .remove(face) |
| 75 | void |
| 76 | remove(shared_ptr<Face> face); |
| 77 | |
| 78 | private: |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 79 | Forwarder& m_forwarder; |
| 80 | FaceId m_lastFaceId; |
| 81 | FaceMap m_faces; |
| 82 | }; |
| 83 | |
| 84 | inline shared_ptr<Face> |
| 85 | FaceTable::get(FaceId id) const |
| 86 | { |
| 87 | std::map<FaceId, shared_ptr<Face> >::const_iterator i = m_faces.find(id); |
| 88 | return (i == m_faces.end()) ? (shared_ptr<Face>()) : (i->second); |
| 89 | } |
| 90 | |
| 91 | inline size_t |
| 92 | FaceTable::size() const |
| 93 | { |
| 94 | return m_faces.size(); |
| 95 | } |
| 96 | |
| 97 | inline FaceTable::const_iterator |
| 98 | FaceTable::begin() const |
| 99 | { |
| 100 | return const_iterator(m_faces.begin()); |
| 101 | } |
| 102 | |
| 103 | inline FaceTable::const_iterator |
| 104 | FaceTable::end() const |
| 105 | { |
| 106 | return const_iterator(m_faces.end()); |
| 107 | } |
| 108 | |
Alexander Afanasyev | 7b7dfdd | 2014-03-21 13:57:54 -0700 | [diff] [blame] | 109 | inline FaceTable::const_reverse_iterator |
| 110 | FaceTable::rbegin() const |
| 111 | { |
| 112 | return const_reverse_iterator(m_faces.rbegin()); |
| 113 | } |
| 114 | |
| 115 | inline FaceTable::const_reverse_iterator |
| 116 | FaceTable::rend() const |
| 117 | { |
| 118 | return const_reverse_iterator(m_faces.rend()); |
| 119 | } |
| 120 | |
Junxiao Shi | a4f2be8 | 2014-03-02 22:56:41 -0700 | [diff] [blame] | 121 | } // namespace nfd |
| 122 | |
| 123 | #endif // NFD_FW_FACE_TABLE_HPP |