blob: 2af2b6bf5ae5eebfdc8a0cce56d434a4505ff6fe [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
41 /** \brief ForwarderIterator for shared_ptr<Face>
42 */
43 typedef MapValueIterator<FaceMap> const_iterator;
44
45 const_iterator
46 begin() const;
47
48 const_iterator
49 end() const;
50
51private:
Junxiao Shic542b2b2014-03-16 21:45:52 -070052 // remove is private because it's a subscriber of face.onFail event.
53 // face->close() closes a face and would trigger .remove(face)
54 void
55 remove(shared_ptr<Face> face);
56
57private:
Junxiao Shia4f2be82014-03-02 22:56:41 -070058 Forwarder& m_forwarder;
59 FaceId m_lastFaceId;
60 FaceMap m_faces;
61};
62
63inline shared_ptr<Face>
64FaceTable::get(FaceId id) const
65{
66 std::map<FaceId, shared_ptr<Face> >::const_iterator i = m_faces.find(id);
67 return (i == m_faces.end()) ? (shared_ptr<Face>()) : (i->second);
68}
69
70inline size_t
71FaceTable::size() const
72{
73 return m_faces.size();
74}
75
76inline FaceTable::const_iterator
77FaceTable::begin() const
78{
79 return const_iterator(m_faces.begin());
80}
81
82inline FaceTable::const_iterator
83FaceTable::end() const
84{
85 return const_iterator(m_faces.end());
86}
87
88} // namespace nfd
89
90#endif // NFD_FW_FACE_TABLE_HPP