taking over memory from std::vector
You can't take ownership of the memory from a vector, but you can solve your underlying problem another way.
Here's how I'd approach it - its a bit hacky because of the static global variable and not thread safe, but it can be made so with some simple locking around accesses to the registry
object.
static std::map<T*, std::vector<T>*> registry;
void my_startProcessing(std::vector<T> * data) {
registry.put(data->data(), data);
lib::startProcesing(data->data(), data->size());
}
void my_callback(T * data, size_t length) {
std::vector<T> * original = registry.get(data);
delete original;
registry.remove(data);
}
Now you can just do
std::vector<T> * input = ...
my_startProcessing(input);
But watch out! Bad things will happen if you add/remove elements to the input after you've called my_startProcessing
- the buffer the library has may be invalidated. (You may be allowed to change values in the vector, as I believe that will write through the to data correctly, but that will depend on what the library allows too.)
Also this doesn't work if T
=bool
since std::vector<bool>::data()
doesn't work.