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