mercredi 30 janvier 2019

How to remove data from class instance after data being returned once

In my app I need to read some values from some source, keep them for a while in a storage, and then apply. After being applied, values are not needed and can be removed from the storage.

class Storage: public Singleton<...> {
public:
    void addValue(int v) {values.push_back(v);}
    // ...
private:
    std::vector<int> values;
    // ...
}

// read values from some source and keep them in the storage
void Init() {
    for (...) {
        Storage::Instance()->addValue(...);
    }
}

// a few seconds later...

// apply values somehow and get rid of them
void Apply() {
    auto &&values = Storage::Instance()->getValues();
    // ideally, at this point Storage must not contain values array
    // process values somehow
    for auto i: values {
    // ...
    }
    // values are not needed any longer
}

My question is how do I implement getValues method? Is it possible to implement it so that it clears values array in Storage after being called (using move semantics or whatsoever)? In other words, there is no need to keep values in Storage after getValues has been called once.

If it is not possible, I will have to implement additional method say Storage::clearValues which I would need to call at the end of Apply() -- this is what I am tryng to avoid.

Aucun commentaire:

Enregistrer un commentaire