GComm  0.2.3
gmcast_link.hpp
1 /*
2  * Copyright (C) 2009 Codership Oy <info@codership.com>
3  */
4 
5 #ifndef GCOMM_GMCAST_LINK_HPP
6 #define GCOMM_GMCAST_LINK_HPP
7 
8 #include "gcomm/uuid.hpp"
9 
10 #include <set>
11 #include <string>
12 
13 namespace gcomm
14 {
15  namespace gmcast
16  {
17  class Link;
18  class LinkMapCmp;
19  class LinkMap;
20  std::ostream& operator<<(std::ostream& os, const LinkMap&);
21  }
22 }
23 
25 {
26 public:
27  Link(const gcomm::UUID& uuid,
28  const std::string& addr,
29  const std::string& mcast_addr) :
30  uuid_ (uuid),
31  addr_ (addr),
32  mcast_addr_(mcast_addr)
33  { }
34 
35  bool operator==(const Link& cmp) const
36  { return (uuid_ == cmp.uuid_ && addr_ == cmp.addr_); }
37 
38  bool operator<(const Link& cmp) const
39  {
40  return (uuid_ < cmp.uuid_ ||
41  (uuid_ == cmp.uuid_ && addr_ < cmp.addr_));
42 
43  }
44 
45  const gcomm::UUID& uuid() const { return uuid_; }
46  const std::string& addr() const { return addr_; }
47  const std::string& mcast_addr() const { return mcast_addr_; }
48 private:
49  UUID uuid_;
50  std::string addr_;
51  std::string mcast_addr_;
52 };
53 
54 
55 
57 {
58  typedef std::set<Link> MType;
59 public:
60  LinkMap() : link_map_() { }
61  typedef MType::iterator iterator;
62  typedef MType::const_iterator const_iterator;
63  typedef MType::value_type value_type;
64 
65  std::pair<iterator, bool> insert(const Link& i)
66  { return link_map_.insert(i); }
67 
68  iterator begin() { return link_map_.begin(); }
69  const_iterator begin() const { return link_map_.begin(); }
70  iterator end() { return link_map_.end(); }
71  const_iterator end() const { return link_map_.end(); }
72  const_iterator find(const value_type& vt) const { return link_map_.find(vt); }
73  size_t size() const { return link_map_.size(); }
74  static const UUID& key(const_iterator i) { return i->uuid(); }
75  static const Link& value(const_iterator i) { return *i; }
76  static const UUID& key(const value_type& vt) { return vt.uuid(); }
77  static const Link& value(const value_type& vt) { return vt; }
78  bool operator==(const LinkMap& cmp) const
79  { return (link_map_ == cmp.link_map_); }
80 private:
81  MType link_map_;
82 };
83 
84 inline std::ostream& gcomm::gmcast::operator<<(std::ostream& os, const LinkMap& lm)
85 {
86  for (LinkMap::const_iterator i = lm.begin(); i != lm.end();
87  ++i)
88  {
89  os << "\n(" << LinkMap::key(i) << ","
90  << LinkMap::value(i).addr() << ")";
91  }
92  return (os << "\n");
93 }
94 
95 
96 #endif // GCOMM_GMCAST_LINK_HPP
Definition: gmcast_link.hpp:56
Definition: uuid.hpp:26