RWOpsWrapper.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../IO/File.h"
  24. #include <SDL/SDL_rwops.h>
  25. namespace Urho3D
  26. {
  27. /// Template wrapper class for using Serializer / Deserializer or their subclasses through SDL's RWOps structure.
  28. template <class T> class RWOpsWrapper
  29. {
  30. public:
  31. /// Construct with object reference.
  32. explicit RWOpsWrapper(T& object)
  33. {
  34. ops_.type = dynamic_cast<File*>(&object) ? SDL_RWOPS_STDFILE : SDL_RWOPS_MEMORY;
  35. ops_.hidden.unknown.data1 = &object;
  36. ops_.size = &Size;
  37. ops_.seek = &Seek;
  38. ops_.close = &Close;
  39. ops_.read = &Read;
  40. ops_.write = &Write;
  41. }
  42. /// Return the RWOps structure.
  43. SDL_RWops* GetRWOps() { return &ops_; }
  44. private:
  45. /// Return data size of the object.
  46. static Sint64 Size(SDL_RWops* context)
  47. {
  48. auto* object = reinterpret_cast<T*>(context->hidden.unknown.data1);
  49. auto* des = dynamic_cast<Deserializer*>(object);
  50. return des ? (Sint64)des->GetSize() : 0;
  51. }
  52. /// Seek within the object's data.
  53. static Sint64 Seek(SDL_RWops* context, Sint64 offset, int whence)
  54. {
  55. auto* object = reinterpret_cast<T*>(context->hidden.unknown.data1);
  56. auto* des = dynamic_cast<Deserializer*>(object);
  57. if (!des)
  58. return 0;
  59. switch (whence)
  60. {
  61. case RW_SEEK_SET:
  62. des->Seek((unsigned)offset);
  63. break;
  64. case RW_SEEK_CUR:
  65. des->Seek((unsigned)(des->GetPosition() + offset));
  66. break;
  67. case RW_SEEK_END:
  68. des->Seek((unsigned)(des->GetSize() + offset));
  69. break;
  70. default:
  71. assert(false); // Should never reach here
  72. break;
  73. }
  74. return (Sint64)des->GetPosition();
  75. }
  76. /// Close the object. Only meaningful for files, no-op otherwise.
  77. static int Close(SDL_RWops* context)
  78. {
  79. auto* object = reinterpret_cast<T*>(context->hidden.unknown.data1);
  80. auto* file = dynamic_cast<File*>(object);
  81. if (file)
  82. file->Close();
  83. return 0;
  84. }
  85. /// Read from the object. Return number of "packets" read.
  86. static size_t Read(SDL_RWops* context, void* ptr, size_t size, size_t maxNum)
  87. {
  88. auto* object = reinterpret_cast<T*>(context->hidden.unknown.data1);
  89. auto* des = dynamic_cast<Deserializer*>(object);
  90. return des ? (size_t)(des->Read(ptr, (unsigned)(size * maxNum)) / size) : 0;
  91. }
  92. /// Write to the object. Return number of "packets" written.
  93. static size_t Write(SDL_RWops* context, const void* ptr, size_t size, size_t maxNum)
  94. {
  95. auto* object = reinterpret_cast<T*>(context->hidden.unknown.data1);
  96. auto* ser = dynamic_cast<Serializer*>(object);
  97. return ser ? (size_t)(ser->Write(ptr, (unsigned)(size * maxNum)) / size) : 0;
  98. }
  99. /// SDL RWOps structure associated with the object.
  100. SDL_RWops ops_;
  101. };
  102. }