1 /*
2  * SObjectizer-5
3  */
4 
5 /*!
6  * \file
7  * \brief A set of helpers for making actual dispatcher instances.
8  *
9  * \since
10  * v.5.6.0
11  */
12 
13 #pragma once
14 
15 #include <so_5/stats/impl/activity_tracking.hpp>
16 
17 #include <so_5/impl/internal_env_iface.hpp>
18 
19 #include <string_view>
20 
21 namespace so_5 {
22 
23 namespace disp {
24 
25 namespace reuse {
26 
27 /*!
28  * \brief Helper functions to adjust some dispatcher parameters with
29  * respect to settings from environment.
30  *
31  * If dispatcher params doesn't have lock-factory specified the
32  * lock-factory will be inherited from environment.
33  *
34  * \tparam Disp_Params_Type type of struct/class with dispatcher-related
35  * parameters. This type should have queue_params() method.
36  *
37  * \since
38  * v.5.6.0
39  */
40 template< typename Disp_Params_Type >
41 void
modify_disp_params(so_5::environment_t & env,Disp_Params_Type & params)42 modify_disp_params(
43 	so_5::environment_t & env,
44 	Disp_Params_Type & params )
45 	{
46 		auto lf = params.queue_params().lock_factory();
47 		if( !lf )
48 		{
49 			auto queue_params = params.queue_params();
50 			queue_params.lock_factory(
51 					so_5::impl::default_lock_factory( env, lf ) );
52 			params.set_queue_params( std::move(queue_params) );
53 		}
54 	}
55 
56 /*!
57  * \brief Helper function for creation of dispatcher instance with
58  * respect to work thread activity tracking flag.
59  *
60  * \note
61  * Calls modify_disp_params() before creation of dispatcher instance.
62  *
63  * \attention
64  * Dispatcher constructor should have the following format:
65  * \code
66  * Dispatcher(
67  * 	outliving_reference_t<environment_t> env,
68  * 	const std::string_view data_source_base_name,
69  * 	Disp_Params_Type disp_params,
70  * 	Args && ...additional_args );
71  * \endcode
72  *
73  * \tparam Disp_Iface_Type a type of interface for actual dispatcher. This type
74  * is expected to be a base class for Disp_No_Tracking and Disp_With_Tracking
75  * types.
76  *
77  * \tparam Disp_No_Tracking a type of dispatcher for case when thread
78  * activity tracking is not used.
79  *
80  * \tparam Disp_With_Tracking a type of dispatcher for case when thread
81  * activity tracking is used.
82  *
83  * \tparam Disp_Params_Type a type of dispatcher-specific parameters.
84  *
85  * \tparam Args types of additional arguments for dispatcher's constructor.
86  *
87  * \since
88  * v.5.6.0
89  */
90 template<
91 	typename Disp_Iface_Type,
92 	typename Disp_No_Tracking,
93 	typename Disp_With_Tracking,
94 	typename Disp_Params_Type,
95 	typename... Args >
96 std::unique_ptr< Disp_Iface_Type >
make_actual_dispatcher(outliving_reference_t<environment_t> env,const std::string_view name_base,Disp_Params_Type disp_params,Args &&...args)97 make_actual_dispatcher(
98 	outliving_reference_t< environment_t > env,
99 	const std::string_view name_base,
100 	Disp_Params_Type disp_params,
101 	Args && ...args )
102 	{
103 		modify_disp_params( env.get(), disp_params );
104 
105 		using so_5::stats::activity_tracking_stuff::create_appropriate_disp;
106 		return create_appropriate_disp<
107 				Disp_Iface_Type,
108 				Disp_No_Tracking,
109 				Disp_With_Tracking >(
110 			env,
111 			name_base,
112 			std::move(disp_params),
113 			std::forward<Args>(args)... );
114 	}
115 
116 } /* namespace reuse */
117 
118 } /* namespace disp */
119 
120 } /* namespace so_5 */
121 
122