diff options
Diffstat (limited to 'src/components/include')
20 files changed, 307 insertions, 31 deletions
diff --git a/src/components/include/application_manager/application_manager.h b/src/components/include/application_manager/application_manager.h index 6d5c7607cc..057f38f78e 100644 --- a/src/components/include/application_manager/application_manager.h +++ b/src/components/include/application_manager/application_manager.h @@ -47,11 +47,6 @@ #include "application_manager/plugin_manager/rpc_plugin_manager.h" #include "application_manager/state_controller.h" #include "policy/policy_types.h" -#include "telemetry_monitor/telemetry_observable.h" - -namespace resumption { -class LastState; -} namespace app_launch { class AppLaunchCtrl; @@ -136,10 +131,14 @@ class ApplicationManager { public: virtual ~ApplicationManager() {} + DEPRECATED + virtual bool Init(resumption::LastState&, + media_manager::MediaManager* media_manager) = 0; + /** * Inits application manager */ - virtual bool Init(resumption::LastState& last_state, + virtual bool Init(resumption::LastStateWrapperPtr last_state_wrapper, media_manager::MediaManager* media_manager) = 0; /** @@ -286,17 +285,17 @@ class ApplicationManager { virtual void set_current_audio_source(const uint32_t source) = 0; /** - * @brief OnHMILevelChanged the callback that allows SDL to react when + * @brief OnHMIStateChanged the callback that allows SDL to react when * application's HMI level has been changed. * - * @param app_id application identifier for which HMILevel has been chaned. + * @param app_id application identifier for which HMIState has been chaned. * - * @param from previous HMILevel. - * @param to current HMILevel. + * @param from previous HMIState. + * @param to current HMIState. */ - virtual void OnHMILevelChanged(uint32_t app_id, - mobile_apis::HMILevel::eType from, - mobile_apis::HMILevel::eType to) = 0; + virtual void OnHMIStateChanged(const uint32_t app_id, + const HmiStatePtr from, + const HmiStatePtr to) = 0; /** * @brief Updates streaming service status for specified session and notifies @@ -619,12 +618,13 @@ class ApplicationManager { mobile_api::AppInterfaceUnregisteredReason::eType reason) = 0; /** - * @brief Checks HMI level and returns true if streaming is allowed + * @brief Checks application HMI state and returns true if streaming is + * allowed * @param app_id Application id * @param service_type Service type to check * @return True if streaming is allowed, false in other case */ - virtual bool HMILevelAllowsStreaming( + virtual bool HMIStateAllowsStreaming( uint32_t app_id, protocol_handler::ServiceType service_type) const = 0; /** diff --git a/src/components/include/application_manager/hmi_capabilities.h b/src/components/include/application_manager/hmi_capabilities.h index 535ab0409d..e10295b56f 100644 --- a/src/components/include/application_manager/hmi_capabilities.h +++ b/src/components/include/application_manager/hmi_capabilities.h @@ -37,13 +37,10 @@ #include "interfaces/HMI_API.h" #include "interfaces/MOBILE_API.h" #include "json/json.h" +#include "resumption/last_state_wrapper.h" #include "smart_objects/smart_object.h" #include "utils/macro.h" -namespace resumption { -class LastState; -} - namespace application_manager { class ApplicationManager; @@ -531,8 +528,11 @@ class HMICapabilities { virtual const smart_objects::SmartObject* seat_location_capability() const = 0; + DEPRECATED virtual void Init(resumption::LastState* last_state) = 0; + virtual void Init(resumption::LastStateWrapperPtr last_state_wrapper) = 0; + /** * @brief Trigger waiting for response * @param request Request object diff --git a/src/components/include/application_manager/policies/policy_handler_interface.h b/src/components/include/application_manager/policies/policy_handler_interface.h index 190a1f786b..3af3b770aa 100644 --- a/src/components/include/application_manager/policies/policy_handler_interface.h +++ b/src/components/include/application_manager/policies/policy_handler_interface.h @@ -661,6 +661,12 @@ class PolicyHandlerInterface : public VehicleDataItemProvider { virtual bool GetModuleTypes(const std::string& policy_app_id, std::vector<std::string>* modules) const = 0; + /** + * @brief Notifies policy manager about OnSystemRequest + * notification receiving + */ + virtual void OnSystemRequestReceived() const = 0; + private: /** * @brief Processes data received via OnAppPermissionChanged notification diff --git a/src/components/include/policy/policy_external/policy/policy_manager.h b/src/components/include/policy/policy_external/policy/policy_manager.h index 248bbfe16c..22721841aa 100644 --- a/src/components/include/policy/policy_external/policy/policy_manager.h +++ b/src/components/include/policy/policy_external/policy/policy_manager.h @@ -830,6 +830,11 @@ class PolicyManager : public usage_statistics::StatisticsManager, */ virtual ExternalConsentStatus GetExternalConsentStatus() = 0; + /** + * @brief Restart PTU timeout if PTU in UPDATING state + */ + virtual void ResetTimeout() = 0; + protected: /** * @brief Checks is PT exceeded IgnitionCycles diff --git a/src/components/include/policy/policy_regular/policy/policy_manager.h b/src/components/include/policy/policy_regular/policy/policy_manager.h index ee1f5fa98f..c9143d6bbb 100644 --- a/src/components/include/policy/policy_regular/policy/policy_manager.h +++ b/src/components/include/policy/policy_regular/policy/policy_manager.h @@ -785,6 +785,11 @@ class PolicyManager : public usage_statistics::StatisticsManager, virtual AppIdURL RetrySequenceUrl(const struct RetrySequenceURL& rs, const EndpointUrls& urls) const = 0; + /** + * @brief Restart PTU timeout if PTU in UPDATING state + */ + virtual void ResetTimeout() = 0; + protected: /** * @brief Checks is PT exceeded IgnitionCycles diff --git a/src/components/include/resumption/last_state.h b/src/components/include/resumption/last_state.h new file mode 100644 index 0000000000..b28e5611b7 --- /dev/null +++ b/src/components/include/resumption/last_state.h @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2019, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef SRC_COMPONENTS_INCLUDE_RESUMPTION_LAST_STATE_H_ +#define SRC_COMPONENTS_INCLUDE_RESUMPTION_LAST_STATE_H_ + +#include "json/json.h" +#include "utils/macro.h" + +namespace resumption { + +class LastState { + public: + /** + * @brief Destructor + */ + virtual ~LastState() {} + + /** + * @brief Saves dictionary to filesystem + */ + DEPRECATED + virtual void SaveStateToFileSystem() = 0; + + /** + * @brief SaveToFileSystem + * Saving dictionary to filesystem + */ + virtual void SaveToFileSystem() = 0; + + /** + * @brief RemoveFromFileSystem + * Remove dictionary from filesystem + */ + virtual void RemoveFromFileSystem() = 0; + + /** + * @brief dictionary Gets internal dictionary + * @return Reference to internal dictionary json value + */ + virtual Json::Value& get_dictionary() = 0; + + /** + * @brief dictionary Gets internal dictionary + * @return Copy of internal dictionary json value + */ + virtual Json::Value dictionary() const = 0; + + /** + * @brief set_dictionary sets internal dictionary + * @param dictionary New dictionary json value to be set + */ + virtual void set_dictionary(const Json::Value& dictionary) = 0; +}; + +} // namespace resumption + +#endif // SRC_COMPONENTS_INCLUDE_RESUMPTION_LAST_STATE_H_ diff --git a/src/components/include/resumption/last_state_wrapper.h b/src/components/include/resumption/last_state_wrapper.h new file mode 100644 index 0000000000..d5af2e958e --- /dev/null +++ b/src/components/include/resumption/last_state_wrapper.h @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2019, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef SRC_COMPONENTS_INCLUDE_RESUMPTION_LAST_STATE_WRAPPER_H_ +#define SRC_COMPONENTS_INCLUDE_RESUMPTION_LAST_STATE_WRAPPER_H_ + +#include <memory> + +#include "resumption/last_state.h" +#include "utils/mutable_data_accessor.h" + +namespace resumption { +class LastStateWrapper; +typedef std::shared_ptr<LastStateWrapper> LastStateWrapperPtr; +typedef MutableDataAccessor<LastState> LastStateAccessor; + +class LastStateWrapper { + public: + /** + * @brief Getter for providing exclusive access to LastState instance + * @return accessor with ability to access to LastState instance + */ + virtual LastStateAccessor get_accessor() const = 0; +}; + +} // namespace resumption + +#endif // SRC_COMPONENTS_INCLUDE_RESUMPTION_LAST_STATE_WRAPPER_H_ diff --git a/src/components/include/test/application_manager/mock_application_manager.h b/src/components/include/test/application_manager/mock_application_manager.h index 92677b12bb..b2de63252e 100644 --- a/src/components/include/test/application_manager/mock_application_manager.h +++ b/src/components/include/test/application_manager/mock_application_manager.h @@ -55,7 +55,7 @@ #include "application_manager/state_controller.h" #include "interfaces/HMI_API.h" #include "interfaces/MOBILE_API.h" -#include "resumption/last_state.h" +#include "resumption/last_state_wrapper.h" #include "smart_objects/smart_object.h" namespace test { @@ -66,6 +66,10 @@ using application_manager::plugin_manager::RPCPluginManager; class MockApplicationManager : public application_manager::ApplicationManager { public: MOCK_METHOD2(Init, + bool(resumption::LastStateWrapperPtr last_state, + media_manager::MediaManager* media_manager)); + DEPRECATED + MOCK_METHOD2(Init, bool(resumption::LastState& last_state, media_manager::MediaManager* media_manager)); MOCK_METHOD0(Stop, bool()); @@ -139,10 +143,10 @@ class MockApplicationManager : public application_manager::ApplicationManager { MOCK_METHOD1(application_id, uint32_t(const int32_t correlation_id)); MOCK_METHOD2(set_application_id, void(const int32_t correlation_id, const uint32_t app_id)); - MOCK_METHOD3(OnHMILevelChanged, - void(uint32_t app_id, - mobile_apis::HMILevel::eType from, - mobile_apis::HMILevel::eType to)); + MOCK_METHOD3(OnHMIStateChanged, + void(const uint32_t app_id, + const application_manager::HmiStatePtr from, + const application_manager::HmiStatePtr to)); MOCK_METHOD3(ProcessOnDataStreamingNotification, void(const protocol_handler::ServiceType service_type, const uint32_t app_id, @@ -260,7 +264,7 @@ class MockApplicationManager : public application_manager::ApplicationManager { void(mobile_apis::AppInterfaceUnregisteredReason::eType reason)); MOCK_METHOD1(HeadUnitReset, void(mobile_apis::AppInterfaceUnregisteredReason::eType reason)); - MOCK_CONST_METHOD2(HMILevelAllowsStreaming, + MOCK_CONST_METHOD2(HMIStateAllowsStreaming, bool(uint32_t app_id, protocol_handler::ServiceType service_type)); MOCK_METHOD5(CheckPolicyPermissions, diff --git a/src/components/include/test/application_manager/mock_rpc_handler.h b/src/components/include/test/application_manager/mock_rpc_handler.h index fb577622ce..cc7fe988a0 100644 --- a/src/components/include/test/application_manager/mock_rpc_handler.h +++ b/src/components/include/test/application_manager/mock_rpc_handler.h @@ -30,8 +30,10 @@ class MockRPCHandler : public application_manager::rpc_handler::RPCHandler { MOCK_METHOD1(OnErrorSending, void(std::shared_ptr<application_manager::Message> message)); +#ifdef TELEMETRY_MONITOR MOCK_METHOD1(SetTelemetryObserver, void(application_manager::AMTelemetryObserver* observer)); +#endif // TELEMETRY_MONITOR }; } // namespace application_manager_test diff --git a/src/components/include/test/application_manager/mock_rpc_plugin.h b/src/components/include/test/application_manager/mock_rpc_plugin.h index 7e4ff11bc7..2633825428 100644 --- a/src/components/include/test/application_manager/mock_rpc_plugin.h +++ b/src/components/include/test/application_manager/mock_rpc_plugin.h @@ -15,6 +15,13 @@ class MockRPCPlugin : public RPCPlugin { rpc_service::RPCService& rpc_service, HMICapabilities& hmi_capabilities, policy::PolicyHandlerInterface& policy_handler, + resumption::LastStateWrapperPtr last_state)); + DEPRECATED + MOCK_METHOD5(Init, + bool(ApplicationManager& app_manager, + rpc_service::RPCService& rpc_service, + HMICapabilities& hmi_capabilities, + policy::PolicyHandlerInterface& policy_handler, resumption::LastState& last_state)); MOCK_METHOD2(IsAbleToProcess, bool(const int32_t function_id, diff --git a/src/components/include/test/application_manager/policies/mock_policy_handler_interface.h b/src/components/include/test/application_manager/policies/mock_policy_handler_interface.h index f74526148a..f6e0b3cb0f 100644 --- a/src/components/include/test/application_manager/policies/mock_policy_handler_interface.h +++ b/src/components/include/test/application_manager/policies/mock_policy_handler_interface.h @@ -327,6 +327,8 @@ class MockPolicyHandlerInterface : public policy::PolicyHandlerInterface { void(const std::string& device_id_from, const std::string& device_id_to)); + MOCK_CONST_METHOD0(OnSystemRequestReceived, void()); + private: #ifdef EXTERNAL_PROPRIETARY_MODE MOCK_METHOD3(OnAppPermissionConsentInternal, diff --git a/src/components/include/test/policy/policy_external/policy/mock_policy_manager.h b/src/components/include/test/policy/policy_external/policy/mock_policy_manager.h index 8e6cc2666d..d2023fd3d4 100644 --- a/src/components/include/test/policy/policy_external/policy/mock_policy_manager.h +++ b/src/components/include/test/policy/policy_external/policy/mock_policy_manager.h @@ -304,6 +304,7 @@ class MockPolicyManager : public PolicyManager { MOCK_CONST_METHOD0(IsAllowedRetryCountExceeded, bool()); MOCK_METHOD0(OnSystemRequestReceived, void()); MOCK_METHOD0(RetrySequenceFailed, void()); + MOCK_METHOD0(ResetTimeout, void()); }; } // namespace policy_manager_test } // namespace components diff --git a/src/components/include/test/policy/policy_regular/policy/mock_policy_manager.h b/src/components/include/test/policy/policy_regular/policy/mock_policy_manager.h index 927f5ba5cc..da9edb9fe5 100644 --- a/src/components/include/test/policy/policy_regular/policy/mock_policy_manager.h +++ b/src/components/include/test/policy/policy_regular/policy/mock_policy_manager.h @@ -306,6 +306,7 @@ class MockPolicyManager : public PolicyManager { RequestType::State(const std::string& policy_app_id)); MOCK_CONST_METHOD1(GetAppRequestSubTypesState, RequestSubType::State(const std::string& policy_app_id)); + MOCK_METHOD0(ResetTimeout, void()); }; } // namespace policy_manager_test diff --git a/src/components/include/test/resumption/mock_last_state.h b/src/components/include/test/resumption/mock_last_state.h index 9f4b11cbbf..99c3799a95 100644 --- a/src/components/include/test/resumption/mock_last_state.h +++ b/src/components/include/test/resumption/mock_last_state.h @@ -35,6 +35,7 @@ #include "gmock/gmock.h" #include "resumption/last_state.h" +#include "utils/macro.h" namespace test { namespace components { @@ -42,8 +43,14 @@ namespace resumption_test { class MockLastState : public resumption::LastState { public: + DEPRECATED MOCK_METHOD0(SaveStateToFileSystem, void()); + DEPRECATED MOCK_METHOD0(get_dictionary, Json::Value&()); + MOCK_METHOD0(SaveToFileSystem, void()); + MOCK_METHOD0(RemoveFromFileSystem, void()); + MOCK_CONST_METHOD0(dictionary, Json::Value()); + MOCK_METHOD1(set_dictionary, void(const Json::Value&)); }; } // namespace resumption_test diff --git a/src/components/include/test/transport_manager/mock_transport_manager.h b/src/components/include/test/transport_manager/mock_transport_manager.h index 884bde50bf..c853ceb512 100644 --- a/src/components/include/test/transport_manager/mock_transport_manager.h +++ b/src/components/include/test/transport_manager/mock_transport_manager.h @@ -35,7 +35,7 @@ #include <string> #include "gmock/gmock.h" -#include "resumption/last_state.h" +#include "resumption/last_state_wrapper.h" #include "telemetry_monitor/telemetry_observable.h" #include "transport_manager/transport_adapter/transport_adapter_event.h" #include "transport_manager/transport_manager.h" @@ -56,6 +56,8 @@ class MockTransportManager : public ::transport_manager::TransportManager, public ::telemetry_monitor::TelemetryObservable< transport_manager::TMTelemetryObserver> { public: + MOCK_METHOD1(Init, int(resumption::LastStateWrapperPtr last_state)); + DEPRECATED MOCK_METHOD1(Init, int(resumption::LastState& last_state)); MOCK_METHOD0(Reinit, int()); MOCK_METHOD0(Deinit, void()); diff --git a/src/components/include/test/transport_manager/transport_adapter/mock_transport_adapter.h b/src/components/include/test/transport_manager/transport_adapter/mock_transport_adapter.h index 0c0e2dd5ec..a07365f8c0 100644 --- a/src/components/include/test/transport_manager/transport_adapter/mock_transport_adapter.h +++ b/src/components/include/test/transport_manager/transport_adapter/mock_transport_adapter.h @@ -111,6 +111,9 @@ class MockTransportAdapter #ifdef TELEMETRY_MONITOR MOCK_METHOD0(GetTelemetryObserver, ::transport_manager::TMTelemetryObserver*()); + MOCK_METHOD1(SetTelemetryObserver, + void(::transport_manager::TMTelemetryObserver* observer)); + #endif // TELEMETRY_MONITOR }; diff --git a/src/components/include/transport_manager/transport_adapter/transport_adapter.h b/src/components/include/transport_manager/transport_adapter/transport_adapter.h index b92e010ece..8b46be253b 100644 --- a/src/components/include/transport_manager/transport_adapter/transport_adapter.h +++ b/src/components/include/transport_manager/transport_adapter/transport_adapter.h @@ -357,6 +357,14 @@ class TransportAdapter { * @param return pointer to Time metric observer */ virtual TMTelemetryObserver* GetTelemetryObserver() = 0; + + /** + * @brief Setup observer for time metric. + * + * @param observer - pointer to observer + */ + virtual void SetTelemetryObserver(TMTelemetryObserver* observer) = 0; + #endif // TELEMETRY_MONITOR }; } // namespace transport_adapter diff --git a/src/components/include/transport_manager/transport_manager.h b/src/components/include/transport_manager/transport_manager.h index ce92bfd9cb..e370e3e4c6 100644 --- a/src/components/include/transport_manager/transport_manager.h +++ b/src/components/include/transport_manager/transport_manager.h @@ -34,14 +34,11 @@ #define SRC_COMPONENTS_INCLUDE_TRANSPORT_MANAGER_TRANSPORT_MANAGER_H_ #include "protocol/common.h" +#include "resumption/last_state_wrapper.h" #include "transport_manager/common.h" #include "transport_manager/transport_adapter/transport_adapter_event.h" #include "transport_manager/transport_manager_listener.h" -namespace resumption { -class LastState; -} - namespace transport_manager { class TransportAdapterEvent; @@ -56,11 +53,14 @@ class TransportManager { **/ virtual ~TransportManager() {} + DEPRECATED + virtual int Init(resumption::LastState& last_state) = 0; + /** * @brief Initialize transport manager. * @return Error code. */ - virtual int Init(resumption::LastState& last_state) = 0; + virtual int Init(resumption::LastStateWrapperPtr last_state_wrapper) = 0; /** * @brief Reinitializes transport manager diff --git a/src/components/include/utils/data_accessor.h b/src/components/include/utils/data_accessor.h index 1be7c3ab53..99820237c8 100644 --- a/src/components/include/utils/data_accessor.h +++ b/src/components/include/utils/data_accessor.h @@ -56,6 +56,7 @@ class DataAccessor { --(*counter_); } } + const T& GetData() const { return data_; } diff --git a/src/components/include/utils/mutable_data_accessor.h b/src/components/include/utils/mutable_data_accessor.h new file mode 100644 index 0000000000..0e03345a45 --- /dev/null +++ b/src/components/include/utils/mutable_data_accessor.h @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2020, Ford Motor Company + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following + * disclaimer in the documentation and/or other materials provided with the + * distribution. + * + * Neither the name of the Ford Motor Company nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef SRC_COMPONENTS_INCLUDE_UTILS_MUTABLE_DATA_ACCESSOR_H +#define SRC_COMPONENTS_INCLUDE_UTILS_MUTABLE_DATA_ACCESSOR_H + +#include "utils/lock.h" + +// This class is for thread-safe mutable access to data + +template <class T> +class MutableDataAccessor { + public: + MutableDataAccessor(T& data, + const std::shared_ptr<sync_primitives::BaseLock>& lock) + : data_(data), lock_(lock), counter_(new uint32_t(0)) { + lock_->Acquire(); + } + + MutableDataAccessor(const MutableDataAccessor<T>& other) + : data_(other.data_), lock_(other.lock_), counter_(other.counter_) { + ++(*counter_); + } + + ~MutableDataAccessor() { + if (0 == *counter_) { + lock_->Release(); + } else { + --(*counter_); + } + } + + const T& GetData() const { + return data_; + } + + T& GetMutableData() { + return data_; + } + + private: + void* operator new(size_t size); + T& data_; + // Require that the lock lives at least as long as the DataAccessor + const std::shared_ptr<sync_primitives::BaseLock> lock_; + std::shared_ptr<uint32_t> counter_; +}; + +#endif // SRC_COMPONENTS_INCLUDE_UTILS_MUTABLE_DATA_ACCESSOR_H |