0

I'm looking for patterns or for some ready to use solutions for next problem:

Container with features:

  1. One element of an arbitrary type storing
  2. When element is taken from container it gets empty
  3. When new element is stored, it overwrites previous content
  4. Thread safety

My naive implementation without thread safety:

template<typename T>
class Boxed {
    T* _data;
public:
    Boxed()
        : _data(nullptr)
    {}

    Boxed(const T& data)
        : _data(new T(data))
    {}

    ~Boxed() {
        if(_data != nullptr) {
            delete _data;
        }
    }

    inline
    void setData(const T& data) {
        if(_data != nullptr) {
            delete _data;
        }
        _data = new T(data);
    }

    inline
    T getData() {
        T res(*_data);
        delete _data;
        _data = nullptr;
        return res;
    }

    inline
    bool empty() const {
        return _data == nullptr;
    }

    inline
    operator bool() cons {
        return empty();
    }
};

I will be grateful for any help.

dFionov
  • 129
  • 10
  • What are you looking for in terms of thread safety? – SergeyA Mar 18 '16 at 13:38
  • 1
    Are you reimplementing std::unique_ptr? Also: please read about the rule of 5. – Werner Henze Mar 18 '16 at 13:57
  • 1
    "Thread safety" is too vague. For an excellent historical example of naive "thread safety" going wrong, look at Java's `Vector` class (see http://stackoverflow.com/questions/1386275/why-is-java-vector-class-considered-obsolete-or-deprecated) and avoid the mistake it made. – Christian Hackl Mar 18 '16 at 14:54
  • @WernerHenze, is it [rule of five](https://en.wikipedia.org/wiki/Rule_of_three_(C%2B%2B_programming))? I'll look at. Thanks a lot for comment! – dFionov Mar 18 '16 at 15:25
  • @SergeyA, I'm looking for thread safe solution as this container can be used from different threads. I know about mutexes, but thought that it's not need for naive implementation to understand what I waant to get, so I didnt include it in code sample. – dFionov Mar 18 '16 at 15:29
  • @dFionov Yes, that's the rule. Previously rule of three, now rule of five. – Werner Henze Mar 18 '16 at 16:08

1 Answers1

1

Use a queue of size 2. Protect the underlining array/vector/deque with a mutex. Logic for push and pop is trivial. On push: if not empty then remove old element, insert new one. On pop: It is empty or has size 1, nothing special to do than a regular pop. So it does not overwrite the current one but will replace it. the difference is not observable.

Signatures:

class only_one
{
public:
    bool push(T& e);
    bool pop(T& e);
    bool is_empty() const;
}

Implementations for thread safe queues with mutex/condition_var can be found in lot of corners of the internet. Example

knivil
  • 787
  • 3
  • 11
  • Thanks a lot! I did few implementations with deque/vector/other STL-container, but I'm looking for simpler solution. If it doesn't exist - I will use one of known. – dFionov Mar 18 '16 at 15:33