The name of the initial branch for new projects is now "main" instead of "master". Existing projects remain unchanged. More information: https://doku.lrz.de/display/PUBLIC/GitLab

datacontainer.cpp 4.44 KB
Newer Older
1 2
// ================================================================================================
// 
schultezub's avatar
schultezub committed
3
// This file is part of the CAMPVis Software Framework.
4 5
// 
// If not explicitly stated otherwise: Copyright (C) 2012, all rights reserved,
schultezub's avatar
schultezub committed
6
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
7 8 9
//      Chair for Computer Aided Medical Procedures
//      Technische Universität München
//      Boltzmannstr. 3, 85748 Garching b. München, Germany
schultezub's avatar
schultezub committed
10
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
// 
// The licensing of this softare is not yet resolved. Until then, redistribution in source or
// binary forms outside the CAMP chair is not permitted, unless explicitly stated in legal form.
// However, the names of the original authors and the above copyright notice must retain in its
// original state in any case.
// 
// Legal disclaimer provided by the BSD license:
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
// AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
// 
// ================================================================================================

schultezub's avatar
schultezub committed
30
#include "datacontainer.h"
schultezub's avatar
schultezub committed
31

32
#include "tgt/assert.h"
schultezub's avatar
schultezub committed
33
#include "core/datastructures/abstractdata.h"
schultezub's avatar
schultezub committed
34

schultezub's avatar
schultezub committed
35 36
namespace campvis {
    const std::string DataContainer::loggerCat_ = "CAMPVis.core.datastructures.DataContainer";
schultezub's avatar
schultezub committed
37 38 39 40 41 42 43 44 45

    DataContainer::DataContainer() {

    }

    DataContainer::~DataContainer() {
        _handles.clear();
    }

46
    DataHandle DataContainer::addData(const std::string& name, AbstractData* data) {
47
        tgtAssert(data != 0, "The Data must not be 0.");
48
        DataHandle dh(data);
schultezub's avatar
schultezub committed
49
        addDataHandle(name, dh);
schultezub's avatar
schultezub committed
50 51 52
        return dh;
    }

53
    void DataContainer::addDataHandle(const std::string& name, const DataHandle& dh) {
54
        tgtAssert(dh.getData() != 0, "The data in the DataHandle must not be 0!");
schultezub's avatar
schultezub committed
55 56
        {
            tbb::spin_mutex::scoped_lock lock(_localMutex);
57
            std::map<std::string, DataHandle>::iterator it = _handles.lower_bound(name);
schultezub's avatar
schultezub committed
58 59 60 61 62 63
            if (it != _handles.end() && it->first == name) {
                it->second = dh;
            }
            else {
                _handles.insert(it, std::make_pair(name, dh));
            }
schultezub's avatar
schultezub committed
64
        }
schultezub's avatar
schultezub committed
65
        s_dataAdded(name, dh);
66
        s_changed();
schultezub's avatar
schultezub committed
67 68 69
    }

    bool DataContainer::hasData(const std::string& name) const {
70
        tbb::spin_mutex::scoped_lock lock(_localMutex);
schultezub's avatar
schultezub committed
71 72 73
        return (_handles.find(name) != _handles.end());
    }

74
    DataHandle DataContainer::getData(const std::string& name) const {
75
        tbb::spin_mutex::scoped_lock lock(_localMutex);
76
        std::map<std::string, DataHandle>::const_iterator it = _handles.find(name);
schultezub's avatar
schultezub committed
77
        if (it == _handles.end())
78
            return DataHandle(0);
schultezub's avatar
schultezub committed
79
        else
80
            return it->second;
schultezub's avatar
schultezub committed
81
    }
schultezub's avatar
schultezub committed
82 83 84

    void DataContainer::removeData(const std::string& name) {
        tbb::spin_mutex::scoped_lock lock(_localMutex);
85
        std::map<std::string, DataHandle>::const_iterator it = _handles.find(name);
schultezub's avatar
schultezub committed
86 87 88 89
        if (it != _handles.end()) {
            _handles.erase(it);
        }
    }
90

91 92
    std::vector< std::pair< std::string, DataHandle> > DataContainer::getDataHandlesCopy() const {
        std::vector< std::pair< std::string, DataHandle> > toReturn;
93 94 95
        toReturn.reserve(_handles.size());

        tbb::spin_mutex::scoped_lock lock(_localMutex);
96 97
        for (std::map<std::string, DataHandle>::const_iterator it = _handles.begin(); it != _handles.end(); ++it) {
            toReturn.push_back(std::make_pair(it->first, it->second));
98 99 100 101 102
        }

        return toReturn;
    }

103
    std::map<std::string, DataHandle> DataContainer::getHandlesCopy() const {
104
        tbb::spin_mutex::scoped_lock lock(_localMutex);
105
        return _handles;
106 107
    }

schultezub's avatar
schultezub committed
108
}