The name of the initial branch for new projects is now "main" instead of "master". Existing projects remain unchanged. More information: https://doku.lrz.de/display/PUBLIC/GitLab

genericpointerdata.h 4.21 KB
Newer Older
1 2 3 4
// ================================================================================================
// 
// This file is part of the CAMPVis Software Framework.
// 
5
// If not explicitly stated otherwise: Copyright (C) 2012-2014, all rights reserved,
6 7
//      Christian Schulte zu Berge <christian.szb@in.tum.de>
//      Chair for Computer Aided Medical Procedures
8 9
//      Technische Universitaet Muenchen
//      Boltzmannstr. 3, 85748 Garching b. Muenchen, Germany
10
// 
11 12
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
// 
13 14 15 16
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 
// except in compliance with the License. You may obtain a copy of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
17
// 
18 19 20 21
// Unless required by applicable law or agreed to in writing, software distributed under the 
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
// either express or implied. See the License for the specific language governing permissions 
// and limitations under the License.
22 23 24 25 26 27 28 29 30
// 
// ================================================================================================

#ifndef GENERICPOINTERDATA_H__
#define GENERICPOINTERDATA_H__


#include "core/datastructures/abstractdata.h"
#include <string>
31
#include <memory>
32 33 34 35 36

namespace campvis {

    /**
     * Class that generically wraps around a pointer of the template type and takes ownership of it.
37
     * Ownership is handled through shared pointers, as the clone() method only returns a shallow copy.
38 39 40 41 42 43 44
     * \tparam  T   Type of the pointer this AbstractData wraps around.
     */
    template<typename T>
    class GenericPointerData : public AbstractData {
    public:
        /**
         * Creates a new GenericPointerData and initializes its pointer with \a data.
45
         * \param   data    The initial pointer for this data, may be 0, GenericPointerData takes ownership.
46
         */
47
        explicit GenericPointerData(T* data)
48 49 50 51 52
            : AbstractData()
            , _data(data)
        {};

        /**
53 54 55 56 57 58 59 60 61 62
         * Creates a new GenericPointerData and initializes its pointer with \a data.
         * \param   data    The initial pointer for this data, may be 0, GenericPointerData takes ownership.
         */
        explicit GenericPointerData(std::shared_ptr<T> data)
            : AbstractData()
            , _data(data)
        {};

        /**
         * Destructor
63 64
         */
        virtual ~GenericPointerData() {
65

66 67 68 69 70 71 72
        };

        /**
         * Returns the pointer to the wrapped data.
         * \return  _data, may be 0, GenericPointerData has ownership
         */
        const T* getData() const {
73
            return _data.get();
74 75 76 77 78 79 80
        };

        /**
         * Returns the pointer to the wrapped data.
         * \return  _data, may be 0, GenericPointerData has ownership
         */
        T* getData() {
81
            return _data.get();
82 83 84 85
        };

        /**
         * Sets the data to \a data.
86
         * \param   data    The new pointer for this data, may be 0, GenericPointerData takes ownership.
87 88
         */
        void setData(T* data) {
89
            _data = std::shared_ptr<T>(data);
90 91 92 93 94 95 96
        };


        /**
         * Prototype - clone method, some people call this virtual constructor...
         * \return  A SHALLOW copy of this object.
         */
97
        virtual GenericPointerData<T>* clone() const {
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
            return new GenericPointerData<T>(_data);
        };

        /**
         * Returns the local memory footprint of the data in bytes.
         * \return  Number of bytes occupied in local memory by the data.
         */
        virtual size_t getLocalMemoryFootprint() const {
            return sizeof(T) + sizeof(T*);
        };

        /**
         * Returns the video memory footprint of the data in bytes.
         * \return  Number of bytes occupied in video memory by the data.
         */
        virtual size_t getVideoMemoryFootprint() const {
            return 0;
        };

    protected:
118
        std::shared_ptr<T> _data;       ///< Shared pointer to the data.
119 120 121 122
    };
}

#endif // GENERICPOINTERDATA_H__