Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
ServiceFactory.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* ServiceFactory.cc (C) 2000-2019 */
9/* */
10/* Factory for services/modules. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/utils/List.h"
15
16#include "arcane/core/IServiceInfo.h"
18#include "arcane/core/ServiceInstance.h"
19
20#include <iostream>
21
22/*---------------------------------------------------------------------------*/
23/*---------------------------------------------------------------------------*/
24
25namespace Arcane::Internal
26{
27
28/*---------------------------------------------------------------------------*/
29/*---------------------------------------------------------------------------*/
30
31/*!
32 * \file ServiceFactory.h
33 *
34 * \brief This file contains the various service factories
35 * and macros for registering services.
36 *
37 * Most types in this file are internal to Arcane. The only element
38 * useful for a user is the ARCANE_REGISTER_SERVICE() macro, which
39 * allows a service to be registered.
40 */
41
42/*!
43 * \file ServiceProperty.h
44 *
45 * \brief This file contains the various types and classes
46 * for specifying service properties.
47 */
48
49/*!
50 * \defgroup Service Service
51 *
52 * \brief Collection of types used in service management.
53 *
54 * Most user services are subdomain services
55 * and derive indirectly from the BasicService class. Generally, a service is defined in an
56 * AXL file, and the tool \a axl2cc allows generating the base class
57 * of a service from this AXL file. For more
58 * information, refer to section \ref arcanedoc_core_types_service.
59 *
60 * Nevertheless, it is possible to have services without an AXL file. In this case, registering a service so that it is
61 * recognized by Arcane is done via the ARCANE_REGISTER_SERVICE() macro.
62 */
63
64/*---------------------------------------------------------------------------*/
65/*---------------------------------------------------------------------------*/
66
67/*!
68 * \brief Singleton service instances.
69 *
70 * Singleton services can implement multiple interfaces.
71 * There is therefore one IServiceInstance instance per interface plus an instance
72 * for the service itself. Since all these instances reference the
73 * same service, care must be taken not to destroy the service more than once.
74 */
78{
79 public:
80
81 ServiceInstance(IServiceInfo* si)
82 : m_service_info(si)
83 {}
84 ~ServiceInstance()
85 {
86 destroyInstance();
87 }
88
89 public:
90
91 void addReference() override { ++m_nb_ref; }
92 void removeReference() override
93 {
94 Int32 v = std::atomic_fetch_add(&m_nb_ref, -1);
95 if (v == 1)
96 delete this;
97 }
98
99 public:
100
101 ServiceInstanceCollection interfaceInstances() override { return m_instances; }
102 void destroyInstance()
103 {
104 m_true_instance.reset();
105 m_instances.clear();
106 }
107 IServiceInfo* serviceInfo() const override { return m_service_info; }
108 void setTrueInstance(ServiceInstanceRef si) { m_true_instance = si; }
109
110 public:
111
112 void addInstance(ServiceInstanceRef instance) override
113 {
114 m_instances.add(instance);
115 }
116
117 private:
118
119 IServiceInfo* m_service_info;
120 List<ServiceInstanceRef> m_instances;
121 ServiceInstanceRef m_true_instance;
122 std::atomic<Int32> m_nb_ref = 0;
123};
124
125/*---------------------------------------------------------------------------*/
126/*---------------------------------------------------------------------------*/
127
128//! Created a singleton service
131{
132 auto x = new ServiceInstance(m_service_info);
133 IServiceInstanceAdder* sia = x;
134 ServiceInstanceRef si = _createInstance(sbib, sia);
135 x->setTrueInstance(si);
137}
138
139/*---------------------------------------------------------------------------*/
140/*---------------------------------------------------------------------------*/
141
144{
145 ++m_nb_ref;
146}
147
150{
151 // Decrements and returns the previous value.
152 // If it is 1, it means there are no more references
153 // to the object and it must be destroyed.
154 Int32 v = std::atomic_fetch_add(&m_nb_ref, -1);
155 if (v == 1)
156 delete this;
157}
158
159/*---------------------------------------------------------------------------*/
160/*---------------------------------------------------------------------------*/
161
162} // End namespace Arcane::Internal
163
164/*---------------------------------------------------------------------------*/
165/*---------------------------------------------------------------------------*/
This file contains the various service factories and macros for registering services.
Interface for service or module information.
void removeReference() override
Remove a reference.
void addReference() override
Add a reference.
ServiceInstanceCollection interfaceInstances() override
List of instances of interfaces implemented by the singleton.
Ref< ISingletonServiceInstance > createSingletonServiceInstance(const ServiceBuildInfoBase &sbib) override
Creates a singleton service instance.
IServiceInfo * serviceInfo() const override
Returns the IServiceInfo associated with this factory.
Reference to an instance.
Information for creating a service.
Reference to a service instance.
Internal types of Arcane.
auto makeRef(InstanceType *t) -> Ref< InstanceType >
Creates a reference on a pointer.
Collection< ServiceInstanceRef > ServiceInstanceCollection
Collection of service instances.
std::int32_t Int32
Signed integer type of 32 bits.