/* * Copyright (c) 2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef META_EXT_ENGINE_INTERNAL_ACCESS_H #define META_EXT_ENGINE_INTERNAL_ACCESS_H #include #include #include #include META_BEGIN_NAMESPACE() template class EngineInternalValueAccessImpl : public IntroduceInterfaces { public: IAny::Ptr CreateAny() const override { return IAny::Ptr(new AnyType); } bool IsCompatible(const CORE_NS::PropertyTypeDecl& type) const override { return MetaType::coreType == type; } AnyReturnValue SyncToEngine(const IAny& value, const EnginePropertyParams& params) const override { CORE_NS::ScopedHandle guard { params.handle }; return guard ? value.GetData(UidFromType(), (void*)((uintptr_t) & *guard + params.Offset()), sizeof(Type)) /*NOLINT(bugprone-sizeof-expression)*/ : AnyReturn::FAIL; } AnyReturnValue SyncFromEngine(const EnginePropertyParams& params, IAny& out) const override { CORE_NS::ScopedHandle guard { params.handle }; return guard ? out.SetData(UidFromType(), (const void*)((uintptr_t) & *guard + params.Offset()), sizeof(Type)) /*NOLINT(bugprone-sizeof-expression)*/ : AnyReturn::FAIL; } }; template class EngineInternalValueAccess : public EngineInternalValueAccessImpl> {}; template class EngineInternalValueAccess> : public EngineInternalValueAccessImpl, ArrayAny> {}; template class EngineInternalArrayValueAccess : public IntroduceInterfaces { public: using InternalType = BASE_NS::vector; IAny::Ptr CreateAny() const override { return IAny::Ptr(new ArrayAny); } bool IsCompatible(const CORE_NS::PropertyTypeDecl& type) const override { return MetaType::coreType == type; } AnyReturnValue SyncToEngine(const IAny& value, const EnginePropertyParams& params) const override { AnyReturnValue res = AnyReturn::FAIL; CORE_NS::ScopedHandle guard { params.handle }; if (guard && params.property.metaData.containerMethods) { BASE_NS::vector vec; res = value.GetData(UidFromType(), &vec, sizeof(InternalType)); if (res) { if (params.property.type.isArray) { size_t size = params.property.count < vec.size() ? params.property.count : vec.size(); for (size_t i = 0; i != size; ++i) { ((Type*)((uintptr_t) & *guard + params.Offset()))[i] = vec[i]; } } else { auto cont = params.property.metaData.containerMethods; cont->resize(params.Offset(), vec.size()); for (size_t i = 0; i != vec.size(); ++i) { *((Type*)cont->get(params.Offset(), i)) = vec[i]; } } } } return res; } AnyReturnValue SyncFromEngine(const EnginePropertyParams& params, IAny& out) const override { AnyReturnValue res = AnyReturn::FAIL; CORE_NS::ScopedHandle guard { params.handle }; if (guard && params.property.metaData.containerMethods) { BASE_NS::vector vec; if (params.property.type.isArray) { vec.resize(params.property.count); for (size_t i = 0; i != vec.size(); ++i) { vec[i] = ((const Type*)((uintptr_t) & *guard + params.Offset()))[i]; } } else { auto cont = params.property.metaData.containerMethods; vec.resize(cont->size(params.Offset())); for (size_t i = 0; i != vec.size(); ++i) { vec[i] = *((const Type*)cont->get(params.Offset(), i)); } } res = out.SetData(UidFromType(), &vec, sizeof(InternalType)); } return res; } }; META_END_NAMESPACE() #endif