Sourceforge.net - The VCF's Project Host
   The VCF Website Home   |   Online Discussion Forums   |   Sourceforge.net Project Page   

VCF::Dictionary Class Reference

The Dictionary class is a utility class useful storing a collection of key-value pairs. More...

#include <vcf/FoundationKit/Dictionary.h>

Inheritance diagram for VCF::Dictionary:

VCF::Object VCF::Persistable VCF::PropertyListing List of all members.

Public Types

typedef std::map< String,
VariantData
DictionaryMap
typedef String Key
typedef VariantData Value
typedef std::pair< const String,
VariantData
pair
typedef uint32 size_type
typedef Enumerator< pairEnumerator

Public Member Functions

 Dictionary ()
 Dictionary (const Dictionary &rhs)
Dictionaryoperator= (const Dictionary &rhs)
virtual ~Dictionary ()
virtual Objectclone (bool deep=false) const
 Makes a complete clone of this object.
size_type size () const
 returns the number of elements in the dictionary.
size_type max_size () const
 returns the maximum size of the dictionary.
bool empty () const
 Returns true if the dictionary has no values in it, otherwise returns false.
Valueoperator[] (const Key &key)
 Returns a VariantData reference to the specified key.
Value operator[] (const Key &key) const
 Returns a VariantData copy to the specified key.
Valueget (const Key &key)
 Returns a VariantData reference to the specified key.
Value get (const Key &key) const
 Returns a VariantData copy to the specified key.
bool keyExists (const Key &key) const
 Returns whether or not the specified key exists in the collection.
void insert (const Key &key, const Value &value)
void remove (const Key &key)
void clear ()
bool getOwnsObjectValues () const
 Returns whether or not the dictionary "owns" the object values in it.
void setOwnsObjectValues (const bool &val)
 Sets the owns object value flag, which tells the dictionary whether or not it should clean up object values when the dictionary is destroyed.
virtual void loadFromStream (InputStream *stream, const MIMEType &type=MIMEType())
 Read the object from the specified input stream.
virtual void saveToStream (OutputStream *stream, const MIMEType &type=MIMEType())
 Write the object to the specified output stream.
Dictionary::EnumeratorgetEnumerator ()
const Dictionary::EnumeratorgetEnumerator () const

Protected Attributes

DictionaryMap data_
DictionaryEnumerator dataContainer_
bool ownsObjectValues_

Detailed Description

The Dictionary class is a utility class useful storing a collection of key-value pairs.

It is not a separate implementation, but instead uses the STL std::map as it's underlying data member. Usage of the class is the same as the std::map, and it is basically the same as working with a std::map<String,VariantData>. The class is intended for use when you want to have a named collection of various values. Using the VariantData as the value type, allows you to easily store any type of value. For example:

Dictionary dict;

dict["size"] = 200;
dict["Name"] = "Bob";
We now have two entries, one name "Name" and one named "size".

The other primary difference from a std::map<String,VariantData> is the support for persistence. The Dictionary class implements the Persistable interface, and can be written to, or read from, an OutputStream or InputStream (respectively). For example, to save to a file:

FileOutputStream fs("test.dict.txt");
fs &lt;&lt; &amp;dict;
And to read from a file:
FileInputStream fs("test.dict.txt");
fs &gt;&gt; &amp;dict;

The reason for deriving the Dictionary class from the Object root class is so that a Dictionary instance may itself be a value in a "parent" dictionary. For example:

Dictionary stuff;
Dictionary moreStuff;

stuff["food"] = "empty";

moreStuff["paintings"] = 10;
moreStuff["lightbulbs"] = 1298;

stuff["junk"] = &moreStuff;

If you'd like a fancier persistence scheme, in the Dictionaries example there's a simple implementation of reading and writing the dictionary to an xml based stream. The format for the xml tags is the same as that used Apple's PList xml format. See http://developer.apple.com/documentation/Cocoa/Conceptual/PropertyLists/Concepts/XMLPListsConcept.html for more information on this format.


Member Typedef Documentation

typedef std::map<String,VariantData> VCF::Dictionary::DictionaryMap
 

typedef Enumerator<pair> VCF::Dictionary::Enumerator
 

typedef String VCF::Dictionary::Key
 

