Flutter Engine
embedder_safe_access.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef FLUTTER_SHELL_PLATFORM_EMBEDDER_EMBEDDER_SAFE_ACCESS_H_
6 #define FLUTTER_SHELL_PLATFORM_EMBEDDER_EMBEDDER_SAFE_ACCESS_H_
7 
8 #include <type_traits>
9 
10 #define SAFE_ACCESS(pointer, member, default_value) \
11  ([=]() { \
12  if (offsetof(std::remove_pointer<decltype(pointer)>::type, member) + \
13  sizeof(pointer->member) <= \
14  pointer->struct_size) { \
15  return pointer->member; \
16  } \
17  return static_cast<decltype(pointer->member)>((default_value)); \
18  })()
19 
20 /// Checks if the member exists.
21 #define SAFE_EXISTS(pointer, member) \
22  (SAFE_ACCESS(pointer, member, nullptr) != nullptr)
23 
24 /// Checks if exactly one of member1 or member2 exists.
25 #define SAFE_EXISTS_ONE_OF(pointer, member1, member2) \
26  (SAFE_EXISTS(pointer, member1) != SAFE_EXISTS(pointer, member2))
27 
28 #endif // FLUTTER_SHELL_PLATFORM_EMBEDDER_EMBEDDER_SAFE_ACCESS_H_