File: ObjectHandle.h

package info (click to toggle)
ospray 3.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,048 kB
  • sloc: cpp: 80,569; ansic: 951; sh: 805; makefile: 170; python: 69
file content (95 lines) | stat: -rw-r--r-- 2,556 bytes parent folder | download | duplicates (2)
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
// Copyright 2009 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include "MPICommon.h"
#include "common/OSPCommon.h"
#include "ospray_mpi_common_export.h"
#include "rkcommon/memory/RefCount.h"

namespace ospray {

#define NULL_HANDLE (ObjectHandle(0))

//! (local) handle to a (remote) managed object
/*! abstraction for a remotely-held 'managed object'. the handle
  refers to both 'owner' (the machine that has it) as well as to a
  local ID (by which that owner can look it up). Note that other
  ranks may also have copies of that object.

  note that the 'null handle' is '0', not -1. This allows an app
  to test the handled resturend from ospNewXXX calls for null just
  as if they were pointers (and thus, 'null' objects are
  consistent between local and mpi rendering)
*/
union OSPRAY_MPI_COMMON_EXPORT ObjectHandle
{
  ObjectHandle();
  ObjectHandle(int64 i);
  ObjectHandle(const ObjectHandle &other);
  ObjectHandle &operator=(const ObjectHandle &other);

  void free();

  /*! look up an object by handle, and return it. must be a defined handle */
  memory::RefCount *lookup() const;

  /* Allocate a local ObjectHandle */
  static ObjectHandle allocateLocalHandle();

  /*! Return the handle associated with the given object. */
  static ObjectHandle lookup(memory::RefCount *object);

  /*! check whether the handle is defined *on this rank* */
  bool defined() const;

  /*! define the given handle to refer to given object */
  static void assign(const ObjectHandle &handle, memory::RefCount *object);

  /*! define the given handle to refer to given object */
  void assign(memory::RefCount *object) const;

  void freeObject() const;

  int32 ownerRank() const;
  int32 objID() const;

  /*! cast to int64 to allow fast operations with this type */
  operator int64() const;

  // Data members //

  struct
  {
    int32 ID;
    int32 owner;
  } i32;

  int64 i64;
};

OSPRAY_MPI_COMMON_EXPORT extern const ObjectHandle nullHandle;

// Inlined operator definitions /////////////////////////////////////////////

OSPRAY_MPI_COMMON_EXPORT inline bool operator==(
    const ObjectHandle &a, const ObjectHandle &b)
{
  return a.i64 == b.i64;
}

OSPRAY_MPI_COMMON_EXPORT inline bool operator!=(
    const ObjectHandle &a, const ObjectHandle &b)
{
  return !(a == b);
}

template <typename OSPRAY_TYPE>
inline OSPRAY_TYPE *lookupObject(OSPObject obj)
{
  auto &handle = reinterpret_cast<ObjectHandle &>(obj);
  return handle.defined() ? (OSPRAY_TYPE *)handle.lookup() : (OSPRAY_TYPE *)obj;
}

} // namespace ospray