File: instance_key.h

package info (click to toggle)
android-platform-tools 34.0.5-12
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 150,900 kB
  • sloc: cpp: 805,786; java: 293,500; ansic: 128,288; xml: 127,491; python: 41,481; sh: 14,245; javascript: 9,665; cs: 3,846; asm: 2,049; makefile: 1,917; yacc: 440; awk: 368; ruby: 183; sql: 140; perl: 88; lex: 67
file content (100 lines) | stat: -rw-r--r-- 2,743 bytes parent folder | download | duplicates (11)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef DISCOVERY_DNSSD_IMPL_INSTANCE_KEY_H_
#define DISCOVERY_DNSSD_IMPL_INSTANCE_KEY_H_

#include <string>
#include <utility>

#include "absl/strings/string_view.h"
#include "discovery/dnssd/impl/service_key.h"

namespace openscreen {
namespace discovery {

class DnsSdInstance;
class DomainName;
class MdnsRecord;
class ServiceKey;

// This class is intended to be used as the key of a std::unordered_map or
// std::map  when referencing data related to a specific service instance.
class InstanceKey : public ServiceKey {
 public:
  // NOTE: The record provided must have valid instance, service, and domain
  // labels.
  explicit InstanceKey(const MdnsRecord& record);
  explicit InstanceKey(const DomainName& domain);
  explicit InstanceKey(const DnsSdInstance& instance);

  // NOTE: The provided parameters must be valid instance,  service and domain
  // ids.
  InstanceKey(absl::string_view instance,
              absl::string_view service,
              absl::string_view domain);

  InstanceKey(const InstanceKey& other);
  InstanceKey(InstanceKey&& other);

  InstanceKey& operator=(const InstanceKey& rhs);
  InstanceKey& operator=(InstanceKey&& rhs);

  DomainName GetName() const override;

  const std::string& instance_id() const { return instance_id_; }

 private:
  std::string instance_id_;

  template <typename H>
  friend H AbslHashValue(H h, const InstanceKey& key);

  friend bool operator<(const InstanceKey& lhs, const InstanceKey& rhs);
};

template <typename H>
H AbslHashValue(H h, const InstanceKey& key) {
  return H::combine(std::move(h), key.service_id(), key.domain_id(),
                    key.instance_id_);
}

inline bool operator<(const InstanceKey& lhs, const InstanceKey& rhs) {
  int comp = lhs.domain_id().compare(rhs.domain_id());
  if (comp != 0) {
    return comp < 0;
  }

  comp = lhs.service_id().compare(rhs.service_id());
  if (comp != 0) {
    return comp < 0;
  }

  return lhs.instance_id_ < rhs.instance_id_;
}

inline bool operator>(const InstanceKey& lhs, const InstanceKey& rhs) {
  return rhs < lhs;
}

inline bool operator<=(const InstanceKey& lhs, const InstanceKey& rhs) {
  return !(rhs > lhs);
}

inline bool operator>=(const InstanceKey& lhs, const InstanceKey& rhs) {
  return !(rhs < lhs);
}

inline bool operator==(const InstanceKey& lhs, const InstanceKey& rhs) {
  return lhs <= rhs && lhs >= rhs;
}

inline bool operator!=(const InstanceKey& lhs, const InstanceKey& rhs) {
  return !(lhs == rhs);
}

}  // namespace discovery
}  // namespace openscreen

#endif  // DISCOVERY_DNSSD_IMPL_INSTANCE_KEY_H_