weaklytypedpointer.h 5.85 KB
Newer Older
schultezub's avatar
schultezub committed
1
2
3
4
#ifndef WEAKLYTYPEDPOINTER_H__
#define WEAKLYTYPEDPOINTER_H__

#include "tgt/assert.h"
5
6
7
#include "tgt/logmanager.h"
#include "tgt/texture.h"
#include "tgt/tgt_gl.h"
schultezub's avatar
schultezub committed
8
#include "tgt/types.h"
schultezub's avatar
schultezub committed
9
10
11

namespace TUMVis {

schultezub's avatar
schultezub committed
12
13
    /**
     * Struct for handling void pointers slightly more typesafe.
14
15
     * Therfore it stores an enum value together with the pointer for deducing its data type.
     * \note    WeaklyTypedPointer takes _not_ take ownership of its pointer, hence it won't take care of deleting it!
16
     */
schultezub's avatar
schultezub committed
17
    struct WeaklyTypedPointer {
schultezub's avatar
schultezub committed
18
19
20
        /**
         * Base data type.
         **/
21
        enum BaseType {
schultezub's avatar
schultezub committed
22
23
24
25
26
27
            UINT8,      ///< unsigned 8 bit integer
            INT8,       ///< signed 8 bit integer
            UINT16,     ///< unsigned 16 bit integer
            INT16,      ///< signed 16 bit integer
            UINT32,     ///< unsigned 32 bit integer
            INT32,      ///< signed 32 bit integer
schultezub's avatar
schultezub committed
28
            FLOAT,      ///< float
schultezub's avatar
schultezub committed
29
            UINT64,     ///< unsigned 64 bit integer
30
            INT64,      ///< signed 64 bit integer
schultezub's avatar
schultezub committed
31
            DOUBLE      ///< double
schultezub's avatar
schultezub committed
32
33
34
35
        };

        /**
         * Returns the number of bytes occupied by one element of the type \a pt.
36
37
38
         * 
         * \note    There is a compiletime version in WeaklyTypedPointerTraits.
         * \sa      WeaklyTypedPointerTraits::numBytes()
schultezub's avatar
schultezub committed
39
         * \param   pt  Data type to check.
40
41
         * \returns The number of bytes occupied by one element of the type \a pt.
         */
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
        static size_t numBytes(BaseType pt, size_t numChannels);

        /**
         * Calculates the number of channels for the given OpenGL format.
         * Kinda inverse function of getGlFormat().
         * \sa      WeaklyTypedPointer::getGlFormat()
         * \param   glFormat    OpenGL format
         * \return  Number of channels of the given OpenGL format.
         */
        static size_t numChannels(GLint glFormat);

        /**
         * Calculates the base type for the given OpenGL data type.
         * Kinda inverse function of getGlDataType().
         * \sa      WeaklyTypedPointer::getGlDataTypet()
         * \param   glDataType  OpenGL format
         * \return  Base type of the given OpenGL data type.
         */
        static WeaklyTypedPointer::BaseType baseType(GLenum glDataType);

// = Starting with the real stuff =================================================================
schultezub's avatar
schultezub committed
63
64

        /**
schultezub's avatar
schultezub committed
65
         * Constructs a new weakly typed pointer.
66
67
68
         * \param pt            Base data type of the pointer.
         * \param numChannels   Number of channels, must be in [1, 4]!
         * \param ptr           Pointer to the data, WeaklyTypedPointer will take ownership of it.
schultezub's avatar
schultezub committed
69
         */
70
71
72
73
74
75
76
        WeaklyTypedPointer(BaseType pt, size_t numChannels, void* ptr);

        /**
         * Destructor, does _not_ delete the handled pointer!
         */
        virtual ~WeaklyTypedPointer();

schultezub's avatar
schultezub committed
77

schultezub's avatar
schultezub committed
78
79
80
81
        /**
         * Returns the number of bytes occupied by one element of the type of this pointer.
         * \returns The number of bytes occupied by one element of the type of this pointer.
         */
82
83
84
85
86
        size_t getNumBytesPerElement() const;

        GLint getGlFormat() const;

        GLenum getGlDataType() const;
schultezub's avatar
schultezub committed
87

88
89
90
91
        GLint getGlInternalFormat() const;

        BaseType _pointerType;      ///< Base data type of the pointer
        size_t _numChannels;        ///< Number of channels, must be in [1, 4]!
schultezub's avatar
schultezub committed
92
        void* _pointer;             ///< Pointer to the data
93
94
95
96

    private:

        static const std::string loggerCat_;
schultezub's avatar
schultezub committed
97
98
    };

99
// = Some traits for WeaklyTypedPointers ==========================================================
100
101
102
103
104

    /**
     * Some compile time traits for WeaklyTypedPointers.
     * \tparam  pt  Data type to check.
     */
105
    template<WeaklyTypedPointer::BaseType pt>
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
    struct WeaklyTypedPointerTraits {
        /**
         * Returns the number of bytes occupied by one element of the given type.
         * 
         * \note    There is a runtime version in WeaklyTypedPointer.
         * \sa      WeaklyTypedPointer::numBytes()
         * \tparam  pt  Data type to check.
         * \returns The number of bytes occupied by one element of the given type.
         */
        static size_t numBytes() { return 0; };
    };

// - Template Specialization ----------------------------------------------------------------------

    template<>
schultezub's avatar
schultezub committed
121
    struct WeaklyTypedPointerTraits<WeaklyTypedPointer::UINT8> {
122
        static size_t numBytes() { return 1; };
123
124
125
    };

    template<>
schultezub's avatar
schultezub committed
126
127
    struct WeaklyTypedPointerTraits<WeaklyTypedPointer::INT8> {
        static size_t numBytes() { return 1; };
128
129
130
    };

    template<>
schultezub's avatar
schultezub committed
131
132
    struct WeaklyTypedPointerTraits<WeaklyTypedPointer::UINT16> {
        static size_t numBytes() { return 2; };
133
134
135
    };

    template<>
schultezub's avatar
schultezub committed
136
137
    struct WeaklyTypedPointerTraits<WeaklyTypedPointer::INT16> {
        static size_t numBytes() { return 2; };
138
139
140
    };

    template<>
schultezub's avatar
schultezub committed
141
142
    struct WeaklyTypedPointerTraits<WeaklyTypedPointer::UINT32> {
        static size_t numBytes() { return 4; };
143
144
145
    };

    template<>
schultezub's avatar
schultezub committed
146
147
    struct WeaklyTypedPointerTraits<WeaklyTypedPointer::INT32> {
        static size_t numBytes() { return 4; };
148
149
150
151
152
153
154
155
    };

    template<>
    struct WeaklyTypedPointerTraits<WeaklyTypedPointer::FLOAT> {
        static size_t numBytes() { return sizeof(float); };
    };

    template<>
schultezub's avatar
schultezub committed
156
157
    struct WeaklyTypedPointerTraits<WeaklyTypedPointer::UINT64> {
        static size_t numBytes() { return 8; };
158
159
160
    };

    template<>
schultezub's avatar
schultezub committed
161
162
    struct WeaklyTypedPointerTraits<WeaklyTypedPointer::INT64> {
        static size_t numBytes() { return 8; };
163
164
165
166
167
168
169
170
    };

    template<>
    struct WeaklyTypedPointerTraits<WeaklyTypedPointer::DOUBLE> {
        static size_t numBytes() { return sizeof(double); };
    };


schultezub's avatar
schultezub committed
171
172
173
}

#endif // WEAKLYTYPEDPOINTER_H__