datacontainer.cpp 1.82 KB
Newer Older
schultezub's avatar
schultezub committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "datacontainer.h"

namespace TUMVis {
    const std::string DataContainer::loggerCat_ = "TUMVis.core.datastructures.DataContainer";

    DataContainer::DataContainer() {

    }

    DataContainer::~DataContainer() {
        // remove ownership op all owned DataHandles
        for (std::map<std::string, const DataHandle*>::iterator it = _handles.begin(); it != _handles.end(); ++it) {
            DataHandle::removeOwner(it->second, this);
        }
        _handles.clear();
    }

    const DataHandle* DataContainer::addData(const std::string& name, AbstractData* data) {
schultezub's avatar
schultezub committed
19
        DataHandle* dh = new DataHandle(data);
schultezub's avatar
schultezub committed
20
        addDataHandle(name, dh);
schultezub's avatar
schultezub committed
21
22
23
24
        return dh;
    }

    void DataContainer::addDataHandle(const std::string& name, const DataHandle* dh) {
schultezub's avatar
schultezub committed
25
26
27
28
29
30
31
32
33
34
        {
            tbb::spin_mutex::scoped_lock lock(_localMutex);
            std::map<std::string, const DataHandle*>::iterator it = _handles.lower_bound(name);
            if (it != _handles.end() && it->first == name) {
                DataHandle::removeOwner(it->second, this);
                it->second = dh;
            }
            else {
                _handles.insert(it, std::make_pair(name, dh));
            }
schultezub's avatar
schultezub committed
35
        }
schultezub's avatar
schultezub committed
36
        DataHandle::addOwner(dh, this);
schultezub's avatar
schultezub committed
37
        s_dataAdded(name, dh);
schultezub's avatar
schultezub committed
38
39
40
    }

    bool DataContainer::hasData(const std::string& name) const {
41
        tbb::spin_mutex::scoped_lock lock(_localMutex);
schultezub's avatar
schultezub committed
42
43
44
45
        return (_handles.find(name) != _handles.end());
    }

    const DataHandle* DataContainer::getData(const std::string& name) const {
46
        tbb::spin_mutex::scoped_lock lock(_localMutex);
schultezub's avatar
schultezub committed
47
48
49
50
51
52
53
        std::map<std::string, const DataHandle*>::const_iterator it = _handles.find(name);
        if (it == _handles.end())
            return 0;
        else
            return it->second;
    }
}