Aspose.Tasks for C++
PropertyKeyedCollection.h
1 #pragma once
2 //-----------------------------------------------------------------------
3 // <copyright file="PropertyKeyedCollection.cs" company="Aspose Pty Ltd">
4 // Copyright (c) 2002-2025 Aspose Pty Ltd. All Rights Reserved.
5 // </copyright>
6 //-----------------------------------------------------------------------
7 
8 #include <system/type_info.h>
9 #include <system/object_ext.h>
10 #include <system/exceptions.h>
11 #include <system/details/pointer_collection_helpers.h>
12 #include <system/constraints.h>
13 #include <system/collections/keyvalue_pair.h>
14 #include <system/collections/ienumerator.h>
15 #include <system/collections/icollection.h>
16 #include <system/collections/dictionary.h>
17 #include <system/array.h>
18 #include <cstdint>
19 
20 #include "aspose.tasks.cpp/PropertyCollections/PropertyCollection.h"
21 #include "aspose.tasks.cpp/PropertyCollections/Property.h"
22 
23 namespace Aspose
24 {
25 namespace Tasks
26 {
27 namespace Properties
28 {
29 class BuiltInProjectPropertyCollection;
30 class CustomProjectPropertyCollection;
31 class CustomProjectPropertyWrapperCollection;
32 } // namespace Properties
33 } // namespace Tasks
34 } // namespace Aspose
35 
36 namespace Aspose {
37 
38 namespace Tasks {
39 
40 namespace Properties {
41 
42 /// <summary>
43 /// A base class of collection of properties.
44 /// </summary>
45 /// <typeparam name="T">The type of property.</typeparam>
46 template<typename T>
47 class PropertyKeyedCollection : public PropertyCollection<T>, public System::Collections::Generic::ICollection<T>
48 {
49  typedef Property BaseT_Property;
50  assert_is_base_of(BaseT_Property, T);
51 
53  typedef PropertyCollection<T> BaseType;
54  typedef System::Collections::Generic::ICollection<T> BaseType1;
55 
56  typedef ::System::BaseTypesInfo<BaseType, BaseType1> ThisTypeBaseTypesInfo;
57  RTTI_INFO_TEMPLATE_CLASS(ThisType, ThisTypeBaseTypesInfo);
58 
59  template<typename FT0> friend class PropertyKeyedCollection;
62  friend class CustomProjectPropertyWrapperCollection;
63 
64 public:
65 
66  /// <summary>
67  /// Gets the collection of all property names.
68  /// </summary>
69  System::SharedPtr<System::Collections::Generic::ICollection<System::String>> get_Names()
70  {
71  return this->dictionary->get_Keys();
72  }
73 
74  /// <summary>
75  /// Gets the number of properties in the collection.
76  /// </summary>
77  int32_t get_Count() const override
78  {
79  return this->dictionary->get_Count();
80  }
81 
82  /// <summary>
83  /// Gets a value indicating whether this collection is read-only; otherwise, false.
84  /// </summary>
85  bool get_IsReadOnly() const override = 0;
86 
87  /// <summary>
88  /// Gets the Property associated with the specified key.
89  /// </summary>
90  /// <param name="name">The name of the Property to get.</param>
91  /// <returns>The Property associated with the specified name.</returns>
92  T idx_get(const System::String& name)
93  {
94  return this->Get(name);
95  }
96 
97  /// <inheritdoc ></inheritdoc>
98  void Clear() override
99  {
100  this->ThrowIfReadOnly();
101  this->ClearInternal();
102  }
103 
104  /// <summary>
105  /// Determines whether the <see cref="Aspose::Tasks::Properties::PropertyCollection{T}"></see> contains a property with the specified name.
106  /// </summary>
107  /// <param name="name">The name of a property</param>
108  /// <returns>true if the <see cref="Aspose::Tasks::Properties::PropertyCollection{T}"></see> contains a property with the specified name; otherwise, false.</returns>
109  bool Contains(const System::String& name) const
110  {
111  return this->dictionary->ContainsKey(name);
112  }
113 
114  /// <summary>
115  /// Creates a new custom property.
116  /// </summary>
117  /// <param name="item">The property to add.</param>
118  void Add(const T& item) override
119  {
120  this->ThrowIfReadOnly();
121  this->AddInternal(item);
122  }
123 
124  void SetTemplateWeakPtr(uint32_t argument) override
125  {
126  switch (argument)
127  {
128  case 0:
129  BaseType::SetTemplateWeakPtr(0);
130  System::Details::CollectionHelpers::SetWeakPointer(1, dictionary);
131  break;
132 
133  }
134  }
135 
136 protected:
137 
138  /// <summary>Initializes a new instance of the <see cref="PropertyKeyedCollection{T}"></see> class.</summary>
139  PropertyKeyedCollection()
140  : dictionary(System::MakeObject<System::Collections::Generic::Dictionary<System::String, T>>())
141  {
142  }
143 
144  /// <summary>
145  /// Gets the Property associated with the specified key.
146  /// </summary>
147  /// <param name="name">The name of the Property to get.</param>
148  /// <param name="value">The Property associated with the specified name.</param>
149  void idx_set(const System::String& name, T value)
150  {
151  this->ThrowIfReadOnly();
152  this->Set(name, value);
153  }
154 
155  System::SharedPtr<System::Collections::Generic::IEnumerator<T>> GetEnumeratorInternal() override
156  {
157  return this->dictionary->get_Values()->GetEnumerator();
158  }
159 
160  bool TryGetValue(const System::String& key, T& value)
161  {
162  return this->dictionary->TryGetValue(key, value);
163  }
164 
165  /// <summary>
166  /// Adds the passed property into collection.
167  /// </summary>
168  /// <param name="value">The property to add.</param>
169  /// <exception cref="ArgumentNullException">is thrown when value is null.</exception>
170  void AddInternal(T value)
171  {
172  if (System::ObjectExt::UnknownIsNull(value))
173  {
174  throw System::ArgumentNullException(u"value");
175  }
176 
177  this->dictionary->Add(value->get_Name(), value);
178  }
179 
180  /// <summary>
181  /// Remove a property from the collection by string key.
182  /// </summary>
183  /// <param name="key">The property name to remove.</param>
184  bool RemoveInternal(const System::String& key)
185  {
186  return this->dictionary->Remove(key);
187  }
188 
189  /// <summary>
190  /// Clears the collection.
191  /// </summary>
192  virtual void ClearInternal()
193  {
194  this->dictionary->Clear();
195  }
196 
197  void ThrowIfReadOnly()
198  {
199  if (this->get_IsReadOnly())
200  {
201  throw System::NotSupportedException(this->GetType().get_Name() + u" is read-only.");
202  }
203  }
204 
205  /// <summary>
206  /// Remove a property from the collection.
207  /// </summary>
208  /// <param name="item">The property to remove.</param>
209  bool RemoveInternal(T item)
210  {
211  System::String key;
212  for (auto&& kvp : this->dictionary)
213  {
214  if (System::ObjectExt::Equals(kvp.get_Value(), System::ExplicitCast<System::Object>(item)))
215  {
216  key = kvp.get_Key();
217  }
218  }
219 
220  return key != nullptr && this->RemoveInternal(key);
221  }
222 
223  /// <summary>
224  /// Sets the property by string key.
225  /// </summary>
226  /// <param name="key">
227  /// The key.
228  /// </param>
229  /// <param name="value">
230  /// The value.
231  /// </param>
232  virtual void Set(System::String key, T value)
233  {
234  this->dictionary->idx_set(key, value);
235  }
236 
237  /// <summary>
238  /// Gets the property by string key.
239  /// </summary>
240  /// <param name="key">
241  /// The key.
242  /// </param>
243  virtual T Get(System::String key)
244  {
245  return this->dictionary->idx_get(key);
246  }
247 
248  virtual ~PropertyKeyedCollection()
249  {
250  }
251 
252  #ifdef ASPOSE_GET_SHARED_MEMBERS
253  void GetSharedMembers(System::Object::shared_members_type& result) const override
254  {
255  PropertyCollection<T>::GetSharedMembers(result);
256 
257  result.Add("Aspose::Tasks::Properties::PropertyKeyedCollection::dictionary", this->dictionary);
258  }
259  #endif
260 
261 
262 private:
263 
264  System::SharedPtr<System::Collections::Generic::Dictionary<System::String, T>> dictionary;
265 
266  /// <inheritdoc ></inheritdoc>
267  bool Contains(const T& item) const override
268  {
269  for (auto&& kvp : this->dictionary)
270  {
271  if (System::ObjectExt::Equals(kvp.get_Value(), System::ExplicitCast<System::Object>(item)))
272  {
273  return true;
274  }
275  }
276 
277  return false;
278  }
279 
280  /// <inheritdoc ></inheritdoc>
281  bool Remove(const T& item) override
282  {
283  this->ThrowIfReadOnly();
284  return this->RemoveInternal(item);
285  }
286 
287  /// <inheritdoc ></inheritdoc>
288  void CopyTo(System::ArrayPtr<T> array, int32_t arrayIndex) override
289  {
290  this->dictionary->get_Values()->CopyTo(array, arrayIndex);
291  }
292 
293 
294 };
295 
296 } // namespace Properties
297 } // namespace Tasks
298 } // namespace Aspose
299 
300 
Represents a collection of built-in project properties.
Definition: BuiltInProjectPropertyCollection.h:40
Represents a collection of custom project properties.
Definition: CustomProjectPropertyCollection.h:50
A base class of collection of properties.
Definition: PropertyCollection.h:36
Represents a base class of a property.
Definition: Property.h:34
A base class of collection of properties.
Definition: PropertyKeyedCollection.h:48
int32_t get_Count() const override
Gets the number of properties in the collection.
Definition: PropertyKeyedCollection.h:77
void Clear() override
Definition: PropertyKeyedCollection.h:98
bool Contains(const System::String &name) const
Determines whether the Aspose::Tasks::Properties::PropertyCollection<T> contains a property with the ...
Definition: PropertyKeyedCollection.h:109
T idx_get(const System::String &name)
Gets the Property associated with the specified key.
Definition: PropertyKeyedCollection.h:92
System::SharedPtr< System::Collections::Generic::ICollection< System::String > > get_Names()
Gets the collection of all property names.
Definition: PropertyKeyedCollection.h:69
void Add(const T &item) override
Creates a new custom property.
Definition: PropertyKeyedCollection.h:118
bool get_IsReadOnly() const override=0
Gets a value indicating whether this collection is read-only; otherwise, false.
Aspose.
Definition: Asn.h:13