MapLibre Native Core
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
shader.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string_view>
4 #include <type_traits>
5 
6 namespace mbgl {
7 namespace gfx {
8 
9 class Shader;
10 
11 // Assert that a type is a valid shader for downcasting.
12 // A valid shader must:
13 // * Inherit gfx::Shader
14 // * Declare a public, unique type name (string_view T::Name)
15 // * Be a final class
16 template<typename T>
17 inline constexpr bool is_shader_v =
18  std::is_base_of_v<gfx::Shader, T> &&
19  std::is_same_v<
20  std::remove_cv_t<decltype(T::Name)>,
21  std::string_view> &&
22  std::is_final_v<T>;
23 
24 
27 class Shader {
28  public:
29  virtual ~Shader() = default;
30 
33  virtual const std::string_view typeName() const noexcept = 0;
34 
38  template<typename T,
39  typename std::enable_if_t<is_shader_v<T>, bool>* = nullptr>
40  T* to() noexcept {
41  if (typeName() != T::Name) {
42  return nullptr;
43  }
44  return static_cast<T*>(this);
45  }
46 };
47 
48 } // namespace gfx
49 } // namespace mbgl
A shader is used as the base class for all programs across any supported backend API....
Definition: shader.hpp:27
virtual const std::string_view typeName() const noexcept=0
Get the type name of this shader.
T * to() noexcept
Downcast to a type.
Definition: shader.hpp:40
virtual ~Shader()=default
constexpr bool is_shader_v
Definition: shader.hpp:17
Definition: actor.hpp:15
Definition: tile_id.hpp:256