summaryrefslogtreecommitdiffstats
path: root/mojo/services/view_manager/ids.h
blob: 71e38332a3482a6d3c1c041615dac16e73cdcfe8 (plain)
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
// Copyright 2014 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 MOJO_SERVICES_VIEW_MANAGER_IDS_H_
#define MOJO_SERVICES_VIEW_MANAGER_IDS_H_

#include "mojo/services/public/cpp/view_manager/types.h"
#include "mojo/services/public/cpp/view_manager/util.h"
#include "mojo/services/view_manager/view_manager_export.h"

namespace mojo {
namespace service {

// Connection id is used to indicate no connection. That is, no
// ViewManagerServiceImpl ever gets this id.
const ConnectionSpecificId kInvalidConnectionId = 0;

// TODO(sky): remove this, temporary while window manager API is in existing
// api.
const ConnectionSpecificId kWindowManagerConnection = 1;

// Adds a bit of type safety to node ids.
struct MOJO_VIEW_MANAGER_EXPORT NodeId {
  NodeId(ConnectionSpecificId connection_id, ConnectionSpecificId node_id)
      : connection_id(connection_id),
        node_id(node_id) {}
  NodeId() : connection_id(0), node_id(0) {}

  bool operator==(const NodeId& other) const {
    return other.connection_id == connection_id &&
        other.node_id == node_id;
  }

  bool operator!=(const NodeId& other) const {
    return !(*this == other);
  }

  ConnectionSpecificId connection_id;
  ConnectionSpecificId node_id;
};

// Adds a bit of type safety to view ids.
struct MOJO_VIEW_MANAGER_EXPORT ViewId {
  ViewId(ConnectionSpecificId connection_id, ConnectionSpecificId view_id)
      : connection_id(connection_id),
        view_id(view_id) {}
  ViewId() : connection_id(0), view_id(0) {}

  bool operator==(const ViewId& other) const {
    return other.connection_id == connection_id &&
        other.view_id == view_id;
  }

  bool operator!=(const ViewId& other) const {
    return !(*this == other);
  }

  ConnectionSpecificId connection_id;
  ConnectionSpecificId view_id;
};

// Functions for converting to/from structs and transport values.
inline NodeId NodeIdFromTransportId(Id id) {
  return NodeId(HiWord(id), LoWord(id));
}

inline Id NodeIdToTransportId(const NodeId& id) {
  return (id.connection_id << 16) | id.node_id;
}

inline ViewId ViewIdFromTransportId(Id id) {
  return ViewId(HiWord(id), LoWord(id));
}

inline Id ViewIdToTransportId(const ViewId& id) {
  return (id.connection_id << 16) | id.view_id;
}

inline NodeId RootNodeId() {
  return NodeId(kInvalidConnectionId, 1);
}

// Returns a NodeId that is reserved to indicate no node. That is, no node will
// ever be created with this id.
inline NodeId InvalidNodeId() {
  return NodeId(kInvalidConnectionId, 0);
}

}  // namespace service
}  // namespace mojo

#endif  // MOJO_SERVICES_VIEW_MANAGER_IDS_H_