datacontainer.cpp 1.74 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
19
#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) {
        DataHandle* dh = new DataHandle(this, 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) {
25
        tbb::spin_mutex::scoped_lock lock(_localMutex);
schultezub's avatar
schultezub committed
26
27
28
29
30
31
32
33
        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
34
35
36
37
        DataHandle::addOwner(dh, this);
    }

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

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