Boost 循环缓冲区指针访问 (c++)
Boost Circular Buffer pointer access (c++)
我想使用 Boost 循环缓冲区来存储由硬件生成的数组 API。 API 接受内存位置的地址并相应地推送数组。所以我有以下内容:
typedef unsigned char API_data [10];
boost::circular_buffer<API_data> data(10);
boost::circular_buffer<API_data>::iterator it = data.begin();
但是我无法将指针 it
传递给 API 因为:
no suitable conversion function from "boost::cb_details::iterator<boost::circular_buffer<API_data, std::allocator<API_data>>, boost::cb_details::nonconst_traits<boost::container::allocator_traits<std::allocator<API_data>>>>
" to LPVOID
exists.
API 需要一个 LPVOID
类型的指针,但 it
指针是不同类型的。
循环缓冲区API类似于::std::vector
API。但是有一个问题!首先,存储在循环缓冲区中的项目必须是可复制分配的,因此您不能在其中存储原始数组。其次,您在构造函数中提供容器容量,而不是初始容器大小,因此在尝试将指针传递给存储的项目之前,您需要确保它在那里。如果缓冲区已满,则推入循环缓冲区可能会丢弃最旧的项目,这与 std::vector
将始终增长不同。然后你可以获得对推送项的引用并将其转换为指向 void 的指针。
using API_data_buffer = ::std::array< unsigned char, 10 >;
::boost::circular_buffer< API_data_buffer > buffers(10); // buffers is still empty!
buffers.push_back();
auto & api_data_buffer{buffers.back()};
auto const p_void_api_data{reinterpret_cast< void * >(reinterpret_cast< ::std::uintptr_t >(api_data_buffer.data()))};
我想使用 Boost 循环缓冲区来存储由硬件生成的数组 API。 API 接受内存位置的地址并相应地推送数组。所以我有以下内容:
typedef unsigned char API_data [10];
boost::circular_buffer<API_data> data(10);
boost::circular_buffer<API_data>::iterator it = data.begin();
但是我无法将指针 it
传递给 API 因为:
no suitable conversion function from "
boost::cb_details::iterator<boost::circular_buffer<API_data, std::allocator<API_data>>, boost::cb_details::nonconst_traits<boost::container::allocator_traits<std::allocator<API_data>>>>
" toLPVOID
exists.
API 需要一个 LPVOID
类型的指针,但 it
指针是不同类型的。
循环缓冲区API类似于::std::vector
API。但是有一个问题!首先,存储在循环缓冲区中的项目必须是可复制分配的,因此您不能在其中存储原始数组。其次,您在构造函数中提供容器容量,而不是初始容器大小,因此在尝试将指针传递给存储的项目之前,您需要确保它在那里。如果缓冲区已满,则推入循环缓冲区可能会丢弃最旧的项目,这与 std::vector
将始终增长不同。然后你可以获得对推送项的引用并将其转换为指向 void 的指针。
using API_data_buffer = ::std::array< unsigned char, 10 >;
::boost::circular_buffer< API_data_buffer > buffers(10); // buffers is still empty!
buffers.push_back();
auto & api_data_buffer{buffers.back()};
auto const p_void_api_data{reinterpret_cast< void * >(reinterpret_cast< ::std::uintptr_t >(api_data_buffer.data()))};