DataControlFileFactory.hh 4 KB
Newer Older
1
2
3
4
5
/***********************************************************************
 * Copyright 2011-2012 Computer Graphics Group RWTH Aachen University. *
 * All rights reserved.                                                *
 * Distributed under the terms of the MIT License (see LICENSE.TXT).   *
 **********************************************************************/
6

7
8
#ifndef ACGL_OPENGL_CONTROLLER_DATACONTROLFILEFACTORY_HH
#define ACGL_OPENGL_CONTROLLER_DATACONTROLFILEFACTORY_HH
9
#ifdef ACGL_INCLUDE_DEPRECATED_FUNCTIONALITY
10
11
12
13
14
/**
 * Image loaders can register the supported types at TextureDataControlFileFactory so a call like
 *  ... = TextureControlFile("bunny.ppm").create();
 * will find the correct loader of the given file ending.
 */
15
16
17
18

#include <ACGL/ACGL.hh>

#include <ACGL/Base/Singleton.hh>
19
#include <ACGL/OpenGL/Controller/DataControlFile.hh>
20
#include <ACGL/Utils/StringHelpers.hh>
21
22
23
24
25
26
27

#include <map>
#include <string>

namespace ACGL{
namespace OpenGL{

28
29
30
31
32
33
//
// Generic FileFactories:
//

template<class DATA_CONTROL_FILE_TYPE>
class DataControlFileFactory : public Base::Singleton< DataControlFileFactory<DATA_CONTROL_FILE_TYPE> >
34
{
35
    ACGL_SINGLETON(DataControlFileFactory)
36
37
38
39
40

    // ========================================================================================================= \/
    // ================================================================================================ TYPEDEFS \/
    // ========================================================================================================= \/
private:
41
    typedef DATA_CONTROL_FILE_TYPE (*factoryCreate)(const std::string&);
42
43
44
45
46
47
    typedef std::map<std::string, factoryCreate> FactoryMap;

    // ========================================================================================================= \/
    // ============================================================================================ CONSTRUCTORS \/
    // ========================================================================================================= \/
protected:
48
    DataControlFileFactory(void) : mRegisteredTypes(0), mFactoryMap() {}
49
50

public:
51
    virtual ~DataControlFileFactory(void) {}
52
53
54
55
56
57

    // ========================================================================================================= \/
    // ================================================================================================= METHODS \/
    // ========================================================================================================= \/
public:
    int_t getRegisteredTypes(void) const { return mRegisteredTypes; }
58
59
60

    int_t registerType(const std::string& _type, factoryCreate _creator)
    {
Robert Menzel's avatar
Robert Menzel committed
61
        //ACGL::Utils::debug() << "DataControlFileFactory register type: " << _type << " with id " << mRegisteredTypes << std::endl;
62
63
64
65
66
67
68
69
        mFactoryMap[_type] = _creator;
        return mRegisteredTypes++;
    }

    DATA_CONTROL_FILE_TYPE create(const std::string& _filename) const
    {
        std::string file;
        std::string extension;
70
        ACGL::Utils::StringHelpers::splitFileExtension(_filename, file, extension);
71
72
73
74
75
76
77
        typename FactoryMap::const_iterator existingCreator = mFactoryMap.find(extension);
        if (existingCreator != mFactoryMap.end()) {
            return existingCreator->second(_filename);
        }
        return DATA_CONTROL_FILE_TYPE();
    }

78
79
80
81
82
83
84
85
86

    // ========================================================================================================= \/
    // ================================================================================================== FIELDS \/
    // ========================================================================================================= \/
private:
    int_t mRegisteredTypes;
    FactoryMap mFactoryMap;
};

87
88
89
90
91
92
93
//
// Individual FileFactories:
//
typedef DataControlFileFactory< SharedTextureDataControlFile >  TextureDataControlFileFactory;
typedef DataControlFileFactory< SharedGeometryDataControlFile > GeometryDataControlFileFactory;


94
95
} // OpenGL
} // ACGL
96
#endif
97
#endif // ACGL_OPENGL_CONTROLLER_DATACONTROLFILEFACTORY_HH