• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     /// Check if valid
48     bool IsValid() const
49     {
50         return p_ != nullptr;
51     }
52 
53     /// returns true if valid
54     explicit operator bool() const
55     {
56         return IsValid();
57     }
58 
59     /// Locked access to the property functionality
60     TypedArrayPropertyLock<Type> operator->() const
61     {
62         return GetLockedAccess();
63     }
64     /// Get locked access to the property functionality
65     TypedArrayPropertyLock<Type> GetLockedAccess() const
66     {
67         return TypedArrayPropertyLock<Type>(NOCHECK, p_.get());
68     }
69     /// Get unlocked access to the property functionality
70     ArrayPropertyInterface<Type> GetUnlockedAccess() const
71     {
72         return ArrayPropertyInterface<Type>(p_.get());
73     }
74     /// Convert to const pointer of IProperty
75     operator IProperty::ConstPtr() const
76     {
77         return p_;
78     }
79     /// Convert to pointer of IProperty
80     operator IProperty::Ptr()
81     {
82         return p_;
83     }
84     /// Convert to const weak pointer of IProperty
85     operator IProperty::ConstWeakPtr() const
86     {
87         return p_;
88     }
89     /// Convert to weak pointer of IProperty
90     operator IProperty::WeakPtr()
91     {
92         return p_;
93     }
94     /// Get underlying property
95     PropertyType GetProperty() const
96     {
97         return p_;
98     }
99 
100 private:
101     PropertyType p_;
102 };
103 
104 template<typename T>
105 using ConstArrayProperty = ArrayProperty<const T>;
106 
107 template<typename T>
108 inline bool operator==(const ArrayProperty<T>& l, const ArrayProperty<T>& r)
109 {
110     return l.GetProperty() == r.GetProperty();
111 }
112 
113 template<typename T>
114 inline bool operator!=(const ArrayProperty<T>& l, const ArrayProperty<T>& r)
115 {
116     return !(l == r);
117 }
118 
119 META_END_NAMESPACE()
120 
121 #endif
122