manager.h 7.39 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
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**********************************************************************
 *                                                                    *
 * tgt - Tiny Graphics Toolbox                                        *
 *                                                                    *
 * Copyright (C) 2006-2011 Visualization and Computer Graphics Group, *
 * Department of Computer Science, University of Muenster, Germany.   *
 * <http://viscg.uni-muenster.de>                                     *
 *                                                                    *
 * This file is part of the tgt library. This library is free         *
 * software; you can redistribute it and/or modify it under the terms *
 * of the GNU Lesser General Public License version 2.1 as published  *
 * by the Free Software Foundation.                                   *
 *                                                                    *
 * This library 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 Lesser General Public   *
 * License in the file "LICENSE.txt" along with this library.         *
 * If not, see <http://www.gnu.org/licenses/>.                        *
 *                                                                    *
 **********************************************************************/

#ifndef TGT_MANAGER_H
#define TGT_MANAGER_H

#include <string>
#include <map>
#include <vector>
#include <list>
#include <iostream>

34
35
36
37
#include "cgt/types.h"
#include "cgt/assert.h"
#include "cgt/filesystem.h"
#include "cgt/logmanager.h"
schultezub's avatar
schultezub committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

namespace tgt {

template <class T>
class ResourceManager {
public:
    /// Constructor
    ResourceManager(bool cacheResources = true);

    /// Destroy all resources in memory
    virtual ~ResourceManager();

    /// Check if resource is loaded
    bool isLoaded(const std::string& filename);

    /// Mark resource as unused
    virtual void dispose(T* ptr);

    // getter - setter
    void addPath(std::string path);
    void removePath(std::string path);

    /**
     * Searches in all paths for file and returns valid filename including complete path.
     * If file is not found in search path, an empty string is returned.
     */
    std::string completePath(std::string filename);

    bool isCaching() const { return cacheResources_; }

    std::vector<std::string> getFilenames();

schultezub's avatar
KissCL:    
schultezub committed
70
71
    const std::list<std::string>& getPathList() const { return pathList_; };

schultezub's avatar
schultezub committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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
203
204
205
206
207
208
209
210
211
protected:
    struct Resource {
        T*          data_;
        ulong       usedBy_;
        std::string filename_;
    };

    std::map< std::string, Resource* >  resourcesByFilename_;
    std::map< T*, Resource* >           resourcesByPtr_;
    bool cacheResources_;

    std::list<std::string> pathList_;

    void reg(T *ptr, const std::string& filename);
    void increaseUsage(const std::string& filename);

    // tgt logging category
    static const std::string loggerCat_;

    // getter - setter
    T* get(const std::string& filename);
    //const T* get(const std::string& filename) const;
};

template <class T>
const std::string ResourceManager<T>::loggerCat_("tgt.Manager");

//#ifdef TGT_BUILD_DLL

template <class T>
T* ResourceManager<T>::get(const std::string& filename) {
    return resourcesByFilename_[filename]->data_;
}

//template <class T>
//const T* ResourceManager<T>::get(const std::string& filename) const {
//    return resourcesByFilename_[filename]->data_;
//}

template <class T>
void ResourceManager<T>::reg(T* ptr, const std::string& filename) {
    if (cacheResources_ && isLoaded(filename)) {
        Resource* r = resourcesByFilename_[filename];
        r->data_ = ptr;
        r->usedBy_++;
        resourcesByPtr_[ptr] = r;
    } else {
        Resource* r = new Resource();
        r->data_ = ptr;
        r->usedBy_ = 1;
        r->filename_ = filename;
        resourcesByFilename_[filename] = r;
        resourcesByPtr_[ptr] = r;
    }
}

template <class T>
void ResourceManager<T>::increaseUsage(const std::string& filename) {
    tgtAssert(cacheResources_, "increaseUsage should not be called in non-caching mode!");
    resourcesByFilename_[filename]->usedBy_++;
}

template <class T>
ResourceManager<T>::ResourceManager(bool cacheResources) {
    // this should not be changed afterwards
    cacheResources_ = cacheResources;
}

template <class T>
ResourceManager<T>::~ResourceManager() {
    while (!resourcesByFilename_.empty()) {
#ifdef TGT_DEBUG
        LWARNING("Un-disposed Resource: " << (*resourcesByFilename_.begin()).second->filename_
                 << " in use by " << (*resourcesByFilename_.begin()).second->usedBy_);
#endif
        delete resourcesByFilename_.begin()->second->data_;
        delete resourcesByFilename_.begin()->second;
        resourcesByFilename_.erase(resourcesByFilename_.begin());
    }
}

template <class T>
bool ResourceManager<T>::isLoaded(const std::string& filename) {
    return(cacheResources_ && (resourcesByFilename_.find(filename) != resourcesByFilename_.end()));
}

template <class T>
void ResourceManager<T>::dispose(T* ptr) {
    if (ptr == 0 || resourcesByPtr_.find(ptr) == resourcesByPtr_.end())
        return;

    Resource* r = resourcesByPtr_[ptr];
    r->usedBy_--;

    // check if resource is still in use
    if (r->usedBy_ == 0) {
        std::string filename = r->filename_;
        delete r->data_;
        resourcesByFilename_.erase(filename);
        resourcesByPtr_.erase(ptr);
        delete r;
    }
    else if (!cacheResources_) {
        resourcesByPtr_.erase(ptr);
    }
    ptr = 0;
}

template <class T>
void ResourceManager<T>::addPath(std::string path) {
    pathList_.push_front(path);
    // remove duplicates
    //TODO: better use std::set<> here
    pathList_.sort();
    pathList_.unique();
}

template <class T>
void ResourceManager<T>::removePath(std::string path) {
    std::list<std::string>::iterator it;
    for (it = pathList_.begin(); it != pathList_.end(); ++it) {
        if (*it == path) {
            pathList_.erase(it);
            break;
        }
    }
}

template <class T>
std::string ResourceManager<T>::completePath(std::string filename) {
    std::string cplFileName = filename;

    if(FileSys.exists(filename))
        return filename;

    bool foundFile = false;
    if (!cplFileName.empty()) {
        std::list<std::string>::iterator iter = pathList_.begin();
        while (iter != pathList_.end() && !foundFile) {
            cplFileName = (!(*iter).empty() ? (*iter) + '/' : "") + filename;
212
            //LDEBUG("Completed file name to " << cplFileName);
schultezub's avatar
schultezub committed
213
214
215
216
217
218
219
220
            if (FileSys.exists(cplFileName))
                foundFile = true;
            iter++;
        }
    }

    if (foundFile)
        return cplFileName;
schultezub's avatar
schultezub committed
221
222
    else {
        LERROR("Could not complete path for file " << filename);
schultezub's avatar
schultezub committed
223
        return "";
schultezub's avatar
schultezub committed
224
    }
schultezub's avatar
schultezub committed
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
}

template <class T>
std::vector<std::string> ResourceManager<T>::getFilenames() {
    std::vector<std::string> filenames;
    for (typename std::map<std::string, Resource*>::const_iterator iter = resourcesByFilename_.begin();
         iter != resourcesByFilename_.end(); iter++)
    {
        filenames.push_back((*iter).first);
    }
    return filenames;
}

//#endif

} // namespace tgt

#endif //TGT_MANAGER_H