NativeBuffer.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* -*- mode: c; tab-width: 4; c-basic-offset: 4; c-file-style: "linux" -*- */
  2. //
  3. // NativeBuffer.h: UWP support library for SDLPal.
  4. // Author: Lou Yihua @ 2017
  5. //
  6. // Copyright (c) 2009-2011, Wei Mingzhi <whistler_wmz@users.sf.net>.
  7. // Copyright (c) 2011-2017 SDLPAL development team.
  8. // All rights reserved.
  9. //
  10. // This file is part of SDLPAL.
  11. //
  12. // SDLPAL is free software: you can redistribute it and/or modify
  13. // it under the terms of the GNU General Public License as published by
  14. // the Free Software Foundation, either version 3 of the License, or
  15. // (at your option) any later version.
  16. //
  17. // This program is distributed in the hope that it will be useful,
  18. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. // GNU General Public License for more details.
  21. //
  22. // You should have received a copy of the GNU General Public License
  23. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. //
  25. #pragma once
  26. #include <wrl.h>
  27. #include <wrl/implements.h>
  28. #include <windows.storage.streams.h>
  29. #include <robuffer.h>
  30. #include <stdint.h>
  31. class NativeBuffer :
  32. public Microsoft::WRL::RuntimeClass<Microsoft::WRL::RuntimeClassFlags<Microsoft::WRL::RuntimeClassType::WinRtClassicComMix>,
  33. ABI::Windows::Storage::Streams::IBuffer,
  34. Windows::Storage::Streams::IBufferByteAccess>
  35. {
  36. public:
  37. virtual ~NativeBuffer()
  38. {
  39. }
  40. STDMETHODIMP RuntimeClassInitialize(byte *buffer, UINT totalSize)
  41. {
  42. m_length = totalSize;
  43. m_buffer = buffer;
  44. return S_OK;
  45. }
  46. STDMETHODIMP Buffer(byte **value)
  47. {
  48. *value = m_buffer;
  49. return S_OK;
  50. }
  51. STDMETHODIMP get_Capacity(UINT32 *value)
  52. {
  53. *value = m_length;
  54. return S_OK;
  55. }
  56. STDMETHODIMP get_Length(UINT32 *value)
  57. {
  58. *value = m_length;
  59. return S_OK;
  60. }
  61. STDMETHODIMP put_Length(UINT32 value)
  62. {
  63. m_length = value;
  64. return S_OK;
  65. }
  66. static Windows::Storage::Streams::IBuffer^ GetIBuffer(byte *buffer, uint32_t totalSize)
  67. {
  68. Microsoft::WRL::ComPtr<NativeBuffer> nativeBuffer;
  69. Microsoft::WRL::Details::MakeAndInitialize<NativeBuffer>(&nativeBuffer, buffer, totalSize);
  70. auto obj = reinterpret_cast<IInspectable*>(nativeBuffer.Get());
  71. return reinterpret_cast<Windows::Storage::Streams::IBuffer^>(obj);
  72. }
  73. private:
  74. UINT32 m_length;
  75. byte *m_buffer;
  76. };