KeyValues: Difference between revisions
Jump to navigation
Jump to search
mNo edit summary |
mNo edit summary |
||
Line 2: | Line 2: | ||
The KeyValues class can load the input from a data buffer or a file. | The KeyValues class can load the input from a data buffer or a file. | ||
==<font color="red">Important Note</font>== | |||
* When you have completed using a KeyValues object, make sure you always check if is is NULL and if not, call the object's <code>deleteThis</code> function. | |||
* If you fail to delete your KeyValues objects, you will end up with memory leaks. | |||
* It's also important to note that many instances of the usage of the KeyValues in the SDK do not call their <code>deleteThis</code> function, therefore making a memory leak.<br />The following is common example of this:'' | |||
ComboBox *pCombo = new ComboBox(...); | |||
pCombo->AddItem("Text", new KeyValues("UserData", "Key", "Value")); | |||
It's important to note that if you were to follow what is called by the AddItem function, you would see that the UserData KeyValues are copied, rather than using the actual input. Copying KeyValues is a common source of confusion of how your KeyValues objects are used, so make sure to follow where they go. If you are unsure, you could try calling the <code>deleteThis</code> function and see if the same functionality as before still exists. | |||
===Fix=== | |||
ComboBox *pCombo = new ComboBox(...); | |||
KeyValues *kv = new KeyValues("UserData", "Key", "Value"); | |||
pCombo->AddItem("Text", kv); | |||
kv->deleteThis(); | |||
==Format== | ==Format== | ||
Line 15: | Line 31: | ||
"SubKey2" | "SubKey2" | ||
{ | { | ||
<font color=" | <font color="blue">And so on...</font> | ||
} | } | ||
} | } |
Revision as of 21:13, 10 January 2006
KeyValues handles input and output.
The KeyValues class can load the input from a data buffer or a file.
Important Note
- When you have completed using a KeyValues object, make sure you always check if is is NULL and if not, call the object's
deleteThis
function. - If you fail to delete your KeyValues objects, you will end up with memory leaks.
- It's also important to note that many instances of the usage of the KeyValues in the SDK do not call their
deleteThis
function, therefore making a memory leak.
The following is common example of this:
ComboBox *pCombo = new ComboBox(...); pCombo->AddItem("Text", new KeyValues("UserData", "Key", "Value"));
It's important to note that if you were to follow what is called by the AddItem function, you would see that the UserData KeyValues are copied, rather than using the actual input. Copying KeyValues is a common source of confusion of how your KeyValues objects are used, so make sure to follow where they go. If you are unsure, you could try calling the deleteThis
function and see if the same functionality as before still exists.
Fix
ComboBox *pCombo = new ComboBox(...); KeyValues *kv = new KeyValues("UserData", "Key", "Value"); pCombo->AddItem("Text", kv); kv->deleteThis();
Format
The format of a data buffer or file is the following.
"SuperKey1" "Value" "SuperKey2" { "Key1" "Value" "Key2" { "SubKey1" "Value" "SubKey2" { And so on... } } }
Useful Traverses
The following loops are contained in the following function: void Traverse(KeyValues *pKV);
for ( KeyValues *sub = pKV->GetFirstSubKey(); sub; sub = sub->GetNextKey() )
- This loop steps through all subkeys contained in pKV.
- To handle subkeys with their own subkeys in pKV, you may want use a recursive loop. To check if a subkey has subkeys, do the following:
if(sub->GetFirstSubKey()) { Traverse(sub); }
for ( KeyValues *sub = pKV->GetFirstValue(); sub; sub = sub->GetNextValue() )
- This loop steps through all subkeys contained in a pKV that have values.
- It is also valid to use the KeyValues member function
GetDataType
in the loop to do a type specific traverse.