File: factory.cpp

package info (click to toggle)
sight 25.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 42,184 kB
  • sloc: cpp: 289,476; xml: 17,257; ansic: 9,878; python: 1,379; sh: 144; makefile: 33
file content (603 lines) | stat: -rw-r--r-- 20,562 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
/************************************************************************
 *
 * Copyright (C) 2009-2025 IRCAD France
 * Copyright (C) 2012-2020 IHU Strasbourg
 *
 * This file is part of Sight.
 *
 * Sight 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.
 *
 * Sight 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 Sight. If not, see <https://www.gnu.org/licenses/>.
 *
 ***********************************************************************/

#include "service/extension/factory.hpp"

#include "service/base.hpp"

#include <core/lazy_instantiator.hpp>
#include <core/runtime/helper.hpp>
#include <core/runtime/runtime.hpp>

#include <data/exception.hpp>

#include <algorithm>
#include <functional>
#include <vector>

namespace sight::service::extension
{

//-----------------------------------------------------------------------------

factory::sptr factory::get()
{
    return core::lazy_instantiator<factory>::get_instance();
}

//-----------------------------------------------------------------------------

void factory::parse_plugin_infos()
{
    srv_reg_container_t module_info_map;

    using extension_t = std::shared_ptr<core::runtime::extension>;

    std::vector<extension_t> ext_elements;
    ext_elements = core::runtime::get_all_extensions_for_point("sight::service::extension::factory");
    for(const extension_t& ext_elt : ext_elements)
    {
        const core::runtime::config_t& cfg_elt_vec = ext_elt->get_config();
        std::string type;
        std::string service;
        std::vector<std::string> objects;
        std::string desc;
        std::string tags;

        for(const auto& cfg_elt : cfg_elt_vec)
        {
            const std::string elt = cfg_elt.first;
            const auto value      = cfg_elt.second.get_value<std::string>();
            if(elt == "type")
            {
                type = core::runtime::filter_id(value);
            }
            else if(elt == "service")
            {
                service = core::runtime::filter_id(value);
            }
            else if(elt == "object")
            {
                objects.push_back(core::runtime::filter_id(value));
            }
            else if(elt == "desc")
            {
                desc = value;
            }
            else if(elt == "tags")
            {
                tags = value;
            }
            else
            {
                SIGHT_FATAL("Unknown element !");
            }
        }

        SIGHT_ASSERT("Missing type element.", !type.empty());
        SIGHT_ASSERT("Missing service element.", !service.empty());

        service_info info;
        info.service_type            = type;
        info.objects_set_from_module = !objects.empty();
        info.object_impl             = std::move(objects);
        info.desc                    = desc;
        info.tags                    = tags;
        info.module                  = ext_elt->get_module();
        SIGHT_ASSERT("Module not found.", info.module);

        module_info_map.emplace(std::make_pair(service, info));
    }

    sight::service::extension::factory::print_info_map(module_info_map);

    core::mt::read_to_write_lock lock(m_srv_impl_to_srv_info_mutex);
    // Merge data info
    for(const auto& module : module_info_map)
    {
        auto iter = m_srv_impl_to_srv_info.find(module.first);

        if(iter != m_srv_impl_to_srv_info.end())
        {
            SIGHT_DEBUG(
                "We already have informations about this service  (from register macro) ( " << module.first
                << " )."
            );

            service_info& info              = iter->second;
            const service_info& info_module = module.second;

            SIGHT_ASSERT("Try to add a module, but this module already exists.", !info.module);
            SIGHT_ASSERT(
                "Try to add a module, but this srv is already registered and doesn't have the same srv type.",
                info_module.service_type == info.service_type
            );
            SIGHT_ASSERT(
                "Try to add a module, but the service '"
                << module.first << "' is already registered and does not have the same objects.",
                info_module.object_impl.empty() || info_module.object_impl == info.object_impl
            );

            info.module                  = info_module.module;
            info.desc                    = info_module.desc;
            info.objects_set_from_module = info_module.objects_set_from_module;
        }
        else
        {
            core::mt::upgrade_to_write_lock upgrade(lock);
            m_srv_impl_to_srv_info.emplace(std::make_pair(module.first, module.second));
        }
    }

    sight::service::extension::factory::print_info_map(m_srv_impl_to_srv_info);
    this->check_services_not_declared_in_plugin_xml();
}

//-----------------------------------------------------------------------------

base::sptr factory::create(const std::string& _srv_impl) const
{
    const std::string srv_impl = core::runtime::filter_id(_srv_impl);
    base::sptr service;

    core::mt::read_lock lock(m_srv_impl_to_srv_info_mutex);
    auto iter = m_srv_impl_to_srv_info.find(srv_impl);

    SIGHT_ASSERT(
        "The service called '" << srv_impl << "' does not exist in the factory ",
        iter != m_srv_impl_to_srv_info.end()
    );

    const service_info& info = iter->second;

    SIGHT_DEBUG("SR creates a new service ( classname = " << srv_impl << " )");

    if(info.factory)
    {
        service = info.factory();
    }
    else
    {
        SIGHT_ASSERT(
            "A module must declare the factory named"
            << srv_impl
            << ", the service declaration might be missing (or misspelled) in a module plugin.",
            info.module
        );
        SIGHT_ASSERT(
            "The module '" + info.module->identifier() + "' is already loaded and the factory '"
            + srv_impl + "' is still missing. The service declaration might be missing (or misspelled)"
                         "in a .cpp file.",
            !info.module->is_started()
        );

        lock.unlock(); // module->start() may trigger calls to add_factory
        info.module->start();
        lock.lock();

        SIGHT_THROW_EXCEPTION_IF(
            data::exception(
                "After loading the module " + info.module->identifier() + " , factory " + srv_impl
                + " is still missing. The service declaration might be missing (or misspelled) "
                  "in a cpp file."
            ),
            !info.factory
        );

        service = info.factory();
    }

    // Setup worker here, this is a better place than the constructor
    // because here, the service slots are also set up
    service->set_worker(core::thread::get_default_worker());

    return service;
}

//------------------------------------------------------------------------------

void factory::add_service_factory(
    factory_t _factory,
    const std::string& _srv_impl,
    const std::string& _srv_type
)
{
    const std::string srv_impl = core::runtime::filter_id(_srv_impl);
    const std::string srv_type = core::runtime::filter_id(_srv_type);

    SIGHT_DEBUG(std::string("New service registering : srvImpl =") + srv_impl + " srvType=" + srv_type);

    core::mt::read_to_write_lock lock(m_srv_impl_to_srv_info_mutex);
    auto iter = m_srv_impl_to_srv_info.find(srv_impl);

    if(iter != m_srv_impl_to_srv_info.end())
    {
        SIGHT_DEBUG("We already have informations about this service ( " + srv_impl + " ).");
        service_info& info = iter->second;
        SIGHT_ASSERT(
            "Try to add factory, but this srv ( " << srv_impl << " ) already has a registered factory.",
            !info.factory
        );
        SIGHT_ASSERT(
            "Try to add factory, but this srv ( "
            << srv_impl << " ) is already registered and doesn't have the same srv type. ( "
            << srv_type << " != " << info.service_type << " )",
            srv_type == info.service_type
        );

        core::mt::upgrade_to_write_lock upgrade(lock);
        info.factory = _factory;
    }
    else
    {
        SIGHT_DEBUG("Add new service factory in registry ( " + srv_impl + " ).");
        core::mt::upgrade_to_write_lock upgrade(lock);
        service_info info;
        info.service_type = srv_type;
        info.factory      = _factory;
        m_srv_impl_to_srv_info.emplace(std::make_pair(srv_impl, info));
    }
}

//-----------------------------------------------------------------------------

void factory::add_object_factory(const std::string& _srv_impl, const std::string& _oimpl)
{
    const std::string srv_impl = core::runtime::filter_id(_srv_impl);
    const std::string o_impl   = core::runtime::filter_id(_oimpl);

    SIGHT_DEBUG(std::string("New object oImpl=") + o_impl + "registering to service: srvImpl =" + srv_impl);
    SIGHT_ASSERT("Empty oImpl", !o_impl.empty());

    core::mt::read_to_write_lock lock(m_srv_impl_to_srv_info_mutex);
    auto iter = m_srv_impl_to_srv_info.find(srv_impl);

    SIGHT_ASSERT(
        "Try to associate an object to a service factory, but this srv is not yet registered.",
        iter != m_srv_impl_to_srv_info.end()
    );

    if(iter != m_srv_impl_to_srv_info.end())
    {
        service_info& info = iter->second;

        // Either the module does not contain objects informations or this service does not belong to a module
        if(info.objects_set_from_module)
        {
#ifdef _DEBUG
            const auto it_find = std::ranges::find(info.object_impl, o_impl);
#endif
            SIGHT_ASSERT(
                "Try to add factory, but the service '" + srv_impl + "' is already registered and does not have the "
                                                                     "same objects.",
                info.object_impl.empty() || it_find != info.object_impl.end()
            );
        }
        else
        {
            core::mt::upgrade_to_write_lock upgrade(lock);
            info.object_impl.push_back(o_impl);
        }
    }
}

//-----------------------------------------------------------------------------

void factory::print_info_map(const srv_reg_container_t& _src)
{
    // not thread-safe

    //Print information
    for(const auto& srv_reg : _src)
    {
        SIGHT_DEBUG(" Service name = " << srv_reg.first);
        SIGHT_DEBUG("  - type   = " << srv_reg.second.service_type);

#if SIGHT_DEBUG_ENABLED
        std::size_t objNum = 0;
        for(const auto& objImpl : srvReg.second.objectImpl)
        {
            SIGHT_DEBUG("  - object " << objNum++ << " = " << objImpl)
        }
#endif

        SIGHT_DEBUG_IF("  - module = " << srv_reg.second.module->identifier(), srv_reg.second.module);
        SIGHT_DEBUG_IF("  - module = ( no module registered )", !srv_reg.second.module);

        SIGHT_DEBUG_IF(
            "  - name after creation = "
            << srv_reg.second.factory()->get_classname(),
            srv_reg.second.factory
        );
        SIGHT_DEBUG_IF("  - name after creation = ( no factory registered )", !srv_reg.second.factory);
    }
}

//-----------------------------------------------------------------------------

void factory::check_services_not_declared_in_plugin_xml() const
{
    // not thread-safe
    //Print information
    for(const auto& srv_reg : m_srv_impl_to_srv_info)
    {
        if(!srv_reg.second.module)
        {
            SIGHT_WARN("Service " << srv_reg.first << " is not declared/found in a plugin.xml.");
        }
    }
}

//-----------------------------------------------------------------------------

void factory::clear_factory()
{
    core::mt::write_lock lock(m_srv_impl_to_srv_info_mutex);
    m_srv_impl_to_srv_info.clear();
}

//-----------------------------------------------------------------------------

std::vector<std::string> factory::get_implementation_id_from_object_and_type(
    const std::string& _object,
    const std::string& _type
) const
{
    const std::string object = core::runtime::filter_id(_object);
    const std::string type   = core::runtime::filter_id(_type);

    std::vector<std::string> service_impl;

    core::mt::read_lock lock(m_srv_impl_to_srv_info_mutex);
    for(const auto& srv : m_srv_impl_to_srv_info)
    {
        const service_info& srv_info = srv.second;
        for(const auto& oimpl : srv_info.object_impl)
        {
            if(srv_info.service_type == type
               && (oimpl == object || oimpl == "sight::data::object"))
            {
                service_impl.push_back(srv.first);
                break;
            }
        }
    }

    return service_impl;
}

//-----------------------------------------------------------------------------

std::string factory::get_default_implementation_id_from_object_and_type(
    const std::string& _object,
    const std::string& _type
) const
{
    const std::string object = core::runtime::filter_id(_object);
    const std::string type   = core::runtime::filter_id(_type);
    SIGHT_ASSERT("This case is not managed ", object != "sight::data::object");

    std::string service_impl;
#ifdef _DEBUG
    bool generic_impl_is_found = false;
#endif
    bool specific_impl_is_found = false;

    core::mt::read_lock lock(m_srv_impl_to_srv_info_mutex);
    for(const auto& srv : m_srv_impl_to_srv_info)
    {
        const service_info& srv_info = srv.second;
        if(srv_info.service_type == type)
        {
            for(const auto& oimpl : srv_info.object_impl)
            {
                if(oimpl == object)
                {
                    SIGHT_ASSERT(
                        "Method has already found a specific ("
                        << service_impl << " != " << srv.first
                        << ") service for the object " << oimpl << ".",
                        !specific_impl_is_found
                    );

                    specific_impl_is_found = true;
                    service_impl           = srv.first;
                    break;
                }

                if(oimpl == "sight::data::object")
                {
                    SIGHT_ASSERT(
                        "Method has already found a generic service for the object ("
                        << oimpl << ").",
                        !generic_impl_is_found
                    );
#ifdef _DEBUG
                    generic_impl_is_found = true;
#endif
                    if(!specific_impl_is_found)
                    {
                        service_impl = srv.first;
                        break;
                    }
                }
            }
        }
    }

    SIGHT_ASSERT("A default implementation is not found for this type of service " << type, !service_impl.empty());

    return service_impl;
}

//-----------------------------------------------------------------------------

const std::vector<std::string>& factory::get_service_objects(const std::string& _srv_impl) const
{
    const std::string srv_impl = core::runtime::filter_id(_srv_impl);
    core::mt::read_lock lock(m_srv_impl_to_srv_info_mutex);
    auto iter = m_srv_impl_to_srv_info.find(srv_impl);
    SIGHT_ASSERT("The service " << srv_impl << " is not found.", iter != m_srv_impl_to_srv_info.end());
    return iter->second.object_impl;
}

//-----------------------------------------------------------------------------

std::string factory::get_service_description(const std::string& _srv_impl) const
{
    const std::string srv_impl = core::runtime::filter_id(_srv_impl);
    core::mt::read_lock lock(m_srv_impl_to_srv_info_mutex);
    auto iter = m_srv_impl_to_srv_info.find(srv_impl);
    SIGHT_ASSERT("The service " << srv_impl << " is not found.", iter != m_srv_impl_to_srv_info.end());
    return iter->second.desc;
}

//-----------------------------------------------------------------------------

std::string factory::get_service_tags(const std::string& _srv_impl) const
{
    const std::string srv_impl = core::runtime::filter_id(_srv_impl);
    core::mt::read_lock lock(m_srv_impl_to_srv_info_mutex);
    auto iter = m_srv_impl_to_srv_info.find(srv_impl);
    SIGHT_ASSERT("The service " << srv_impl << " is not found.", iter != m_srv_impl_to_srv_info.end());
    return iter->second.tags;
}

//-----------------------------------------------------------------------------

bool factory::check_service_validity(const std::string& _object, const std::string& _srv_impl) const
{
    const std::string object   = core::runtime::filter_id(_object);
    const std::string srv_impl = core::runtime::filter_id(_srv_impl);
    bool is_valid              = true;
    core::mt::read_lock lock(m_srv_impl_to_srv_info_mutex);
    auto iter = m_srv_impl_to_srv_info.find(srv_impl);
    is_valid &= (iter != m_srv_impl_to_srv_info.end());
    if(is_valid)
    {
        const service_info& srv_info = iter->second;

        is_valid = false;
        for(const auto& oimpl : srv_info.object_impl)
        {
            if(oimpl == "sight::data::object" || oimpl == object)
            {
                is_valid = true;
                break;
            }
        }
    }

    return is_valid;
}

//-----------------------------------------------------------------------------

bool factory::support(const std::string& _object, const std::string& _srv_type, const std::string& _srv_impl) const
{
    const std::string object   = core::runtime::filter_id(_object);
    const std::string srv_type = core::runtime::filter_id(_srv_type);
    const std::string srv_impl = core::runtime::filter_id(_srv_impl);

    bool is_supported = true;
    core::mt::read_lock lock(m_srv_impl_to_srv_info_mutex);
    auto iter = m_srv_impl_to_srv_info.find(srv_impl);
    is_supported &= (iter != m_srv_impl_to_srv_info.end());
    if(is_supported)
    {
        const service_info& srv_info = iter->second;

        is_supported = false;
        if(srv_info.service_type == srv_type)
        {
            for(const auto& oimpl : srv_info.object_impl)
            {
                if(oimpl == "sight::data::object" || oimpl == object)
                {
                    is_supported = true;
                    break;
                }
            }
        }
    }

    return is_supported;
}

//-----------------------------------------------------------------------------

bool factory::support(const std::string& _object, const std::string& _srv_type)
{
    const std::string object   = core::runtime::filter_id(_object);
    const std::string srv_type = core::runtime::filter_id(_srv_type);

    bool is_supported = false;
    support_map_t::key_type key(object, srv_type);
    core::mt::read_to_write_lock support_map_lock(m_support_map_mutex);
    support_map_t::const_iterator iter = m_support_map.find(key);
    if(iter != m_support_map.end())
    {
        is_supported = iter->second;
    }
    else
    {
        core::mt::read_lock lock(m_srv_impl_to_srv_info_mutex);
        for(const auto& srv : m_srv_impl_to_srv_info)
        {
            const service_info& srv_info = srv.second;

            if(srv_info.service_type == srv_type)
            {
                for(const auto& oimpl : srv_info.object_impl)
                {
                    if(oimpl == "sight::data::object" || oimpl == object)
                    {
                        is_supported = true;
                        break;
                    }
                }
            }
        }

        core::mt::upgrade_to_write_lock upgrade(support_map_lock);
        m_support_map.insert(support_map_t::value_type(key, is_supported));
    }

    return is_supported;
}

//-----------------------------------------------------------------------------

factory::key_vector_t factory::get_factory_keys() const
{
    core::mt::read_lock lock(m_srv_impl_to_srv_info_mutex);
    key_vector_t vect_keys;
    std::ranges::transform(
        m_srv_impl_to_srv_info,
        std::back_inserter(vect_keys),
        [](const auto& _e){return _e.first;});
    return vect_keys;
}

//-----------------------------------------------------------------------------

} // namespace sight::service::extension