IOBuffer.hpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /***************************************************************************
  2. *
  3. * Project _____ __ ____ _ _
  4. * ( _ ) /__\ (_ _)_| |_ _| |_
  5. * )(_)( /(__)\ )( (_ _)(_ _)
  6. * (_____)(__)(__)(__) |_| |_|
  7. *
  8. *
  9. * Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com>
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. *
  23. ***************************************************************************/
  24. #ifndef oatpp_data_buffer_IOBuffer_hpp
  25. #define oatpp_data_buffer_IOBuffer_hpp
  26. #include "oatpp/core/base/memory/ObjectPool.hpp"
  27. #include "oatpp/core/base/Countable.hpp"
  28. namespace oatpp { namespace data{ namespace buffer {
  29. /**
  30. * Predefined buffer implementation for I/O operations.
  31. * Allocates buffer bytes using &id:oatpp::base::memory::ThreadDistributedMemoryPool;.
  32. */
  33. class IOBuffer : public oatpp::base::Countable {
  34. public:
  35. OBJECT_POOL(IOBuffer_Pool, IOBuffer, 32)
  36. SHARED_OBJECT_POOL(Shared_IOBuffer_Pool, IOBuffer, 32)
  37. public:
  38. /**
  39. * Buffer size constant.
  40. */
  41. static constexpr v_buff_size BUFFER_SIZE = 4096;
  42. private:
  43. static oatpp::base::memory::ThreadDistributedMemoryPool& getBufferPool() {
  44. static auto pool = new oatpp::base::memory::ThreadDistributedMemoryPool("IOBuffer_Buffer_Pool", BUFFER_SIZE, 16);
  45. return *pool;
  46. }
  47. private:
  48. void* m_entry;
  49. public:
  50. /**
  51. * Constructor.
  52. */
  53. IOBuffer();
  54. public:
  55. /**
  56. * Create shared IOBuffer.
  57. * @return
  58. */
  59. static std::shared_ptr<IOBuffer> createShared();
  60. /**
  61. * Virtual destructor.
  62. */
  63. ~IOBuffer();
  64. /**
  65. * Get pointer to buffer data.
  66. * @return
  67. */
  68. void* getData();
  69. /**
  70. * Get buffer size.
  71. * @return - should always return &l:IOBuffer::BUFFER_SIZE;.
  72. */
  73. v_buff_size getSize();
  74. };
  75. }}}
  76. #endif /* oatpp_data_buffer_IOBuffer_hpp */