/*
* 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_INTERFACE_TYPED_PROPERTY_H
#define META_INTERFACE_TYPED_PROPERTY_H
#include
#include
META_BEGIN_NAMESPACE()
template
class Property {
public:
using ValueType = BASE_NS::remove_const_t;
using PropertyInterfaceType = BASE_NS::conditional_t, const IProperty, IProperty>;
using PropertyType = BASE_NS::shared_ptr;
Property() = default;
Property(nullptr_t) {}
template>>
Property(BASE_NS::shared_ptr p) : p_(BASE_NS::move(p))
{
if (p_ && !p_->IsCompatible(UidFromType())) {
CORE_LOG_W("Not compatible any for given property type [%s]", p_->GetName().c_str());
p_ = nullptr;
}
}
template>>
Property(NoCheckT, BASE_NS::shared_ptr p) : p_(p)
{}
template && BASE_NS::is_same_v>>
Property(const Property& p) : Property(p.GetProperty())
{}
bool IsValid() const
{
return p_ != nullptr;
}
explicit operator bool() const
{
return IsValid();
}
TypedPropertyLock operator->() const
{
return GetLockedAccess();
}
TypedPropertyLock GetLockedAccess() const
{
return TypedPropertyLock(p_.get());
}
PropertyInterface GetUnlockedAccess() const
{
return PropertyInterface(p_.get());
}
operator IProperty::ConstPtr() const
{
return p_;
}
operator IProperty::Ptr()
{
return p_;
}
operator IProperty::ConstWeakPtr() const
{
return p_;
}
operator IProperty::WeakPtr()
{
return p_;
}
PropertyType GetProperty() const
{
return p_;
}
private:
PropertyType p_;
};
template
using ConstProperty = Property;
template
inline bool operator==(const Property& l, const Property& r)
{
return l.GetProperty() == r.GetProperty();
}
template
inline bool operator!=(const Property& l, const Property& r)
{
return !(l == r);
}
META_END_NAMESPACE()
// NOLINTBEGIN(readability-identifier-naming) to keep std like syntax
template
BASE_NS::shared_ptr interface_pointer_cast(const META_NS::Property& p)
{
static_assert(META_NS::HasGetInterfaceMethod_v, "T::GetInterface not defined");
return interface_pointer_cast(p.GetProperty());
}
template
T* interface_cast(const META_NS::Property& p)
{
static_assert(META_NS::HasGetInterfaceMethod_v, "T::GetInterface not defined");
return interface_cast(p.GetProperty());
}
// NOLINTEND(readability-identifier-naming) to keep std like syntax
#endif