summaryrefslogtreecommitdiffstats
path: root/ppapi/shared_impl/id_assignment.h
blob: 039e7fed4ca3f9199cbcbd0e9f44ac4a0c693e65 (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
// Copyright (c) 2011 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 PPAPI_SHARED_IMPL_ID_ASSIGNMENT_H_
#define PPAPI_SHARED_IMPL_ID_ASSIGNMENT_H_

#include <limits>

#include "base/basictypes.h"
#include "ppapi/shared_impl/ppapi_shared_export.h"

namespace ppapi {

enum PPIdType {
  PP_ID_TYPE_MODULE,
  PP_ID_TYPE_INSTANCE,
  PP_ID_TYPE_RESOURCE,
  PP_ID_TYPE_VAR,

  // Not a real type, must be last.
  PP_ID_TYPE_COUNT
};

PPAPI_SHARED_EXPORT extern const unsigned int kPPIdTypeBits;

extern const int32 kMaxPPId;

// The most significant bits are the type, the rest are the value.
template <typename T> inline T MakeTypedId(T value, PPIdType type) {
  return (value << kPPIdTypeBits) | static_cast<T>(type);
}

template <typename T> inline bool CheckIdType(T id, PPIdType type) {
  // Say a resource of 0 is always valid, since that means "no resource."
  // You shouldn't be passing 0 var, instance, or module IDs around so those
  // are still invalid.
  if (type == PP_ID_TYPE_RESOURCE && !id)
    return true;
  const T mask = (static_cast<T>(1) << kPPIdTypeBits) - 1;
  return (id & mask) == type;
}

}  // namespace ppapi

#endif  // PPAPI_SHARED_IMPL_ID_ASSIGNMENT_H_