RWOpsWrapper.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // Copyright (c) 2008-2014 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 "File.h"
  24. #include <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. 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. T* object = reinterpret_cast<T*>(context->hidden.unknown.data1);
  49. Deserializer* des = dynamic_cast<Deserializer*>(object);
  50. return des ? des->GetSize() : 0;
  51. }
  52. /// Seek within the object's data.
  53. static Sint64 Seek(SDL_RWops* context, Sint64 offset, int whence)
  54. {
  55. T* object = reinterpret_cast<T*>(context->hidden.unknown.data1);
  56. Deserializer* 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. }
  71. return (Sint64)des->GetPosition();
  72. }
  73. /// Close the object. Only meaningful for files, no-op otherwise.
  74. static int Close(SDL_RWops* context)
  75. {
  76. T* object = reinterpret_cast<T*>(context->hidden.unknown.data1);
  77. File* file = dynamic_cast<File*>(object);
  78. if (file)
  79. file->Close();
  80. return 0;
  81. }
  82. /// Read from the object. Return number of "packets" read.
  83. static size_t Read(SDL_RWops* context, void* ptr, size_t size, size_t maxNum)
  84. {
  85. T* object = reinterpret_cast<T*>(context->hidden.unknown.data1);
  86. Deserializer* des = dynamic_cast<Deserializer*>(object);
  87. return des ? des->Read(ptr, size * maxNum) / size : 0;
  88. }
  89. /// Write to the object. Return number of "packets" written.
  90. static size_t Write(SDL_RWops* context, const void* ptr, size_t size, size_t maxNum)
  91. {
  92. T* object = reinterpret_cast<T*>(context->hidden.unknown.data1);
  93. Serializer* ser = dynamic_cast<Serializer*>(object);
  94. return ser ? ser->Write(ptr, size * maxNum) / size : 0;
  95. }
  96. /// SDL RWOps structure associated with the object.
  97. SDL_RWops ops_;
  98. };
  99. }