fork(1) download
  1. #include <iostream>
  2. #include <string>
  3. #include <utility>
  4.  
  5. struct Image { std::string id; };
  6.  
  7. struct ogl_backend
  8. {
  9. typedef unsigned texture_handle_type;
  10.  
  11. void load(texture_handle_type& texture, const Image& image)
  12. {
  13. std::cout << "loading, " << image.id << '\n';
  14. }
  15.  
  16. void destroy(texture_handle_type& texture)
  17. {
  18. std::cout << "destroying texture\n";
  19. }
  20. };
  21.  
  22. template <class GraphicsBackend>
  23. struct texture_gpu_resource
  24. {
  25. typedef GraphicsBackend graphics_backend;
  26. typedef typename GraphicsBackend::texture_handle_type texture_handle;
  27.  
  28. texture_gpu_resource(graphics_backend& backend)
  29. : _backend(backend)
  30. {
  31. }
  32.  
  33. ~texture_gpu_resource()
  34. {
  35. // should check if it is a valid handle first
  36. _backend.destroy(_handle);
  37. }
  38.  
  39. void load(const Image& image)
  40. {
  41. _backend.load(_handle, image);
  42. }
  43.  
  44. const texture_handle& handle() const
  45. {
  46. return _handle;
  47. }
  48.  
  49. private:
  50.  
  51. graphics_backend& _backend;
  52. texture_handle _handle;
  53. };
  54.  
  55.  
  56. template <typename GraphicBackend>
  57. class graphics_device
  58. {
  59. typedef graphics_device<GraphicBackend> this_type;
  60.  
  61. public:
  62.  
  63. typedef texture_gpu_resource<GraphicBackend> texture;
  64.  
  65. template <typename... Args>
  66. texture createTexture(Args&&... args)
  67. {
  68. return texture{_backend, std::forward(args)...};
  69. }
  70.  
  71. template <typename Resource, typename... Args>
  72. Resource create(Args&&... args)
  73. {
  74. return Resource{_backend, std::forward(args)...};
  75. }
  76.  
  77. private:
  78.  
  79. GraphicBackend _backend;
  80. };
  81.  
  82.  
  83. class ogl_graphics_device : public graphics_device<ogl_backend>
  84. {
  85. public:
  86.  
  87. enum class feature
  88. {
  89. texturing
  90. };
  91.  
  92. void enableFeature(feature f)
  93. {
  94. std::cout << "enabling feature... " << (int)f << '\n';
  95. }
  96. };
  97.  
  98.  
  99. // or...
  100. // typedef graphics_device<ogl_backend> ogl_graphics_device
  101.  
  102.  
  103. int main()
  104. {
  105. ogl_graphics_device device;
  106.  
  107. device.enableFeature(ogl_graphics_device::feature::texturing);
  108.  
  109. auto texture = device.create<decltype(device)::texture>();
  110.  
  111. texture.load({"hello"});
  112.  
  113. return 0;
  114. }
  115.  
  116. /*
  117.  
  118.  Expected output:
  119.   enabling feature... 0
  120.   loading, hello
  121.   destroying texture
  122.  
  123. */
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
enabling feature... 0
loading, hello
destroying texture