RWOpsWrapper.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../IO/File.h"
  5. #include <SDL/SDL_rwops.h>
  6. namespace Urho3D
  7. {
  8. /// Template wrapper class for using Serializer / Deserializer or their subclasses through SDL's RWOps structure.
  9. template <class T> class RWOpsWrapper
  10. {
  11. public:
  12. /// Construct with object reference.
  13. explicit RWOpsWrapper(T& object)
  14. {
  15. ops_.type = dynamic_cast<File*>(&object) ? SDL_RWOPS_STDFILE : SDL_RWOPS_MEMORY;
  16. ops_.hidden.unknown.data1 = &object;
  17. ops_.size = &Size;
  18. ops_.seek = &Seek;
  19. ops_.close = &Close;
  20. ops_.read = &Read;
  21. ops_.write = &Write;
  22. }
  23. /// Return the RWOps structure.
  24. SDL_RWops* GetRWOps() { return &ops_; }
  25. private:
  26. /// Return data size of the object.
  27. static Sint64 Size(SDL_RWops* context)
  28. {
  29. auto* object = reinterpret_cast<T*>(context->hidden.unknown.data1);
  30. auto* des = dynamic_cast<Deserializer*>(object);
  31. return des ? (Sint64)des->GetSize() : 0;
  32. }
  33. /// Seek within the object's data.
  34. static Sint64 Seek(SDL_RWops* context, Sint64 offset, int whence)
  35. {
  36. auto* object = reinterpret_cast<T*>(context->hidden.unknown.data1);
  37. auto* des = dynamic_cast<Deserializer*>(object);
  38. if (!des)
  39. return 0;
  40. switch (whence)
  41. {
  42. case RW_SEEK_SET:
  43. des->Seek((unsigned)offset);
  44. break;
  45. case RW_SEEK_CUR:
  46. des->Seek((unsigned)(des->GetPosition() + offset));
  47. break;
  48. case RW_SEEK_END:
  49. des->Seek((unsigned)(des->GetSize() + offset));
  50. break;
  51. default:
  52. assert(false); // Should never reach here
  53. break;
  54. }
  55. return (Sint64)des->GetPosition();
  56. }
  57. /// Close the object. Only meaningful for files, no-op otherwise.
  58. static int Close(SDL_RWops* context)
  59. {
  60. auto* object = reinterpret_cast<T*>(context->hidden.unknown.data1);
  61. auto* file = dynamic_cast<File*>(object);
  62. if (file)
  63. file->Close();
  64. return 0;
  65. }
  66. /// Read from the object. Return number of "packets" read.
  67. static size_t Read(SDL_RWops* context, void* ptr, size_t size, size_t maxNum)
  68. {
  69. auto* object = reinterpret_cast<T*>(context->hidden.unknown.data1);
  70. auto* des = dynamic_cast<Deserializer*>(object);
  71. return des ? (size_t)(des->Read(ptr, (unsigned)(size * maxNum)) / size) : 0;
  72. }
  73. /// Write to the object. Return number of "packets" written.
  74. static size_t Write(SDL_RWops* context, const void* ptr, size_t size, size_t maxNum)
  75. {
  76. auto* object = reinterpret_cast<T*>(context->hidden.unknown.data1);
  77. auto* ser = dynamic_cast<Serializer*>(object);
  78. return ser ? (size_t)(ser->Write(ptr, (unsigned)(size * maxNum)) / size) : 0;
  79. }
  80. /// SDL RWOps structure associated with the object.
  81. SDL_RWops ops_;
  82. };
  83. }