File: instance_handle.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (75 lines) | stat: -rw-r--r-- 3,182 bytes parent folder | download | duplicates (9)
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
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef PPAPI_CPP_INSTANCE_HANDLE_H_
#define PPAPI_CPP_INSTANCE_HANDLE_H_

#include "ppapi/c/pp_instance.h"


/// @file
/// This file defines an instance handle used to identify an instance in a
/// constructor for a resource.
namespace pp {

class Instance;

/// An instance handle identifies an instance in a constructor for a resource.
/// This class solves two different problems:
///
/// 1. A pp::Instance object's lifetime is managed by the system on the main
/// pepper thread of the module. This means that it may get destroyed at any
/// time based on something that happens on the web page. Therefore, it's not
/// safe to refer to a <code>pp::Instance</code> object on a background thread.
/// Instead, we need to pass some kind of identifier to resource constructors
/// so that they may safely be used on background threads. If the instance
/// becomes invalid, the resource creation will fail on the background thread,
/// but it won't crash.
///
/// 2. <code>PP_Instance</code> would be a good identifier to use for this case.
/// However, using <code>PP_Instance</code> in the constructor to resources is
/// problematic because it is just a typedef for an integer, as is a
/// <code>PP_Resource</code>. Many resources have alternate constructors that
/// just take an existing <code>PP_Resource</code>, so the constructors would
/// be ambiguous. Having this wrapper around a <code>PP_Instance</code>
/// prevents this ambiguity, and also provides a nice place to consolidate an
/// implicit conversion from <code>pp::Instance*</code> for prettier code on
/// the main thread (you can just pass "this" to resource constructors in your
/// instance objects).
///
/// You should always pass an <code>InstanceHandle</code> to background threads
/// instead of a <code>pp::Instance</code>, and use them in resource
/// constructors and code that may be used from background threads.
class InstanceHandle {
 public:
  /// Implicit constructor for converting a <code>pp::Instance</code> to an
  /// instance handle.
  ///
  /// @param[in] instance The instance with which this
  /// <code>InstanceHandle</code> will be associated.
  InstanceHandle(Instance* instance);

  /// This constructor explicitly converts a <code>PP_Instance</code> to an
  /// instance handle. This should not be implicit because it can make some
  /// resource constructors ambiguous. <code>PP_Instance</code> is just a
  /// typedef for an integer, as is <code>PP_Resource</code>, so the compiler
  /// can get confused between the two.
  ///
  /// @param[in] pp_instance The instance with which this
  /// <code>InstanceHandle</code> will be associated.
  explicit InstanceHandle(PP_Instance pp_instance)
      : pp_instance_(pp_instance) {}

  /// The pp_instance() function returns the <code>PP_Instance</code>.
  ///
  /// @return A <code>PP_Instance</code> internal instance handle.
  PP_Instance pp_instance() const { return pp_instance_; }

 private:
  PP_Instance pp_instance_;
};

}  // namespace pp

#endif  // PPAPI_CPP_INSTANCE_HANDLE_H_