this repo has no description
1// Copyright (c) Facebook, Inc. and its affiliates. (http://www.facebook.com)
2#include "handles.h"
3
4#include "objects.h"
5#include "runtime.h"
6
7namespace py {
8
9// do_type_check<T> is a helper struct to perform an exact or subtype check,
10// based on which handle list contains T.
11namespace {
12template <typename T>
13struct do_type_check;
14#define EXACT_TYPE_CHECK(ty) \
15 template <> \
16 struct do_type_check<Raw##ty> { \
17 bool operator()(Thread*, RawObject obj) const { return obj.is##ty(); } \
18 };
19HANDLE_TYPES(EXACT_TYPE_CHECK)
20#undef EXACT_TYPE_CHECK
21
22#define SUBTYPE_CHECK(ty) \
23 template <> \
24 struct do_type_check<Raw##ty> { \
25 bool operator()(Thread* thread, RawObject obj) const { \
26 return thread->runtime()->isInstanceOf##ty(obj); \
27 } \
28 };
29SUBTYPE_HANDLE_TYPES(SUBTYPE_CHECK)
30#undef SUBTYPE_CHECK
31} // namespace
32
33// This function needs Runtime's definition for the subtype case, which we
34// can't use in handles.h due to a circular dependency. Define it in here for
35// now, and manually instantiate Handle as needed.
36template <typename T>
37bool Handle<T>::isValidType() const {
38 return do_type_check<T>()(Thread::current(), **this);
39}
40
41#define INSTANTIATE(ty) template class Handle<Raw##ty>;
42HANDLE_TYPES(INSTANTIATE)
43SUBTYPE_HANDLE_TYPES(INSTANTIATE)
44#undef INSTANTIATE
45
46} // namespace py