IOBuffer.hpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 std::once_flag flag;
  45. static oatpp::base::memory::ThreadDistributedMemoryPool *pool = nullptr;
  46. std::call_once(flag, []() {
  47. pool = new oatpp::base::memory::ThreadDistributedMemoryPool("IOBuffer_Buffer_Pool", BUFFER_SIZE, 16);
  48. });
  49. return *pool;
  50. }
  51. private:
  52. void* m_entry;
  53. public:
  54. /**
  55. * Constructor.
  56. */
  57. IOBuffer();
  58. public:
  59. /**
  60. * Create shared IOBuffer.
  61. * @return
  62. */
  63. static std::shared_ptr<IOBuffer> createShared();
  64. /**
  65. * Virtual destructor.
  66. */
  67. ~IOBuffer();
  68. /**
  69. * Get pointer to buffer data.
  70. * @return
  71. */
  72. void* getData();
  73. /**
  74. * Get buffer size.
  75. * @return - should always return &l:IOBuffer::BUFFER_SIZE;.
  76. */
  77. v_buff_size getSize();
  78. };
  79. }}}
  80. #endif /* oatpp_data_buffer_IOBuffer_hpp */