Currently job artifacts in CI/CD pipelines on LRZ GitLab never expire. Starting from Wed 26.1.2022 the default expiration time will be 30 days (GitLab default). Currently existing artifacts in already completed jobs will not be affected by the change. The latest artifacts for all jobs in the latest successful pipelines will be kept. More information: https://gitlab.lrz.de/help/user/admin_area/settings/continuous_integration.html#default-artifacts-expiration

imagerepresentationconverter.h 5.55 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
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
70
71
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
// 
// For a full list of authors and contributors, please refer to the file "AUTHORS.txt".
// 
// 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
// 
// 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.
// 
// ================================================================================================

#ifndef IMAGEREPRESENTATIONCONVERTER_H__
#define IMAGEREPRESENTATIONCONVERTER_H__

#include "tgt/logmanager.h"
#include "tgt/singleton.h"

#include <tbb/atomic.h>
#include <tbb/spin_mutex.h>

#include "core/coreapi.h"
#include "core/datastructures/abstractimagerepresentation.h"

#include <map>
#include <string>
#include <typeindex>
#include <typeinfo>
#include <vector>

class QWidget;

namespace campvis {
    /**
     * Factory for creating PropertyWidgets depending on the Property type.
     * Using some template-magic, ImageRepresentationConverter is able to register PropertyWidgets during static 
     * initialization in cooperation with the ConversionFunctionRegistrar.
     * 
     * \note    ImageRepresentationConverter is a thread-safe lazy-instantiated singleton.
     */
    class CAMPVIS_CORE_API ImageRepresentationConverter {
    public:
        /// Typedef for a function pointer to convert between image representations.
        typedef const AbstractImageRepresentation* (*ConversionFunctionPointer) (const AbstractImageRepresentation*);

        /**
         * Returns a reference to the PipelineFactory singleton.
         * Creates the singleton in a thread-safe fashion, if necessary.
         * \return  *_singleton
         */
        static ImageRepresentationConverter& getRef();
    
        /**
         * Deinitializes the Singleton.
         */
        static void deinit();

        /**
         * Registers the the property of type \a type to have widgets created with the given function pointers.
         * \note    The template instantiation of ConversionFunctionRegistrar takes care of calling this method.
         * \param   type        Property type to register.
         * \param   funcPtr     Pointer to a conversion function.
         * \return 
         */
        size_t registerConversionFunction(const std::type_info& type, ConversionFunctionPointer funcPtr);


        template<typename T>
        const T* tryConvertFrom(const AbstractImageRepresentation* source) {
            auto itPair = _conversionFunctionMap.equal_range(std::type_index(typeid(T)));

            // try conversion with all registered convertes for the given target type
            for (auto it = itPair.first; it != itPair.second; ++it) {
                const T* tester = static_cast<const T*>(it->second(source));
                if (tester != nullptr)
                    return tester;
            }

            return nullptr;
        };

    private:
        mutable tbb::spin_mutex _mutex;                                 ///< Mutex protecting the singleton during initialization
        static tbb::atomic<ImageRepresentationConverter*> _singleton;   ///< the singleton object

        /// Typedef for the map associating property types with conversion function pointers
        typedef std::multimap<std::type_index, ConversionFunctionPointer> ConversionFunctionMapType;
        /// map associating property types with creator function pointers
        ConversionFunctionMapType _conversionFunctionMap;
    };


// ================================================================================================

    /**
     * Templated class exploiting the CRTP to allow easy registration of property widgets crossing
     * DLL bounds using a single template instantiation.
     * \tparam  WidgetType      Property widget type to register.
     */
    template<typename ConversionTarget, typename ConverterClass>
    class ConversionFunctionRegistrar {
    public:
        static const AbstractImageRepresentation* tryConvertFrom(const AbstractImageRepresentation* source) {
            return ConverterClass::tryConvertFrom(source);
        }

    private:
        /// static helper field to ensure registration at static initialization time.
121
        static size_t _factoryId;
122
123
124
125
126
    };

    // The meat of the static factory registration, call the registerConversionFunction() method and use the 
    // returned size_t to store it in the static member and thus ensure calling during static initialization.
    template<typename ConversionTarget, typename ConverterClass>
127
    size_t ConversionFunctionRegistrar<ConversionTarget, ConverterClass>::_factoryId 
128
129
130
131
132
133
134
135
        = ImageRepresentationConverter::getRef().registerConversionFunction(
            typeid(ConversionTarget), 
            &ConversionFunctionRegistrar<ConversionTarget, ConverterClass>::tryConvertFrom
        );

}

#endif // IMAGEREPRESENTATIONCONVERTER_H__