File: config.cpp

package info (click to toggle)
sight 25.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 42,180 kB
  • sloc: cpp: 289,476; xml: 17,257; ansic: 9,878; python: 1,379; sh: 144; makefile: 33
file content (165 lines) | stat: -rw-r--r-- 5,234 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
/************************************************************************
 *
 * Copyright (C) 2009-2024 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/config.hpp"

#include <core/runtime/runtime.hpp>

namespace sight::service::extension
{

const std::string config::CONFIG_EXT_POINT = "sight::service::extension::config";

config::sptr config::s_current_service_config = std::make_shared<config>();

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

config::sptr config::get_default()
{
    return s_current_service_config;
}

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

void config::parse_plugin_infos()
{
    using extension_t = std::shared_ptr<core::runtime::extension>;

    std::vector<extension_t> ext_elements;
    ext_elements = core::runtime::get_all_extensions_for_point(CONFIG_EXT_POINT);
    for(const extension_t& ext : ext_elements)
    {
        const auto& cfg = ext->get_config();

        const auto config_id = cfg.get<std::string>("id");
        const auto desc      = cfg.get<std::string>("desc", "No description available");
        const auto service   = cfg.get<std::string>("service", "");

        // Get config
        const auto config = cfg.get_child("config");

        // Add service config info
        this->add_service_config_info(config_id, service, desc, config);
    }
}

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

void config::add_service_config_info
(
    const std::string& _config_id,
    const std::string& _service,
    const std::string& _desc,
    const boost::property_tree::ptree& _config
)
{
    core::mt::write_lock lock(m_registry_mutex);

    SIGHT_DEBUG(
        "New service config registering : "
        << " configId = " << _config_id
        << " service = " << _service
        << " desc = " << _desc
    );

    SIGHT_ASSERT(
        "The service config with the id " << _config_id << " already exists.",
        m_reg.find(_config_id) == m_reg.end()
    );

    service_config_info info {
        .service = sight::core::runtime::filter_id(_service),
        .desc    = _desc,
        .config  = _config,
    };
    m_reg[_config_id] = info;
}

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

void config::clear_registry()
{
    core::mt::write_lock lock(m_registry_mutex);
    m_reg.clear();
}

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

boost::property_tree::ptree config::get_service_config(
    const std::string& _config_id,
    const std::string& _service_impl
) const
{
#ifndef _DEBUG
    SIGHT_NOT_USED(_service_impl);
#else
    const std::string service_impl = core::runtime::filter_id(_service_impl);
#endif
    core::mt::read_lock lock(m_registry_mutex);
    auto iter = m_reg.find(_config_id);
    SIGHT_ASSERT(
        "The id " << _config_id << " is not found in the application configuration registry",
        iter != m_reg.end()
    );
    SIGHT_ASSERT(
        "The id " << _config_id << " is not allowed for this service " << service_impl,
        service_impl.empty() || iter->second.service.empty() || iter->second.service == service_impl
    );
    return iter->second.config;
}

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

const std::string& config::get_config_desc(const std::string& _config_id) const
{
    core::mt::read_lock lock(m_registry_mutex);
    auto iter = m_reg.find(_config_id);
    SIGHT_ASSERT(
        "The id " << _config_id << " is not found in the application configuration registry",
        iter != m_reg.end()
    );
    return iter->second.desc;
}

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

std::vector<std::string> config::get_all_config_for_service(std::string _service_impl, bool _matching_only) const
{
    const std::string service_impl = core::runtime::filter_id(_service_impl);
    core::mt::read_lock lock(m_registry_mutex);
    std::vector<std::string> configs;

    for(const auto& srv_cfg : m_reg)
    {
        const service_config_info& info = srv_cfg.second;
        if((info.service.empty() && !_matching_only) || info.service == service_impl)
        {
            configs.push_back(srv_cfg.first);
        }
    }

    return configs;
}

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

} // namespace sight::service::extension