ResourceManagerT_impl.hh 8.54 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
/*===========================================================================*\
 *                                                                           *
 *                            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/>.                                      *
 *                                                                           *
\*===========================================================================*/

#define RESOURCEMANAGERT_CC

#include "ResourceManager.hh"
#include "PropertyDefines.hh"
39
#include "TypeName.hh"
40 41 42

namespace OpenVolumeMesh {

43

44
template<class T>
45
VertexPropertyT<T> ResourceManager::request_vertex_property(const std::string& _name, const T _def) {
46

47
    return internal_request_property<std::vector<BaseProperty*>,VertexPropertyT<T>,VertexPropHandle,T>(vertex_props_, _name, n_vertices(), _def);
48 49 50
}

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

53
    return internal_request_property<std::vector<BaseProperty*>,EdgePropertyT<T>,EdgePropHandle,T>(edge_props_, _name, n_edges(), _def);
54 55 56
}

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

59
    return internal_request_property<std::vector<BaseProperty*>,HalfEdgePropertyT<T>,HalfEdgePropHandle,T>(halfedge_props_, _name, n_edges()*2u, _def);
60 61 62
}

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

65
    return internal_request_property<std::vector<BaseProperty*>,FacePropertyT<T>,FacePropHandle,T>(face_props_, _name, n_faces(), _def);
66 67 68
}

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

71
    return internal_request_property<std::vector<BaseProperty*>,HalfFacePropertyT<T>,HalfFacePropHandle,T>(halfface_props_, _name, n_faces()*2u, _def);
72 73 74
}

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

77
    return internal_request_property<std::vector<BaseProperty*>,CellPropertyT<T>,CellPropHandle,T>(cell_props_, _name, n_cells(), _def);
78 79 80
}

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

83
    return internal_request_property<std::vector<BaseProperty*>,MeshPropertyT<T>,MeshPropHandle,T>(mesh_props_, _name, 1, _def);
84 85
}

86
template<class StdVecT, class PropT, class HandleT, class T>
87 88 89
PropT ResourceManager::internal_request_property(StdVecT& _vec, const std::string& _name, size_t _size, const T _def)
{
    auto type_name = get_type_name<T>();
90

91
    if(!_name.empty()) {
92 93
        for(typename StdVecT::iterator it = _vec.begin();
                it != _vec.end(); ++it) {
94 95 96
            if((*it)->name() == _name
                && (*it)->internal_type_name() == type_name)
            {
97
                return *static_cast<PropT*>(*it);
98
            }
99 100 101
        }
    }

102
    auto handle = HandleT::from_unsigned(_vec.size());
103

104
    PropT* prop = new PropT(_name, type_name, *this, handle, _def);
105
    prop->resize(_size);
106 107

    // Store property pointer
108
    _vec.push_back(prop);
109 110 111 112

    return *prop;
}

113
// request_property: work around C++ currently now allowing partial specialisation on functions by using structs:
114

115 116 117 118
template<class T, typename EntityTag>
struct request_property_impl {
    static PropertyTT<T, EntityTag> _(ResourceManager* /*resman*/, const std::string& /*_name*/, const T /*_def*/);
};
119

120 121 122 123 124 125
template<class T>
struct request_property_impl<T, Entity::Vertex>{
    static PropertyTT<T, Entity::Vertex> _(ResourceManager *resman, const std::string &_name, const T _def) {
        return resman->request_vertex_property<T>(_name, _def);
    }
};
126
template<class T>
127 128 129 130 131
struct request_property_impl<T, Entity::Edge>{
    static PropertyTT<T, Entity::Edge> _(ResourceManager *resman, const std::string &_name, const T _def) {
        return resman->request_edge_property<T>(_name, _def);
    }
};
132
template<class T>
133 134 135 136 137
struct request_property_impl<T, Entity::HalfEdge>{
    static PropertyTT<T, Entity::HalfEdge> _(ResourceManager *resman, const std::string &_name, const T _def) {
        return resman->request_halfedge_property<T>(_name, _def);
    }
};
138
template<class T>
139 140 141 142 143
struct request_property_impl<T, Entity::Face>{
    static PropertyTT<T, Entity::Face> _(ResourceManager *resman, const std::string &_name, const T _def) {
        return resman->request_face_property<T>(_name, _def);
    }
};
144
template<class T>
145 146 147 148 149
struct request_property_impl<T, Entity::HalfFace>{
    static PropertyTT<T, Entity::HalfFace> _(ResourceManager *resman, const std::string &_name, const T _def) {
        return resman->request_halfface_property<T>(_name, _def);
    }
};
150
template<class T>
151 152 153 154 155
struct request_property_impl<T, Entity::Cell>{
    static PropertyTT<T, Entity::Cell> _(ResourceManager *resman, const std::string &_name, const T _def) {
        return resman->request_cell_property<T>(_name, _def);
    }
};
156

157 158 159 160
template<typename T, typename EntityTag>
PropertyTT<T, EntityTag> ResourceManager::request_property(const std::string& _name, const T _def)
{
    return request_property_impl<T, EntityTag>::_(this, _name, _def);
161 162
}

163

164 165 166
template<typename T, class EntityTag>
void ResourceManager::set_persistent(PropertyTT<T, EntityTag>& _prop, bool _flag)
{
167 168 169 170 171 172 173
    if(_flag == _prop->persistent()) return;
    _prop->set_persistent(_flag);
}

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

174 175 176
    auto prop_ptr = _vec[_idx];
    prop_ptr->setResMan(nullptr);
    delete prop_ptr;
177
    _vec.erase(_vec.begin() + _idx);
178
    updatePropHandles(_vec);
179 180 181
}

template<class StdVecT>
182
void ResourceManager::resize_props(StdVecT& _vec, size_t _n) {
183 184 185 186 187 188 189 190 191 192 193 194

    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) {
195
        (*it)->delete_element(_h.uidx());
196 197 198 199 200 201
    }
}

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

202 203 204
    for (auto prop: _vec) {
        prop->setResMan(nullptr);
        delete prop;
205
    }
206 207
    _vec.clear();
}
208

209 210 211 212 213
template<class StdVecT>
void ResourceManager::updatePropHandles(StdVecT &_vec)
{
    size_t n = _vec.size();
    for(size_t i = 0; i < n; ++i) {
214
        _vec[i]->set_handle(OpenVolumeMeshHandle(static_cast<int>(i)));
215
    }
216 217
}

218
} // Namespace OpenVolumeMesh