2.12.2021, 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

datacontainer.cpp 3.94 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-2014, all rights reserved,
schultezub's avatar
schultezub committed
6
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
7
//      Chair for Computer Aided Medical Procedures
8
9
//      Technische Universitaet Muenchen
//      Boltzmannstr. 3, 85748 Garching b. Muenchen, Germany
10
// 
schultezub's avatar
schultezub committed
11
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
12
// 
13
14
15
16
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 
// except in compliance with the License. You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
17
// 
18
19
20
21
// Unless required by applicable law or agreed to in writing, software distributed under the 
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
// either express or implied. See the License for the specific language governing permissions 
// and limitations under the License.
22
23
24
// 
// ================================================================================================

schultezub's avatar
schultezub committed
25
#include "datacontainer.h"
schultezub's avatar
schultezub committed
26

27
#include "tgt/assert.h"
28
#include "tgt/logmanager.h"
schultezub's avatar
schultezub committed
29
#include "core/datastructures/abstractdata.h"
schultezub's avatar
schultezub committed
30

schultezub's avatar
schultezub committed
31
32
namespace campvis {
    const std::string DataContainer::loggerCat_ = "CAMPVis.core.datastructures.DataContainer";
schultezub's avatar
schultezub committed
33

34
35
36
    DataContainer::DataContainer(const std::string& name)
        : _name(name)
    {
schultezub's avatar
schultezub committed
37
38
39
40
41
42
43

    }

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

44
    DataHandle DataContainer::addData(const std::string& name, AbstractData* data) {
45
46
47
48
49
        if (name.empty()) {
            LERROR("Tried to add data with empty name to DataContainer.");
            return DataHandle(0);
        }

50
        tgtAssert(data != 0, "The Data must not be 0.");
51
52
        tgtAssert(!name.empty(), "The data's name must not be empty.");

53
        DataHandle dh(data);
schultezub's avatar
schultezub committed
54
        addDataHandle(name, dh);
schultezub's avatar
schultezub committed
55
56
57
        return dh;
    }

58
    void DataContainer::addDataHandle(const std::string& name, DataHandle dh) {
59
60
61
62
63
        if (name.empty()) {
            LERROR("Tried to add data with empty name to DataContainer.");
            return;
        }

64
        tgtAssert(dh.getData() != 0, "The data in the DataHandle must not be 0!");
65
        tgtAssert(!name.empty(), "The data's name must not be empty.");
66
67
68
        _handles.erase(name);
        _handles.insert(std::make_pair(name, dh));
 
schultezub's avatar
schultezub committed
69
        s_dataAdded(name, dh);
70
        s_changed();
schultezub's avatar
schultezub committed
71
72
73
    }

    bool DataContainer::hasData(const std::string& name) const {
74
75
        tbb::concurrent_hash_map<std::string, DataHandle>::const_accessor a;
        return _handles.find(a, name);
schultezub's avatar
schultezub committed
76
77
    }

78
    DataHandle DataContainer::getData(const std::string& name) const {
79
80
81
82
83
        tbb::concurrent_hash_map<std::string, DataHandle>::const_accessor a;
        if (_handles.find(a, name)) {
            return a->second;
        }
        else {
84
            return DataHandle(0);
85
        }
schultezub's avatar
schultezub committed
86
    }
schultezub's avatar
schultezub committed
87
88

    void DataContainer::removeData(const std::string& name) {
89
        _handles.erase(name);
schultezub's avatar
schultezub committed
90
    }
91

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

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

        return toReturn;
    }

104
105
    void DataContainer::clear() {
        _handles.clear();
106
107
    }

108
109
110
111
112
113
114
115
    const std::string& DataContainer::getName() const {
        return _name;
    }

    void DataContainer::setName(const std::string& name) {
        _name = name;
    }

schultezub's avatar
schultezub committed
116
}