environment_factory.h (4774B)
1 /* 2 * Copyright 2023 The WebRTC Project Authors. All rights reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #ifndef API_ENVIRONMENT_ENVIRONMENT_FACTORY_H_ 12 #define API_ENVIRONMENT_ENVIRONMENT_FACTORY_H_ 13 14 #include <memory> 15 #include <utility> 16 17 #include "absl/base/nullability.h" 18 #include "api/environment/environment.h" 19 #include "api/field_trials_view.h" 20 #include "api/ref_counted_base.h" 21 #include "api/rtc_event_log/rtc_event_log.h" 22 #include "api/scoped_refptr.h" 23 #include "api/task_queue/task_queue_factory.h" 24 #include "rtc_base/system/rtc_export.h" 25 #include "system_wrappers/include/clock.h" 26 27 namespace webrtc { 28 // Constructs `Environment`. 29 // Individual utilities are provided using one of the `Set` functions. 30 // `Set` functions do nothing when nullptr value is passed. 31 // Creates default implementations for utilities that are not provided. 32 // 33 // Examples: 34 // Environment default_env = EnvironmentFactory().Create(); 35 // 36 // EnvironmentFactory factory; 37 // factory.Set(std::make_unique<CustomTaskQueueFactory>()); 38 // factory.Set(std::make_unique<CustomFieldTrials>()); 39 // Environment custom_env = factory.Create(); 40 // 41 class RTC_EXPORT EnvironmentFactory final { 42 public: 43 EnvironmentFactory() = default; 44 explicit EnvironmentFactory(const Environment& env); 45 46 EnvironmentFactory(const EnvironmentFactory&) = default; 47 EnvironmentFactory(EnvironmentFactory&&) = default; 48 EnvironmentFactory& operator=(const EnvironmentFactory&) = default; 49 EnvironmentFactory& operator=(EnvironmentFactory&&) = default; 50 51 ~EnvironmentFactory() = default; 52 53 void Set(absl_nullable std::unique_ptr<const FieldTrialsView> utility); 54 void Set(absl_nullable std::unique_ptr<Clock> utility); 55 void Set(absl_nullable std::unique_ptr<TaskQueueFactory> utility); 56 void Set(absl_nullable std::unique_ptr<RtcEventLog> utility); 57 58 void Set(const FieldTrialsView* absl_nullable utility); 59 void Set(Clock* absl_nullable utility); 60 void Set(TaskQueueFactory* absl_nullable utility); 61 void Set(RtcEventLog* absl_nullable utility); 62 63 Environment Create() const; 64 65 private: 66 Environment CreateWithDefaults() &&; 67 68 scoped_refptr<const RefCountedBase> leaf_; 69 70 const FieldTrialsView* absl_nullable field_trials_ = nullptr; 71 Clock* absl_nullable clock_ = nullptr; 72 TaskQueueFactory* absl_nullable task_queue_factory_ = nullptr; 73 RtcEventLog* absl_nullable event_log_ = nullptr; 74 }; 75 76 // Helper for concise way to create an environment. 77 // `Environment env = CreateEnvironment(utility1, utility2)` is a shortcut to 78 // `EnvironmentFactory factory; 79 // factory.Set(utility1); 80 // factory.Set(utility2); 81 // Environment env = factory.Create();` 82 // 83 // Examples: 84 // Environment default_env = CreateEnvironment(); 85 // Environment custom_env = 86 // CreateEnvironment(std::make_unique<CustomTaskQueueFactory>(), 87 // std::make_unique<CustomFieldTrials>()); 88 template <typename... Utilities> 89 Environment CreateEnvironment(Utilities&&... utilities); 90 91 //------------------------------------------------------------------------------ 92 // Implementation details follow 93 //------------------------------------------------------------------------------ 94 95 inline void EnvironmentFactory::Set( 96 const FieldTrialsView* absl_nullable utility) { 97 if (utility != nullptr) { 98 field_trials_ = utility; 99 } 100 } 101 102 inline void EnvironmentFactory::Set(Clock* absl_nullable utility) { 103 if (utility != nullptr) { 104 clock_ = utility; 105 } 106 } 107 108 inline void EnvironmentFactory::Set(TaskQueueFactory* absl_nullable utility) { 109 if (utility != nullptr) { 110 task_queue_factory_ = utility; 111 } 112 } 113 114 inline void EnvironmentFactory::Set(RtcEventLog* absl_nullable utility) { 115 if (utility != nullptr) { 116 event_log_ = utility; 117 } 118 } 119 120 namespace webrtc_create_environment_internal { 121 122 inline void Set(EnvironmentFactory& /* factory */) {} 123 124 template <typename FirstUtility, typename... Utilities> 125 void Set(EnvironmentFactory& factory, 126 FirstUtility&& first, 127 Utilities&&... utilities) { 128 factory.Set(std::forward<FirstUtility>(first)); 129 Set(factory, std::forward<Utilities>(utilities)...); 130 } 131 132 } // namespace webrtc_create_environment_internal 133 134 template <typename... Utilities> 135 Environment CreateEnvironment(Utilities&&... utilities) { 136 EnvironmentFactory factory; 137 webrtc_create_environment_internal::Set( 138 factory, std::forward<Utilities>(utilities)...); 139 return factory.Create(); 140 } 141 142 } // namespace webrtc 143 144 #endif // API_ENVIRONMENT_ENVIRONMENT_FACTORY_H_