I have two independent classes that each maintain a buffer of limited size:
class A {
private:
std::deque<uint8_t> buffer;
size_t max_size;
};
class B {
private:
static const size_t MAX_SIZE = 1024;
uint8_t buffer[MAX_SIZE];
}
Now, I also have a function C that isn't a member of A or B. This function needs to take (i.e. remove) as many bytes as possible from the beginning of A and write it to B. Ofc no buffer overflows allowed.
Question: How do I do this efficiently while ensuring that A and B are encapsulated? I.e. A, B and C do not know how the others are implemented.
Things I don't want, for obvious reasons:
class A
returns a const iterator via a new public method.
class B
exposes a method to copy those bytes in.
class A {
public:
const std::deque<uint8_t>& getBuffer() const
{
return buffer;
}
private:
std::deque<uint8_t> buffer;
size_t max_size;
size_t current_size;
};
class B {
public:
B() : max_size(MAX_SIZE), current_size(0)
{
}
void Transfer(const std::deque<uint8_t>& data)
{
size_t remaining = max_size - current_size;
size_t toCopy = data.size() > remaining ? remaining : data.size();
for (size_t i = 0; i < toCopy; i++)
{
buffer[i+current_size] = data[i];
}
current_size += toCopy;
}
private:
static const size_t MAX_SIZE = 1024;
uint8_t buffer[MAX_SIZE];
};
CopyAToB(const A& a, B& b)
{
b.Transfer(a.getBuffer());
}