1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef META_ENGINE_INTERFACE_ENGINE_VALUE_H 17 #define META_ENGINE_INTERFACE_ENGINE_VALUE_H 18 19 #include <core/ecs/entity.h> 20 #include <core/ecs/intf_component_manager.h> 21 #include <core/property/intf_property_handle.h> 22 23 #include <meta/interface/intf_value.h> 24 25 META_BEGIN_NAMESPACE() 26 27 enum class EngineSyncDirection { 28 AUTO, /// Synchronise cached value to engine if changed, otherwise sync engine to cached value 29 TO_ENGINE, /// Synchronise cached value to engine 30 FROM_ENGINE /// Synchronise from engine to cached value 31 }; 32 33 META_REGISTER_INTERFACE(IEngineValue, "2edaea8a-936d-4bc5-a5eb-ecfdbb50574d") 34 35 class IEngineValue : public IValue { 36 META_INTERFACE(IValue, IEngineValue) 37 public: 38 virtual BASE_NS::string GetName() const = 0; 39 virtual AnyReturnValue Sync(EngineSyncDirection) = 0; 40 }; 41 42 43 struct EnginePropertyHandle { 44 CORE_NS::IComponentManager* manager {}; 45 CORE_NS::Entity entity; 46 // in case the property path contains IPropertyHandle*, we need to keep pointer to the most 47 // recent value having such handle, so that we are able to update the handle properly 48 IValue::Ptr parentValue; 49 HandleEnginePropertyHandle50 CORE_NS::IPropertyHandle* Handle() const 51 { 52 if (parentValue) { 53 return GetValue<CORE_NS::IPropertyHandle*>(parentValue->GetValue()); 54 } 55 return manager ? manager->GetData(entity) : nullptr; 56 } 57 58 explicit operator bool() const 59 { 60 return (manager && CORE_NS::EntityUtil::IsValid(entity)) || parentValue; 61 } 62 }; 63 64 struct EnginePropertyParams { 65 EnginePropertyHandle handle; 66 CORE_NS::Property property {}; 67 // offset to the parent property data, this is needed for member properties 68 // calculate baseOffset + property.offset to access the data, 69 // baseOffset is zero if not a member property 70 uintptr_t baseOffset {}; 71 OffsetEnginePropertyParams72 uintptr_t Offset() const 73 { 74 return baseOffset + property.offset; 75 } 76 77 // to support some old code, we allow to push the value directly to the engine property 78 // if this is set, the values can only be set in the engine task queue thread 79 bool pushValueToEngineDirectly {}; 80 }; 81 82 META_REGISTER_INTERFACE(IEngineInternalValueAccess, "f11f1272-e936-4804-9202-3b93606c25ea") 83 class IEngineInternalValueAccess : public CORE_NS::IInterface { 84 META_INTERFACE(CORE_NS::IInterface, IEngineInternalValueAccess) 85 public: 86 virtual IAny::Ptr CreateAny(const CORE_NS::Property&) const = 0; 87 virtual bool IsCompatible(const CORE_NS::PropertyTypeDecl& type) const = 0; 88 virtual AnyReturnValue SyncToEngine(const IAny& value, const EnginePropertyParams& params) const = 0; 89 virtual AnyReturnValue SyncFromEngine(const EnginePropertyParams& params, IAny& out) const = 0; 90 }; 91 92 class IEngineValueInternal : public CORE_NS::IInterface { 93 META_INTERFACE(CORE_NS::IInterface, IEngineValueInternal, "86afd68c-7924-46b8-8bb8-aeace0029945") 94 public: 95 virtual IAny::Ptr CreateAny() const = 0; 96 virtual IEngineInternalValueAccess::ConstPtr GetInternalAccess() const = 0; 97 virtual EnginePropertyParams GetPropertyParams() const = 0; 98 virtual bool SetPropertyParams(const EnginePropertyParams& p) = 0; 99 virtual bool ResetPendingNotify() = 0; 100 }; 101 102 META_INTERFACE_TYPE(META_NS::IEngineValue) 103 META_END_NAMESPACE() 104 105 #endif 106