File: StructShared.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 (176 lines) | stat: -rw-r--r-- 4,496 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Copyright 2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include <cstdlib>
#include <new>
#include <type_traits>

#include "rkcommon/common.h"
#include "rkcommon/math/rkmath.h"
#include "rkcommon/math/vec.h"

#include "DeviceRT.h"

namespace ispc {

// Shared structure members may use types from rkcommon
using namespace rkcommon;
using namespace rkcommon::math;

// Shared structure members may use ispc specific types
using uint8 = uint8_t;
using int32 = int32_t;
using uint32 = uint32_t;
using int64 = int64_t;

static_assert(sizeof(bool) == 1,
    "Sharing structs with ISPC not possible, because different sizes of 'bool'.");

} // namespace ispc

namespace ospray {

/*
   Usage:
    derive from AddStructShared<cpp::Base, ispc::StructShared>

   We use multiple inheritance with a virtual base class, thus only a single
   instance of StructSharedPtr is present, which will be initialized first.
   StructSharedGet adds getSh returning the correctly typed pointer. It is
   derived from first to handle the memory allocation with the maximum size of
   the final StructShared. StructSharedGet does not have any data to ensure
   final classes can be C-style casted to ManagedObject* (this pointer stays
   the same).
*/

template <typename T>
inline T *StructSharedCreate(devicert::Device &device)
{
  return new (device.sharedMalloc(sizeof(T))) T;
}

struct StructSharedPtr
{
  StructSharedPtr() : _device(nullptr), _ptr(nullptr) {}
  ~StructSharedPtr();

  template <typename, typename>
  friend struct StructSharedGet;

  template <typename, typename>
  friend struct AddStructShared;

 private:
  devicert::Device *_device;
  void *_ptr;
};

template <typename T, typename>
struct StructSharedGet
{
  StructSharedGet(devicert::Device &, devicert::Device **, void **);
  T *getSh() const;
};

// Traits /////////////////////////////////////////////////////

template <class...>
struct make_void
{
  using type = void;
};

template <typename... T>
using void_t = typename make_void<T...>::type;

template <typename, typename S, typename = void>
struct get_base_structshared_or
{
  using type = S;
};

template <typename T, typename S>
struct get_base_structshared_or<T, S, void_t<typename T::StructShared_t>>
{
  using type = typename T::StructShared_t;
};

template <typename S, typename = void>
struct get_super_or
{
  using type = S;
};

template <typename S>
struct get_super_or<S, void_t<decltype(S::super)>>
{
  using type = decltype(S::super);
};

// AddStructShared ////////////////////////////////////////////

template <typename Base, typename Struct>
struct AddStructShared
    : public StructSharedGet<Struct, AddStructShared<Base, Struct>>,
      public Base,
      public virtual StructSharedPtr
{
  using StructShared_t = Struct;
  using StructSharedGet<Struct, AddStructShared<Base, Struct>>::getSh;
  static_assert(
      std::is_same<typename get_base_structshared_or<Base, Struct>::type,
          typename get_super_or<Struct>::type>::value,
      "StructShared_t needs to have 'super' member of type Base::StructShared_t");

  template <typename... Args>
  AddStructShared(devicert::Device &device, Args &&...args)
      : StructSharedGet<Struct, AddStructShared<Base, Struct>>(
          device, &_device, &_ptr),
        Base(std::forward<Args>(args)...)
  {}
};

// Inlined definitions ////////////////////////////////////////

inline StructSharedPtr::~StructSharedPtr()
{
  _device->free(_ptr);
}

template <typename T, typename B>
StructSharedGet<T, B>::StructSharedGet(
    devicert::Device &device, devicert::Device **_device, void **_ptr)
{
  if (!*_ptr) {
    *_ptr = (void *)StructSharedCreate<T>(device);
    *_device = &device;
  }
}

template <typename T, typename B>
T *StructSharedGet<T, B>::getSh() const
{
  return static_cast<T *>(static_cast<const B *>(this)->_ptr);
}

// Testing ////////////////////////////////////////////////////
namespace test {
// clang-format off
struct A {};
struct B { char b; };
struct D1 { B super; };
struct D2 { D1 super; char c; };

struct ShouldPass1 : public AddStructShared<A, B> {};
struct ShouldPass2 : public AddStructShared<ShouldPass1, D1> {};
struct ShouldPass3 : public AddStructShared<ShouldPass2, D2> {};

//struct ShouldFail1 : public AddStructShared<ShouldPass3, D2> {};
//struct ShouldFail2 : public AddStructShared<ShouldPass2, B> {};
//struct ShouldFail3 : public AddStructShared<ShouldPass3, D1> {};
//  clang-format on
} // namespace test

} // namespace ospray