blob: b806cad083d03a2cc5856c3ce6411a9d17253ea8 [file] [log] [blame]
Junxiao Shia4f2be82014-03-02 22:56:41 -07001/* -*- 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
13namespace nfd
14{
15
16class Forwarder;
17
18/** \brief container of all Faces
19 */
20class FaceTable
21{
22public:
23 explicit
24 FaceTable(Forwarder& forwarder);
25
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070026 VIRTUAL_WITH_TESTS
27 ~FaceTable();
28
29 VIRTUAL_WITH_TESTS void
Junxiao Shia4f2be82014-03-02 22:56:41 -070030 add(shared_ptr<Face> face);
31
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070032 VIRTUAL_WITH_TESTS shared_ptr<Face>
Junxiao Shia4f2be82014-03-02 22:56:41 -070033 get(FaceId id) const;
34
35 size_t
36 size() const;
37
38public: // enumeration
39 typedef std::map<FaceId, shared_ptr<Face> > FaceMap;
40
Alexander Afanasyev7b7dfdd2014-03-21 13:57:54 -070041 /** \brief ForwardIterator for shared_ptr<Face>
Junxiao Shia4f2be82014-03-02 22:56:41 -070042 */
43 typedef MapValueIterator<FaceMap> const_iterator;
44
Alexander Afanasyev7b7dfdd2014-03-21 13:57:54 -070045 /** \brief ReverseIterator for shared_ptr<Face>
46 */
47 typedef MapValueReverseIterator<FaceMap> const_reverse_iterator;
48
Junxiao Shia4f2be82014-03-02 22:56:41 -070049 const_iterator
50 begin() const;
51
52 const_iterator
53 end() const;
54
Alexander Afanasyev7b7dfdd2014-03-21 13:57:54 -070055 const_reverse_iterator
56 rbegin() const;
57
58 const_reverse_iterator
59 rend() const;
60
Junxiao Shibd392bf2014-03-17 15:54:11 -070061public: // 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 Shia4f2be82014-03-02 22:56:41 -070072private:
Junxiao Shic542b2b2014-03-16 21:45:52 -070073 // 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
78private:
Junxiao Shia4f2be82014-03-02 22:56:41 -070079 Forwarder& m_forwarder;
80 FaceId m_lastFaceId;
81 FaceMap m_faces;
82};
83
84inline shared_ptr<Face>
85FaceTable::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
91inline size_t
92FaceTable::size() const
93{
94 return m_faces.size();
95}
96
97inline FaceTable::const_iterator
98FaceTable::begin() const
99{
100 return const_iterator(m_faces.begin());
101}
102
103inline FaceTable::const_iterator
104FaceTable::end() const
105{
106 return const_iterator(m_faces.end());
107}
108
Alexander Afanasyev7b7dfdd2014-03-21 13:57:54 -0700109inline FaceTable::const_reverse_iterator
110FaceTable::rbegin() const
111{
112 return const_reverse_iterator(m_faces.rbegin());
113}
114
115inline FaceTable::const_reverse_iterator
116FaceTable::rend() const
117{
118 return const_reverse_iterator(m_faces.rend());
119}
120
Junxiao Shia4f2be82014-03-02 22:56:41 -0700121} // namespace nfd
122
123#endif // NFD_FW_FACE_TABLE_HPP