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_INTERFACE_TYPED_ARRAY_PROPERTY_H
17 #define META_INTERFACE_TYPED_ARRAY_PROPERTY_H
18 
19 #include <meta/base/types.h>
20 #include <meta/interface/detail/array_property.h>
21 
META_BEGIN_NAMESPACE()22 META_BEGIN_NAMESPACE()
23 
24 template<typename Type>
25 class ArrayProperty final {
26 public:
27     using ValueType = BASE_NS::remove_const_t<Type>;
28     using PropertyInterfaceType = BASE_NS::conditional_t<BASE_NS::is_const_v<Type>, const IProperty, IProperty>;
29     using PropertyType = BASE_NS::shared_ptr<PropertyInterfaceType>;
30     using IndexType = typename ArrayPropertyInterface<Type>::IndexType;
31 
32     ArrayProperty() = default;
33     ArrayProperty(nullptr_t) {}
34 
35     template<typename Prop, typename = BASE_NS::enable_if_t<BASE_NS::is_convertible_v<Prop*, PropertyInterfaceType*>>>
36     ArrayProperty(BASE_NS::shared_ptr<Prop> p) : p_(BASE_NS::move(p))
37     {
38         if (p_ && !p_->IsCompatible(ArrayUidFromType<ValueType>())) {
39             CORE_LOG_W("Not compatible any for given array property type [%s]", p_->GetName().c_str());
40             p_ = nullptr;
41         }
42     }
43     template<typename Prop, typename = BASE_NS::enable_if_t<BASE_NS::is_convertible_v<Prop*, PropertyInterfaceType*>>>
44     ArrayProperty(NoCheckT, BASE_NS::shared_ptr<Prop> p) : p_(p)
45     {}
46 
47     bool IsValid() const
48     {
49         return p_ != nullptr;
50     }
51 
52     explicit operator bool() const
53     {
54         return IsValid();
55     }
56 
57     TypedArrayPropertyLock<Type> operator->() const
58     {
59         return GetLockedAccess();
60     }
61 
62     TypedArrayPropertyLock<Type> GetLockedAccess() const
63     {
64         return TypedArrayPropertyLock<Type>(p_.get());
65     }
66 
67     ArrayPropertyInterface<Type> GetUnlockedAccess() const
68     {
69         return ArrayPropertyInterface<Type>(p_.get());
70     }
71 
72     operator IProperty::ConstPtr() const
73     {
74         return p_;
75     }
76 
77     operator IProperty::Ptr()
78     {
79         return p_;
80     }
81 
82     operator IProperty::ConstWeakPtr() const
83     {
84         return p_;
85     }
86 
87     operator IProperty::WeakPtr()
88     {
89         return p_;
90     }
91 
92     PropertyType GetProperty() const
93     {
94         return p_;
95     }
96 
97 private:
98     PropertyType p_;
99 };
100 
101 template<typename T>
102 using ConstArrayProperty = ArrayProperty<const T>;
103 
104 template<typename T>
105 inline bool operator==(const ArrayProperty<T>& l, const ArrayProperty<T>& r)
106 {
107     return l.GetProperty() == r.GetProperty();
108 }
109 
110 template<typename T>
111 inline bool operator!=(const ArrayProperty<T>& l, const ArrayProperty<T>& r)
112 {
113     return !(l == r);
114 }
115 
116 META_END_NAMESPACE()
117 
118 #endif
119