Reputation: 1698
Refering to https://github.com/cameron314/readerwriterqueue , there is a sample code like following :
ReaderWriterQueue<int> q(100); // Reserve space for at least 100 elements up front
q.enqueue(17); // Will allocate memory if the queue is full
bool succeeded = q.try_enqueue(18); // Will only succeed if the queue has an empty slot (never allocates)
assert(succeeded);
int number;
succeeded = q.try_dequeue(number); // Returns false if the queue was empty
assert(succeeded && number == 17);
// You can also peek at the front item of the queue (consumer only)
int* front = q.peek();
assert(*front == 18);
succeeded = q.try_dequeue(number);
assert(succeeded && number == 18);
front = q.peek();
assert(front == nullptr); // Returns nullptr if the queue was empty
Suppose I like to have a array for ReaderWriterQueue constructed with parameter 100 , how can I define the var ?!
ReaderWriterQueue<int> qp[1024] ;
that is ok , but I like all 1024 of them with parameter 100 ,
ReaderWriterQueue<int>(100) qp[1024] ;
won't compiled , I try to use pointer :
ReaderWriterQueue<int>* qptr;
qptr = new ReaderWriterQueue<int>(1024) ;
will work , but without parameter 100 ,
qptr = new ReaderWriterQueue<int>(100) (1024) ;
won't compile , so how can I make an array of 1024's ReaderWriterQueue , all of them constructed with parameter 100 ?!
Upvotes: 0
Views: 298
Reputation: 217283
As ReaderWriterQueue
is neither copy-able/movable, you may use static_vector
from http://en.cppreference.com/w/cpp/types/aligned_storage
template<class T, std::size_t N>
class static_vector
{
// properly aligned uninitialized storage for N T's
typename std::aligned_storage<sizeof(T), alignof(T)>::type data[N];
std::size_t m_size = 0;
public:
// Create an object in aligned storage
template<typename ...Args> void emplace_back(Args&&... args)
{
if( m_size >= N ) // possible error handling
throw std::bad_alloc{};
new (data + m_size) T(std::forward<Args>(args)...);
++m_size;
}
// Access an object in aligned storage
const T& operator[](std::size_t pos) const
{
return *reinterpret_cast<const T*>(data + pos);
}
// Delete objects from aligned storage
~static_vector()
{
for (std::size_t pos = 0; pos < m_size; ++pos) {
reinterpret_cast<const T*>(data+pos)->~T();
}
}
};
And then use it
static_vector<ReaderWriterQueue<int>, 1024> queues;
for (int i = 0; i != 1024; ++i) {
queues.emplace_back(100)
}
Upvotes: 1
Reputation: 1725
If you really want to use arrays, you can use std::fill
to fill its elements as follows:
ReaderWriterQueue<int> qp[1024];
std::fill(begin(qp), end(qp), ReaderWriterQueue<int>(100));
As it's pointed out in the comments, this would work if ReaderWriterQueue
is copyable, which seems that it is not. Then, another solution would be to declare array or vector of (smart) pointers to ReaderWriterQueue
as follows, and initialize it later:
std::vector<std::shared_ptr<ReaderWriterQueue<int>>> qp(1024);
for(auto& x : qp)
x = make_shared<ReaderWriterQueue<int>>(100);
Upvotes: 0
Reputation: 42838
Doing this with vector
is trivial:
std::vector<ReaderWriterQueue<int>> qp(1024, ReaderWriterQueue<int>(100));
This of course assumes that ReaderWriterQueue
has a (public) copy constructor which does the right thing. However, this is not the case in the current version of the ReaderWriterQueue
, so what you're trying to do is not possible.
Upvotes: 0