RWOpsWrapper.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // Copyright (c) 2008-2017 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. // ATOMIC BEGIN
  25. #include <SDL/include/SDL_rwops.h>
  26. // ATOMIC END
  27. namespace Atomic
  28. {
  29. /// Template wrapper class for using Serializer / Deserializer or their subclasses through SDL's RWOps structure.
  30. template <class T> class RWOpsWrapper
  31. {
  32. public:
  33. /// Construct with object reference.
  34. RWOpsWrapper(T& object)
  35. {
  36. ops_.type = dynamic_cast<File*>(&object) ? SDL_RWOPS_STDFILE : SDL_RWOPS_MEMORY;
  37. ops_.hidden.unknown.data1 = &object;
  38. ops_.size = &Size;
  39. ops_.seek = &Seek;
  40. ops_.close = &Close;
  41. ops_.read = &Read;
  42. ops_.write = &Write;
  43. }
  44. /// Return the RWOps structure.
  45. SDL_RWops* GetRWOps() { return &ops_; }
  46. private:
  47. /// Return data size of the object.
  48. static Sint64 Size(SDL_RWops* context)
  49. {
  50. T* object = reinterpret_cast<T*>(context->hidden.unknown.data1);
  51. Deserializer* des = dynamic_cast<Deserializer*>(object);
  52. return des ? (Sint64)des->GetSize() : 0;
  53. }
  54. /// Seek within the object's data.
  55. static Sint64 Seek(SDL_RWops* context, Sint64 offset, int whence)
  56. {
  57. T* object = reinterpret_cast<T*>(context->hidden.unknown.data1);
  58. Deserializer* des = dynamic_cast<Deserializer*>(object);
  59. if (!des)
  60. return 0;
  61. switch (whence)
  62. {
  63. case RW_SEEK_SET:
  64. des->Seek((unsigned)offset);
  65. break;
  66. case RW_SEEK_CUR:
  67. des->Seek((unsigned)(des->GetPosition() + offset));
  68. break;
  69. case RW_SEEK_END:
  70. des->Seek((unsigned)(des->GetSize() + offset));
  71. break;
  72. default:
  73. assert(false); // Should never reach here
  74. break;
  75. }
  76. return (Sint64)des->GetPosition();
  77. }
  78. /// Close the object. Only meaningful for files, no-op otherwise.
  79. static int Close(SDL_RWops* context)
  80. {
  81. T* object = reinterpret_cast<T*>(context->hidden.unknown.data1);
  82. File* file = dynamic_cast<File*>(object);
  83. if (file)
  84. file->Close();
  85. return 0;
  86. }
  87. /// Read from the object. Return number of "packets" read.
  88. static size_t Read(SDL_RWops* context, void* ptr, size_t size, size_t maxNum)
  89. {
  90. T* object = reinterpret_cast<T*>(context->hidden.unknown.data1);
  91. Deserializer* des = dynamic_cast<Deserializer*>(object);
  92. return des ? (size_t)(des->Read(ptr, (unsigned)(size * maxNum)) / size) : 0;
  93. }
  94. /// Write to the object. Return number of "packets" written.
  95. static size_t Write(SDL_RWops* context, const void* ptr, size_t size, size_t maxNum)
  96. {
  97. T* object = reinterpret_cast<T*>(context->hidden.unknown.data1);
  98. Serializer* ser = dynamic_cast<Serializer*>(object);
  99. return ser ? (size_t)(ser->Write(ptr, (unsigned)(size * maxNum)) / size) : 0;
  100. }
  101. /// SDL RWOps structure associated with the object.
  102. SDL_RWops ops_;
  103. };
  104. }