WModuleFactory.cpp 8.41 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
//---------------------------------------------------------------------------
//
// Project: OpenWalnut ( http://www.openwalnut.org )
//
// Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
// For more information see http://www.openwalnut.org/copying
//
// This file is part of OpenWalnut.
//
// OpenWalnut is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// OpenWalnut is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
//
//---------------------------------------------------------------------------

25
#include <set>
Sebastian Eichelbaum's avatar
[STYLE]  
Sebastian Eichelbaum committed
26
#include <string>
27 28
#include <iostream>
#include <typeinfo>
29 30 31

#include "../common/WLogger.h"

32
#include "../modules/data/WMData.h"
33 34 35 36 37 38
#include "../modules/navSlices/WMNavSlices.h"
#include "../modules/coordinateSystem/WMCoordinateSystem.h"
#include "../modules/fiberDisplay/WMFiberDisplay.h"
#include "../modules/fiberCulling/WMFiberCulling.h"
#include "../modules/fiberClustering/WMFiberClustering.h"
#include "../modules/marchingCubes/WMMarchingCubes.h"
39
#include "../modules/distanceMap/WMDistanceMap.h"
40
#include "../modules/eegTest/WMEEGTest.h"
41
#include "../modules/textureList/WMTextureList.h"
schurade's avatar
schurade committed
42
#include "../modules/hud/WMHud.h"
43
#include "../modules/eegView/WMEEGView.h"
44
#include "../modules/prototypeBoxManipulation/WMPrototypeBoxManipulation.h"
45 46

#include "exceptions/WPrototypeUnknown.h"
47
#include "exceptions/WPrototypeNotUnique.h"
48

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
#include "WModuleFactory.h"

// factory instance as singleton
boost::shared_ptr< WModuleFactory > WModuleFactory::m_instance = boost::shared_ptr< WModuleFactory >();

WModuleFactory::WModuleFactory()
{
    // initialize members
}

WModuleFactory::~WModuleFactory()
{
    // cleanup
}

64 65 66 67 68 69 70 71 72
void WModuleFactory::load()
{
    // load modules
    WLogger::getLogger()->addLogMessage( "Loading Modules", "ModuleFactory", LL_INFO );

    // operation must be exclusive
    boost::unique_lock< boost::shared_mutex > lock = boost::unique_lock< boost::shared_mutex >( m_prototypesLock );

    // currently the prototypes are added by hand. This will be done automatically later.
73
    m_prototypes.insert( boost::shared_ptr< WModule >( new WMData() ) );
74 75 76 77 78 79 80
    m_prototypes.insert( boost::shared_ptr< WModule >( new WMNavSlices() ) );
    m_prototypes.insert( boost::shared_ptr< WModule >( new WMFiberDisplay() ) );
    m_prototypes.insert( boost::shared_ptr< WModule >( new WMFiberCulling() ) );
    m_prototypes.insert( boost::shared_ptr< WModule >( new WMFiberClustering() ) );
    m_prototypes.insert( boost::shared_ptr< WModule >( new WMCoordinateSystem() ) );
    m_prototypes.insert( boost::shared_ptr< WModule >( new WMEEGTest() ) );
    m_prototypes.insert( boost::shared_ptr< WModule >( new WMMarchingCubes() ) );
81
    m_prototypes.insert( boost::shared_ptr< WModule >( new WMDistanceMap() ) );
82
    m_prototypes.insert( boost::shared_ptr< WModule >( new WMTextureList() ) );
schurade's avatar
schurade committed
83
    m_prototypes.insert( boost::shared_ptr< WModule >( new WMHud() ) );
84
    m_prototypes.insert( boost::shared_ptr< WModule >( new WMEEGView() ) );
85
    m_prototypes.insert( boost::shared_ptr< WModule >( new WMPrototypeBoxManipulation() ) );
86 87 88 89 90 91 92

    lock.unlock();

    // for this a read lock is sufficient
    boost::shared_lock< boost::shared_mutex > slock = boost::shared_lock< boost::shared_mutex >( m_prototypesLock );

    // initialize every module in the set
93 94 95
    std::set< std::string > names;  // helper to find duplicates
    for( std::set< boost::shared_ptr< WModule > >::iterator listIter = m_prototypes.begin(); listIter != m_prototypes.end();
            ++listIter )
96
    {
97
        WLogger::getLogger()->addLogMessage( "Loading module: \"" + ( *listIter )->getName() + "\"", "ModuleFactory", LL_INFO );
Sebastian Eichelbaum's avatar
[STYLE]  
Sebastian Eichelbaum committed
98

99 100 101
        // that should not happen. Names should not occur multiple times since they are unique
        if ( names.count( ( *listIter )->getName() ) )
        {
102
            throw WPrototypeNotUnique( "Module \"" + ( *listIter )->getName() + "\" is not unique. Modules have to have a unique name." );
103 104 105 106
        }
        names.insert( ( *listIter )->getName() );

        ( *listIter )->initialize();
107 108 109 110 111
    }

    slock.unlock();
}

