soui 5.0.0.1
Soui5 Doc
 
Loading...
Searching...
No Matches
SCmnMap.h
1#ifndef __SCMNMAP__H__
2#define __SCMNMAP__H__
3
4#include <souicoll.h>
5
6SNSBEGIN
7
8/**
9 * @class SCmnMap
10 * @brief Template class for managing a map of objects with keys
11 *
12 * @details This template class provides a map-like structure to store and manage objects
13 * associated with keys. It includes methods for adding, retrieving, and removing
14 * objects, as well as handling key removal callbacks.
15 *
16 * @tparam TObj Type of the objects stored in the map
17 * @tparam TKey Type of the keys used to identify objects (default is SStringA)
18 */
19template <class TObj, class TKey = SStringA>
20class SCmnMap {
21 public:
22 /**
23 * @brief Constructor
24 * @param funOnKeyRemoved Callback function to be called when a key is removed
25 *
26 * @details Initializes the map and sets the callback function for key removal.
27 */
28 SCmnMap(void (*funOnKeyRemoved)(const TObj &) = NULL)
29 : m_pFunOnKeyRemoved(funOnKeyRemoved)
30 {
31 m_mapNamedObj = new SMap<TKey, TObj>;
32 }
33
34 /**
35 * @brief Destructor
36 *
37 * @details Cleans up the map and deletes the internal map object.
38 */
39 virtual ~SCmnMap()
40 {
41 RemoveAll();
42 delete m_mapNamedObj;
43 }
44
45 /**
46 * @brief Checks if a key exists in the map
47 * @param key Key to check
48 * @return TRUE if the key exists, otherwise FALSE
49 *
50 * @details Returns whether the specified key is present in the map.
51 */
52 bool HasKey(const TKey &key) const
53 {
54 return m_mapNamedObj->Lookup(key) != NULL;
55 }
56
57 /**
58 * @brief Retrieves an object associated with a key
59 * @param key Key to look up
60 * @param obj Object to store the retrieved value
61 * @return TRUE if the key exists and the object is retrieved, otherwise FALSE
62 *
63 * @details Retrieves the object associated with the specified key and stores it in the provided object.
64 */
65 bool GetKeyObject(const TKey &key, TObj &obj) const
66 {
67 if (!HasKey(key))
68 return false;
69 obj = (*m_mapNamedObj)[key];
70 return true;
71 }
72
73 /**
74 * @brief Retrieves an object associated with a key
75 * @param key Key to look up
76 * @return Reference to the object associated with the key
77 *
78 * @details Returns a reference to the object associated with the specified key.
79 * Note: This method does not check if the key exists.
80 */
81 TObj &GetKeyObject(const TKey &key) const
82 {
83 return (*m_mapNamedObj)[key];
84 }
85
86 /**
87 * @brief Adds a key-object pair to the map
88 * @param key Key to add
89 * @param obj Object to associate with the key
90 * @return TRUE if the key was added successfully, otherwise FALSE
91 *
92 * @details Adds a new key-object pair to the map. Returns FALSE if the key already exists.
93 */
94 bool AddKeyObject(const TKey &key, const TObj &obj)
95 {
96 if (HasKey(key))
97 return false;
98 (*m_mapNamedObj)[key] = obj;
99 return true;
100 }
101
102 /**
103 * @brief Sets an object for a key, replacing any existing object
104 * @param key Key to set
105 * @param obj Object to associate with the key
106 *
107 * @details Sets the object for the specified key, removing any existing object associated with the key.
108 */
109 void SetKeyObject(const TKey &key, const TObj &obj)
110 {
111 RemoveKeyObject(key);
112 (*m_mapNamedObj)[key] = obj;
113 }
114
115 /**
116 * @brief Removes a key-object pair from the map
117 * @param key Key to remove
118 * @return TRUE if the key was removed successfully, otherwise FALSE
119 *
120 * @details Removes the key-object pair associated with the specified key.
121 * Calls the callback function if it is set.
122 */
123 bool RemoveKeyObject(const TKey &key)
124 {
125 if (!HasKey(key))
126 return false;
128 {
130 }
131 m_mapNamedObj->RemoveKey(key);
132 return true;
133 }
134
135 /**
136 * @brief Removes all key-object pairs from the map
137 *
138 * @details Removes all key-object pairs from the map and calls the callback function
139 * for each removed object if it is set.
140 */
142 {
144 {
145 SPOSITION pos = m_mapNamedObj->GetStartPosition();
146 while (pos)
147 {
148 typename SMap<TKey, TObj>::CPair *p = m_mapNamedObj->GetNext(pos);
149 m_pFunOnKeyRemoved(p->m_value);
150 }
151 }
152 m_mapNamedObj->RemoveAll();
153 }
154
155 /**
156 * @brief Gets the number of key-object pairs in the map
157 * @return Number of key-object pairs
158 *
159 * @details Returns the total number of key-object pairs in the map.
160 */
161 size_t GetCount()
162 {
163 return m_mapNamedObj->GetCount();
164 }
165
166 protected:
167 void (*m_pFunOnKeyRemoved)(const TObj &obj); /**< Callback function for key removal. */
168 SMap<TKey, TObj> *m_mapNamedObj; /**< Internal map object. */
169};
170
171SNSEND
172
173#endif // __SCMNMAP__H__
void RemoveAll()
Removes all key-object pairs from the map.
Definition SCmnMap.h:141
virtual ~SCmnMap()
Destructor.
Definition SCmnMap.h:39
bool GetKeyObject(const TKey &key, TObj &obj) const
Retrieves an object associated with a key.
Definition SCmnMap.h:65
SCmnMap(void(*funOnKeyRemoved)(const TObj &)=NULL)
Constructor.
Definition SCmnMap.h:28
void SetKeyObject(const TKey &key, const TObj &obj)
Sets an object for a key, replacing any existing object.
Definition SCmnMap.h:109
TObj & GetKeyObject(const TKey &key) const
Retrieves an object associated with a key.
Definition SCmnMap.h:81
SMap< TKey, TObj > * m_mapNamedObj
Definition SCmnMap.h:168
bool AddKeyObject(const TKey &key, const TObj &obj)
Adds a key-object pair to the map.
Definition SCmnMap.h:94
size_t GetCount()
Gets the number of key-object pairs in the map.
Definition SCmnMap.h:161
bool RemoveKeyObject(const TKey &key)
Removes a key-object pair from the map.
Definition SCmnMap.h:123
bool HasKey(const TKey &key) const
Checks if a key exists in the map.
Definition SCmnMap.h:52
void(* m_pFunOnKeyRemoved)(const TObj &obj)
Definition SCmnMap.h:167