File: base.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 (334 lines) | stat: -rw-r--r-- 9,373 bytes parent folder | download
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
/************************************************************************
 *
 * 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/base.hpp"

#include "service/detail/service.hpp"
#include "service/manager.hpp"
#include "service/registry.hpp"

#include <core/com/signal.hxx>
#include <core/com/slot.hxx>
#include <core/com/slots.hxx>
#include <core/runtime/helper.hpp>
#include <core/thread/worker.hpp>

#include <functional>
#include <regex>

namespace sight::service
{

connections_t::connections_t(
    std::initializer_list<std::tuple<const std::string_view, core::com::signals::key_t, core::com::slots::key_t> > _init
)
{
    for(const auto& [key, sig, slot] : _init)
    {
        m_key_connections_map[key].emplace_back(sig, slot);
    }
}

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

void connections_t::push(
    std::string_view _key,
    const core::com::signals::key_t& _sig,
    const core::com::slots::key_t& _slot
)
{
    m_key_connections_map[_key].emplace_back(_sig, _slot);
}

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

bool connections_t::contains(std::string_view _key) const
{
    return m_key_connections_map.contains(_key);
}

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

connections_t::key_connections_map_t::const_iterator connections_t::find(std::string_view _key) const
{
    return m_key_connections_map.find(_key);
}

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

connections_t::key_connections_map_t::const_iterator connections_t::end() const
{
    return m_key_connections_map.cend();
}

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

bool connections_t::empty() const
{
    return m_key_connections_map.empty();
}

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

std::size_t connections_t::size() const
{
    return m_key_connections_map.size();
}

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

connections_t connections_t::operator+(const connections_t& _other) const
{
    connections_t result;
    result.m_key_connections_map.insert(m_key_connections_map.begin(), m_key_connections_map.end());
    result.m_key_connections_map.insert(_other.m_key_connections_map.begin(), _other.m_key_connections_map.end());
    return result;
}

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

base::base() :
    m_pimpl(std::make_unique<detail::service>(*this))
{
    new_signal<signals::started_t>(signals::STARTED);
    new_signal<signals::updated_t>(signals::UPDATED);
    new_signal<signals::swapped_t>(signals::SWAPPED);
    new_signal<signals::stopped_t>(signals::STOPPED);

    new_slot(slots::START, [this](){m_pimpl->start(true);});
    new_slot(slots::STOP, [this](){m_pimpl->stop(true);});
    new_slot(slots::UPDATE, [this](){m_pimpl->update(true);});
    new_slot(
        slots::SWAP_KEY,
        [this](std::string_view _key, data::object::sptr _obj)
        {
            m_pimpl->swap_key(_key, _obj, true);
        });
    new_slot(
        slots::START_ON_PROPERTY,
        [this]()
        {
            if(*m_start_property)
            {
                if(not started())
                {
                    m_pimpl->start(true);
                }
            }
            else
            {
                if(not stopped())
                {
                    m_pimpl->stop(true);
                }
            }
        });
}

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

base::~base() = default;

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

void base::set_worker(core::thread::worker::sptr _worker)
{
    m_pimpl->m_worker = _worker;
    core::com::has_slots::m_slots.set_worker(m_pimpl->m_worker);
}

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

core::thread::worker::sptr base::worker() const
{
    return m_pimpl->m_worker;
}

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

void base::set_config(const config_t& _configuration)
{
    m_pimpl->set_config(_configuration);
}

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

const base::config_t& base::get_config() const
{
    return m_pimpl->get_config();
}

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

void base::configure()
{
    m_pimpl->configure();
}

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

void base::configure(const config_t& _service_config)
{
    this->set_config(_service_config);
    this->configure();
}

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

base::shared_future_t base::start()
{
    if(!m_pimpl->m_worker || core::thread::get_current_thread_id() == m_pimpl->m_worker->get_thread_id())
    {
        return m_pimpl->start(false);
    }

    return slot(slots::START)->async_run();
}

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

base::shared_future_t base::stop()
{
    if(!m_pimpl->m_worker || core::thread::get_current_thread_id() == m_pimpl->m_worker->get_thread_id())
    {
        return m_pimpl->stop(false);
    }

    return slot(slots::STOP)->async_run();
}

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

base::shared_future_t base::update()
{
    if(!m_pimpl->m_worker || core::thread::get_current_thread_id() == m_pimpl->m_worker->get_thread_id())
    {
        return m_pimpl->update(false);
    }

    return slot(slots::UPDATE)->async_run();
}

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

base::shared_future_t base::swap_key(std::string_view _key, data::object::sptr _obj)
{
    if(!m_pimpl->m_worker || core::thread::get_current_thread_id() == m_pimpl->m_worker->get_thread_id())
    {
        return m_pimpl->swap_key(_key, _obj, false);
    }

    return slot(slots::SWAP_KEY)->async_run(_key, _obj);
}

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

base::global_status base::status() const noexcept
{
    return m_pimpl->m_global_state;
}

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

base::configuration_status base::config_status() const noexcept
{
    return m_pimpl->m_configuration_state;
}

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

bool base::started() const noexcept
{
    return m_pimpl->m_global_state == global_status::started;
}

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

bool base::stopped() const noexcept
{
    return m_pimpl->m_global_state == global_status::stopped;
}

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

enum base::updating_status base::updating_status() const noexcept
{
    return m_pimpl->m_updating_state;
}

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

bool base::is_auto_connected() const
{
    return m_pimpl->is_auto_connected();
}

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

connections_t base::auto_connections() const
{
    connections_t connections;
    return connections;
}

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

void base::on_property_set(std::string_view /*unused*/)
{
}

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

void base::info(std::ostream& /*unused*/)
{
}

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

void base::notify_register_out(data::object::sptr _obj, const std::string& _id)
{
    manager::notify_register_out(_obj, _id);
}

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

void base::notify_unregister_out(data::object::sptr _obj, const std::string& _id)
{
    manager::notify_unregister_out(_obj, _id);
}

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

/**
 * @brief Streaming a service
 * @see base::operator<<(std::ostream & _ostream, base& _service)
 * @note Invoke base::info( std::ostream )
 */
std::ostream& operator<<(std::ostream& _ostream, base& _service)
{
    _service.info(_ostream);
    return _ostream;
}

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

} // namespace sight::service