112 113
boost::shared_ptr< WModule > WModuleFactory::create( boost::shared_ptr< WModule > prototype )
{
114 115 116 117 118 119
    // for this a read lock is sufficient
    boost::shared_lock< boost::shared_mutex > slock = boost::shared_lock< boost::shared_mutex >( m_prototypesLock );

    // ensure this one is a prototype and nothing else
    if ( m_prototypes.count( prototype ) == 0 )
    {
120
        throw WPrototypeUnknown( "Could not clone module \"" + prototype->getName() + "\" since it is no prototype." );
121 122 123 124
    }

    slock.unlock();

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
    // call prototypes factory function
    boost::shared_ptr< WModule > clone = boost::shared_ptr< WModule >( prototype->factory() );
    clone->initialize();

    return clone;
}

boost::shared_ptr< WModuleFactory > WModuleFactory::getModuleFactory()
{
    if ( !m_instance )
    {
        m_instance = boost::shared_ptr< WModuleFactory >( new WModuleFactory() );
    }

    return m_instance;
}

142 143 144 145 146 147

const boost::shared_ptr< WModule > WModuleFactory::getPrototypeByName( std::string name )
{
    // for this a read lock is sufficient
    boost::shared_lock< boost::shared_mutex > slock = boost::shared_lock< boost::shared_mutex >( m_prototypesLock );

Sebastian Eichelbaum's avatar
[STYLE]  
Sebastian Eichelbaum committed
148
    // find first and only prototype (ensured during load())
149 150 151 152 153 154 155 156 157 158 159
    boost::shared_ptr< WModule > ret = boost::shared_ptr< WModule >();
    for( std::set< boost::shared_ptr< WModule > >::iterator listIter = m_prototypes.begin(); listIter != m_prototypes.end();
            ++listIter )
    {
        if ( ( *listIter )->getName() == name )
        {
            ret = ( *listIter );
            break;
        }
    }

Sebastian Eichelbaum's avatar
[STYLE]  
Sebastian Eichelbaum committed
160
    slock.unlock();
161 162 163 164

    // if not found -> throw
    if ( ret == boost::shared_ptr< WModule >() )
    {
165
        throw WPrototypeUnknown( "Could not find prototype \"" + name + "\"." );
166 167 168 169 170
    }

    return ret;
}

171
const boost::shared_ptr< WModule > WModuleFactory::getPrototypeByInstance( boost::shared_ptr< WModule > instance )
172
{
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
    return getPrototypeByName( instance->getName() );
}

std::set< boost::shared_ptr< WModule > > WModuleFactory::getCompatiblePrototypes( boost::shared_ptr< WModule > module )
{
    std::set< boost::shared_ptr < WModule > > compatibles;

    // for this a read lock is sufficient
    boost::shared_lock< boost::shared_mutex > slock = boost::shared_lock< boost::shared_mutex >( m_prototypesLock );

    // get offered outputs
    std::set<boost::shared_ptr<WModuleOutputConnector> > cons = module->getOutputConnectors();
    if ( cons.size() == 0 )
    {
        // in this case return the empty list
        return compatibles;
    }
    // TODO(ebaum): see ticket #178 for this
    if ( cons.size() > 1 )
    {
        WLogger::getLogger()->addLogMessage( "Can not find compatibles for modules with more than 1 output connector. Using "
                + ( *cons.begin() )->getCanonicalName() + " for compatibility check.", "ModuleFactory", LL_WARNING );
    }

    // go through every prototype
    for( std::set< boost::shared_ptr< WModule > >::iterator listIter = m_prototypes.begin(); listIter != m_prototypes.end();
            ++listIter )
    {
        // get connectors of this prototype
        std::set<boost::shared_ptr<WModuleInputConnector> > pcons = ( *listIter )->getInputConnectors();

        // ensure we have 1 connector
        if ( pcons.size() == 0 )
        {
            continue;
        }
        if ( pcons.size() > 1 )
        {
            WLogger::getLogger()->addLogMessage( "Can not find compatibles for modules with more than 1 input connector. Using "
                    + ( *pcons.begin() )->getCanonicalName() + " for compatibility check.", "ModuleFactory", LL_WARNING );
        }

        // check whether the outputs are compatible with the inputs of the prototypes
216
        if (   ( *cons.begin() )->connectable( *pcons.begin() )  &&  ( *pcons.begin() )->connectable( *cons.begin() ) )
217 218 219 220 221 222 223
        {
            // it is compatible -> add to list
            compatibles.insert( *listIter );
        }
    }

    slock.unlock();
224

225
    return compatibles;
226 227
}