datahandle.h 2.63 KB
Newer Older
schultezub's avatar
schultezub committed
1
2
3
4
#ifndef datahandle_h__
#define datahandle_h__

namespace TUMVis {
5
    class AbstractData;
schultezub's avatar
schultezub committed
6
7
8

    /**
     * A DataHandle is responsible to manage the lifetime of an AbstractData instance.
9
     * Therefore, it implements a reference counting technique in cooperation with AbstractData.
schultezub's avatar
schultezub committed
10
     *
11
12
13
14
15
16
     * \note    For clarity: An AbstractData instance can be referenced by multiple DataHandles. As soon 
     *          as it is afterwards reference by 0 DataHandles the AbstractData instance will be destroyed.
     *          Also remember that a DataHandle takes ownership of the given AbstractData instance. So do
     *          not delete it once it has been assigned to a DataHandle (respectively DataContainer) or mess
     *          with its reference counting!
     * \note    Reference counting implementation inspired from Scott Meyers: More Effective C++, Item 29
schultezub's avatar
schultezub committed
17
     *
18
     * \todo    Check for thread-safety
schultezub's avatar
schultezub committed
19
20
21
22
     */
    class DataHandle {
    public:
        /**
23
24
25
26
         * Creates a new DataHandle for the given data.
         * \note    By passing the data to DataHandle you will transfer its ownership to the reference
         *          counting mechanism. Make sure not to interfere with it or delete \a data yourself!
         * \param   data    Data for the DataHandle
schultezub's avatar
schultezub committed
27
         */
28
        DataHandle(AbstractData* data);
schultezub's avatar
schultezub committed
29
30

        /**
31
32
33
         * Copy-constructor
         * \note    If \a rhs is not shareable, this implies a copy of the data!
         * \param   rhs     Source DataHandle
schultezub's avatar
schultezub committed
34
         */
35
        DataHandle(const DataHandle& rhs);
schultezub's avatar
schultezub committed
36
37

        /**
38
39
40
41
         * Assignment operator
         * \note    If \a rhs is not shareable, this implies a copy of the data!
         * \param   rhs     source DataHandle
         * \return  *this
schultezub's avatar
schultezub committed
42
         */
43
        DataHandle& operator=(const DataHandle& rhs);
44

schultezub's avatar
schultezub committed
45
        /**
46
         * Destructor, will delete the managed AbstractData.
schultezub's avatar
schultezub committed
47
         */
48
        virtual ~DataHandle();
schultezub's avatar
schultezub committed
49
50

        /**
51
52
         * Grants const access to the managed AbstractData instance.
         * \return  _data;
schultezub's avatar
schultezub committed
53
         */
54
        const AbstractData* getData() const;
schultezub's avatar
schultezub committed
55
56

        /**
57
58
59
         * Grants access to the managed AbstractData instance.
         * \note    If the data is referenced by more than one object, this implies a copy of the data!
         * \return  A modifyable version of the held data.
schultezub's avatar
schultezub committed
60
         */
61
        AbstractData* getData();
schultezub's avatar
schultezub committed
62
63


64
    private:
schultezub's avatar
schultezub committed
65
        /**
66
         * Initializes the reference counting for the data.
schultezub's avatar
schultezub committed
67
         */
68
        void init();
schultezub's avatar
schultezub committed
69

70
        AbstractData* _data;                          ///< managed data
schultezub's avatar
schultezub committed
71
72
73
74
75
    };

}

#endif // datahandle_h__