1// Copyright 2015 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_INTERFACE_PTR_INFO_H_
6#define MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_INTERFACE_PTR_INFO_H_
7
8#include <stdint.h>
9#include <utility>
10
11#include "base/macros.h"
12#include "mojo/public/cpp/bindings/scoped_interface_endpoint_handle.h"
13
14namespace mojo {
15
16// AssociatedInterfacePtrInfo stores necessary information to construct an
17// associated interface pointer. It is similar to InterfacePtrInfo except that
18// it doesn't own a message pipe handle.
19template <typename Interface>
20class AssociatedInterfacePtrInfo {
21 public:
22  AssociatedInterfacePtrInfo() : version_(0u) {}
23  AssociatedInterfacePtrInfo(std::nullptr_t) : version_(0u) {}
24
25  AssociatedInterfacePtrInfo(AssociatedInterfacePtrInfo&& other)
26      : handle_(std::move(other.handle_)), version_(other.version_) {
27    other.version_ = 0u;
28  }
29
30  AssociatedInterfacePtrInfo(ScopedInterfaceEndpointHandle handle,
31                             uint32_t version)
32      : handle_(std::move(handle)), version_(version) {}
33
34  ~AssociatedInterfacePtrInfo() {}
35
36  AssociatedInterfacePtrInfo& operator=(AssociatedInterfacePtrInfo&& other) {
37    if (this != &other) {
38      handle_ = std::move(other.handle_);
39      version_ = other.version_;
40      other.version_ = 0u;
41    }
42
43    return *this;
44  }
45
46  bool is_valid() const { return handle_.is_valid(); }
47
48  ScopedInterfaceEndpointHandle PassHandle() {
49    return std::move(handle_);
50  }
51  const ScopedInterfaceEndpointHandle& handle() const { return handle_; }
52  void set_handle(ScopedInterfaceEndpointHandle handle) {
53    handle_ = std::move(handle);
54  }
55
56  uint32_t version() const { return version_; }
57  void set_version(uint32_t version) { version_ = version; }
58
59  bool Equals(const AssociatedInterfacePtrInfo& other) const {
60    if (this == &other)
61      return true;
62
63    // Now that the two refer to different objects, they are equivalent if
64    // and only if they are both invalid.
65    return !is_valid() && !other.is_valid();
66  }
67
68 private:
69  ScopedInterfaceEndpointHandle handle_;
70  uint32_t version_;
71
72  DISALLOW_COPY_AND_ASSIGN(AssociatedInterfacePtrInfo);
73};
74
75}  // namespace mojo
76
77#endif  // MOJO_PUBLIC_CPP_BINDINGS_ASSOCIATED_INTERFACE_PTR_INFO_H_
78