ResourceManagerT.cc 7.83 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
/*===========================================================================*\
 *                                                                           *
 *                            OpenVolumeMesh                                 *
 *        Copyright (C) 2011 by Computer Graphics Group, RWTH Aachen         *
 *                        www.openvolumemesh.org                             *
 *                                                                           *
 *---------------------------------------------------------------------------*
 *  This file is part of OpenVolumeMesh.                                     *
 *                                                                           *
 *  OpenVolumeMesh is free software: you can redistribute it and/or modify   *
 *  it under the terms of the GNU Lesser General Public License as           *
 *  published by the Free Software Foundation, either version 3 of           *
 *  the License, or (at your option) any later version with the              *
 *  following exceptions:                                                    *
 *                                                                           *
 *  If other files instantiate templates or use macros                       *
 *  or inline functions from this file, or you compile this file and         *
 *  link it with other files to produce an executable, this file does        *
 *  not by itself cause the resulting executable to be covered by the        *
 *  GNU Lesser General Public License. This exception does not however       *
 *  invalidate any other reasons why the executable file might be            *
 *  covered by the GNU Lesser General Public License.                        *
 *                                                                           *
 *  OpenVolumeMesh is distributed in the hope that it will be useful,        *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
 *  GNU Lesser General Public License for more details.                      *
 *                                                                           *
 *  You should have received a copy of the GNU LesserGeneral Public          *
 *  License along with OpenVolumeMesh.  If not,                              *
 *  see <http://www.gnu.org/licenses/>.                                      *
 *                                                                           *
\*===========================================================================*/

/*===========================================================================*\
 *                                                                           *
 *   $Revision$                                                         *
 *   $Date$                    *
 *   $LastChangedBy$                                                *
 *                                                                           *
\*===========================================================================*/

#define RESOURCEMANAGERT_CC

#include "ResourceManager.hh"

#include "PropertyDefines.hh"

#include "PropertyPtr.hh"