typedef std::pair<const String,VariantData> VCF::Dictionary::pair
 

typedef uint32 VCF::Dictionary::size_type
 

typedef VariantData VCF::Dictionary::Value
 


Constructor & Destructor Documentation

VCF::Dictionary::Dictionary  ) 
 

VCF::Dictionary::Dictionary const Dictionary rhs  ) 
 

virtual VCF::Dictionary::~Dictionary  )  [virtual]
 


Member Function Documentation

void VCF::Dictionary::clear  ) 
 

virtual Object* VCF::Dictionary::clone bool  deep = false  )  const [inline, virtual]
 

Makes a complete clone of this object.

A typical implementation might be:

    virtual Object* clone( bool deep ) {
        return new MyObject( this );
    }
In which the implementer simply creates a new instance on the heap and calls the copy constructor. Objects which support cloning should also have a copy constructor defined as well.

Parameters:
bool deep if deep is true then any object instances that this object owns should probably be cloned as well, with full new copies made. Otherwise it would be acceptable to simply copy the pointer values. The default value is for deep cloning to be false.
Returns:
Object a new instance that should be an copy of this. If the object doesn't support cloning the return will value will be NULL.

Reimplemented from VCF::Object.

bool VCF::Dictionary::empty  )  const
 

Returns true if the dictionary has no values in it, otherwise returns false.

Value VCF::Dictionary::get const Key key  )  const
 

Returns a VariantData copy to the specified key.

Value& VCF::Dictionary::get const Key key  ) 
 

Returns a VariantData reference to the specified key.

const Dictionary::Enumerator* VCF::Dictionary::getEnumerator  )  const
 

Dictionary::Enumerator* VCF::Dictionary::getEnumerator  ) 
 

bool VCF::Dictionary::getOwnsObjectValues  )  const [inline]
 

Returns whether or not the dictionary "owns" the object values in it.

If it does it will clean up these instances when the dictionary is destroyed.

void VCF::Dictionary::insert const Key key,
const Value value
 

bool VCF::Dictionary::keyExists const Key key  )  const
 

Returns whether or not the specified key exists in the collection.

virtual void VCF::Dictionary::loadFromStream InputStream stream,
const MIMEType type = MIMEType()
[virtual]
 

Read the object from the specified input stream.

Parameters:
InputStream 
MIMEType indicates how to read the data from the stream. For example, if the object was some sort of image object, and had an array of pixels, then the type might indicate how the data in the stream should be interpreted, such as the JPEG format ("image/jpeg"), or the PNG format ("image/png").

Implements VCF::Persistable.

Reimplemented in VCF::PropertyListing.

size_type VCF::Dictionary::max_size  )  const
 

returns the maximum size of the dictionary.

Dictionary& VCF::Dictionary::operator= const Dictionary rhs  ) 
 

Value VCF::Dictionary::operator[] const Key key  )  const
 

Returns a VariantData copy to the specified key.

Value& VCF::Dictionary::operator[] const Key key  ) 
 

Returns a VariantData reference to the specified key.

void VCF::Dictionary::remove const Key key  ) 
 

virtual void VCF::Dictionary::saveToStream OutputStream stream,
const MIMEType type = MIMEType()
[virtual]
 

Write the object to the specified output stream.

Parameters:
OutputStream 
MIMEType indicates how the object should write it's data. For example, if the object was some sort of image object, and had an array of pixels, then the type might indicate what kind of image type to write the data in, such as the JPEG format ("image/jpeg"), or the PNG format ("image/png").

Implements VCF::Persistable.

Reimplemented in VCF::PropertyListing.

void VCF::Dictionary::setOwnsObjectValues const bool &  val  )  [inline]
 

Sets the owns object value flag, which tells the dictionary whether or not it should clean up object values when the dictionary is destroyed.

size_type VCF::Dictionary::size  )  const
 

returns the number of elements in the dictionary.


Member Data Documentation

DictionaryMap VCF::Dictionary::data_ [protected]
 

DictionaryEnumerator VCF::Dictionary::dataContainer_ [mutable, protected]
 

bool VCF::Dictionary::ownsObjectValues_ [protected]
 


The documentation for this class was generated from the following file:
   Comments or Suggestions?    License Information