namespace OpenVolumeMesh {

template<class T>
VertexPropertyT<T> ResourceManager::request_vertex_property(const std::string& _name) {

56
    return request_property<std::vector<BaseProperty*>,VertexPropertyT<T>,VertexPropHandle>(vertex_props_, _name, n_vertices());
57 58 59 60 61
}

template<class T>
EdgePropertyT<T> ResourceManager::request_edge_property(const std::string& _name) {

62
    return request_property<std::vector<BaseProperty*>,EdgePropertyT<T>,EdgePropHandle>(edge_props_, _name, n_edges());
63 64 65 66 67
}

template<class T>
HalfEdgePropertyT<T> ResourceManager::request_halfedge_property(const std::string& _name) {

68
    return request_property<std::vector<BaseProperty*>,HalfEdgePropertyT<T>,HalfEdgePropHandle>(halfedge_props_, _name, n_edges()*2u);
69 70 71 72 73
}

template<class T>
FacePropertyT<T> ResourceManager::request_face_property(const std::string& _name) {

74
    return request_property<std::vector<BaseProperty*>,FacePropertyT<T>,FacePropHandle>(face_props_, _name, n_faces());
75 76 77 78 79
}

template<class T>
HalfFacePropertyT<T> ResourceManager::request_halfface_property(const std::string& _name) {

80
    return request_property<std::vector<BaseProperty*>,HalfFacePropertyT<T>,HalfFacePropHandle>(halfface_props_, _name, n_faces()*2u);
81 82 83 84 85
}

template<class T>
CellPropertyT<T> ResourceManager::request_cell_property(const std::string& _name) {

86
    return request_property<std::vector<BaseProperty*>,CellPropertyT<T>,CellPropHandle>(cell_props_, _name, n_cells());
87 88 89 90 91
}

template<class T>
MeshPropertyT<T> ResourceManager::request_mesh_property(const std::string& _name) {

92 93 94 95 96 97
    return request_property<std::vector<BaseProperty*>,MeshPropertyT<T>,MeshPropHandle>(mesh_props_, _name, 1);
}

template<class StdVecT, class PropT, class HandleT>
PropT ResourceManager::request_property(StdVecT& _vec, const std::string& _name, size_t _size) {

Mike Kremer's avatar
Mike Kremer committed
98
    if(!_name.empty()) {
99 100 101
        for(typename StdVecT::iterator it = _vec.begin();
                it != _vec.end(); ++it) {
            if(!(*it)->persistent()) continue;
Mike Kremer's avatar
Mike Kremer committed
102
            if((*it)->name() == _name) {
103
                PropT* prop = dynamic_cast<PropT*>(*it);
Mike Kremer's avatar
Mike Kremer committed
104 105
                return *prop;
            }
106 107 108
        }
    }

109
    HandleT handle(_vec.size());
110

111 112
    PropT* prop = new PropT(_name, *this, handle);
    prop->resize(_size);
113 114

    // Store property pointer
115
    _vec.push_back(prop);
116 117 118 119

    return *prop;
}

Mike Kremer's avatar
Mike Kremer committed
120
template<class T>
121 122
void ResourceManager::set_persistent(VertexPropertyT<T>& _prop, bool _flag) {

123
    set_persistentT(_prop, _flag);
Mike Kremer's avatar
Mike Kremer committed
124 125 126
}

template<class T>
127 128
void ResourceManager::set_persistent(EdgePropertyT<T>& _prop, bool _flag) {

129
    set_persistentT(_prop, _flag);
Mike Kremer's avatar
Mike Kremer committed
130 131 132
}

template<class T>
133 134
void ResourceManager::set_persistent(HalfEdgePropertyT<T>& _prop, bool _flag) {

135
    set_persistentT(_prop, _flag);
Mike Kremer's avatar
Mike Kremer committed
136 137 138
}

template<class T>
139 140
void ResourceManager::set_persistent(FacePropertyT<T>& _prop, bool _flag) {

141
    set_persistentT(_prop, _flag);
Mike Kremer's avatar
Mike Kremer committed
142 143 144
}

template<class T>
145 146
void ResourceManager::set_persistent(HalfFacePropertyT<T>& _prop, bool _flag) {

147
    set_persistentT(_prop, _flag);
Mike Kremer's avatar
Mike Kremer committed
148 149 150
}

template<class T>
151 152
void ResourceManager::set_persistent(CellPropertyT<T>& _prop, bool _flag) {

153
    set_persistentT(_prop, _flag);
Mike Kremer's avatar
Mike Kremer committed
154 155 156
}

template<class T>
157 158
void ResourceManager::set_persistent(MeshPropertyT<T>& _prop, bool _flag) {

159 160 161 162 163
    set_persistentT(_prop, _flag);
}

template<class PropT>
void ResourceManager::set_persistentT(PropT& _prop, bool _flag) {
164

165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
    if(_flag == _prop->persistent()) return;

    _prop->set_persistent(_flag);
}

template<class StdVecT>
void ResourceManager::remove_property(StdVecT& _vec, size_t _idx) {

    (*(_vec.begin() + _idx))->lock();
    delete *(_vec.begin() + _idx);
    _vec.erase(_vec.begin() + _idx);
    size_t n = _vec.size();
    for(size_t i = 0; i < n; ++i) {
        _vec[i]->set_handle(OpenVolumeMeshHandle(i));
    }
}

template<class StdVecT>
void ResourceManager::resize_props(StdVecT& _vec, unsigned int _n) {

    for(typename StdVecT::iterator it = _vec.begin();
            it != _vec.end(); ++it) {
        (*it)->resize(_n);
    }
}

template<class StdVecT>
void ResourceManager::entity_deleted(StdVecT& _vec, const OpenVolumeMeshHandle& _h) {

    for(typename StdVecT::iterator it = _vec.begin();
            it != _vec.end(); ++it) {
        (*it)->delete_element(_h.idx());
    }
}

template<class StdVecT>
void ResourceManager::clearVec(StdVecT& _vec) {

203 204 205 206 207 208 209 210 211
    for(typename StdVecT::iterator it = _vec.begin();
            it != _vec.end(); ++it) {
        if(!(*it)->persistent()) {
            std::cerr << "Could not clear properties since at " <<
                    "least one property is still in use!" << std::endl;
            return;
        }
    }

212 213 214
    for(typename StdVecT::iterator it = _vec.begin();
            it != _vec.end(); ++it) {
        delete *it;
215
    }
216
    _vec.clear();
Mike Kremer's avatar
Mike Kremer committed
217 218
}

219
} // Namespace OpenVolumeMesh