im_attrib.h

Go to the documentation of this file.
00001 /** \file
00002  * \brief Attributes Table.
00003  *
00004  * See Copyright Notice in im_lib.h
00005  */
00006 
00007 #ifndef __IM_ATTRIB_H_
00008 #define __IM_ATTRIB_H_
00009 
00010 #include "im_attrib_flat.h"
00011 
00012 /** \brief Attributes Table.
00013  *
00014  * \par
00015  * All the attributes have a name, a type, a count and the data.\n
00016  * Names are usually strings with less that 30 chars.
00017  * \par
00018  * Attributes are stored in a hash table for fast access. \n
00019  * We use the hash function described in "The Pratice of Programming" of Kernighan & Pike.
00020  * \ingroup util */
00021 class imAttribTable
00022 {
00023   imAttribTablePrivate* ptable; 
00024 public:
00025 
00026   /** Creates an empty table.
00027    * If size is zero the default size of 101 is used. Size must be a prime number. 
00028    * Other common values are 67, 599 and 1499.*/ 
00029   imAttribTable(int hash_size)
00030     { ptable = imAttribTableCreate(hash_size); }
00031 
00032   /** Destroys the table and all the attributes. */
00033   ~imAttribTable()
00034     { imAttribTableDestroy(ptable); ptable = 0; }
00035 
00036   /** Returns the number of elements in the table. */
00037   int Count() const
00038     { return imAttribTableCount(ptable); }
00039 
00040   /** Removes all the attributes in the table */
00041   void RemoveAll()
00042     { imAttribTableRemoveAll(ptable); }
00043 
00044   /** Copies the contents of the given table into this table. */
00045   void CopyFrom(const imAttribTable& table)
00046     { imAttribTableCopyFrom(ptable, table.ptable); }
00047 
00048   /** Inserts an attribute into the table. \n 
00049    * If data_type is BYTE then count can be -1 to indicate a NULL terminated string.
00050    * Data is duplicated if not NULL, else data is initialized with zeros.
00051    * See also \ref imDataType. */
00052   void Set(const char* name, int data_type, int count, const void* data)
00053     { imAttribTableSet(ptable, name, data_type, count, data); }
00054 
00055   /** Removes an attribute from the table given its name. */
00056   void UnSet(const char *name)
00057     { imAttribTableUnSet(ptable, name); }
00058 
00059   /** Finds an attribute in the table.
00060    * Returns the attribute if found, NULL otherwise. 
00061    * See also \ref imDataType. */
00062   const void* Get(const char *name, int *data_type = 0, int *count = 0) const
00063     { return imAttribTableGet(ptable, name, data_type, count); }
00064 
00065   /** For each attribute calls the user callback. If the callback returns 0 the function returns. */
00066   void ForEach(void* user_data, imAttribTableCallback attrib_func) const
00067     { imAttribTableForEach(ptable, user_data, attrib_func); }
00068 };
00069 
00070 /** \brief Attributes Table.
00071  *
00072  * \par
00073  * Same as \ref imAttribTable, but uses an array of fixed size.
00074  * \ingroup util */
00075 class imAttribArray
00076 {
00077   imAttribTablePrivate* ptable; 
00078 public:
00079 
00080   /** Creates an empty array. */ 
00081   imAttribArray(int count)
00082     { ptable = imAttribArrayCreate(count); }
00083 
00084   /** Destroys the array and all the attributes. */
00085   ~imAttribArray()
00086     { imAttribTableDestroy(ptable); ptable = 0; }
00087 
00088   /** Returns the number of elements in the array. */
00089   int Count() const
00090     { return imAttribTableCount(ptable); }
00091 
00092   /** Removes all the attributes in the array */
00093   void RemoveAll()
00094     { imAttribTableRemoveAll(ptable); }
00095 
00096   /** Copies the contents of the given table into this table. */
00097   void CopyFrom(const imAttribArray& table)
00098     { imAttribArrayCopyFrom(ptable, table.ptable); }
00099 
00100   /** Inserts one attribute into the array. 
00101    * The attribute data is a simple array of data_type elements of count length. \n 
00102    * Data is duplicated if not NULL, else data is initialized with zeros. 
00103    * When NULL is specified use the Get method to retrieve a pointer to the data 
00104    * so you can initialize it with other values.
00105    * See also \ref imDataType. */
00106   void Set(int index, const char* name, int data_type, int count, const void* data)
00107     { imAttribArraySet(ptable, index, name, data_type, count, data); }
00108 
00109   /** Finds one attribute in the array.
00110    * Returns the attribute if found, NULL otherwise.
00111    * See also \ref imDataType. */
00112   const void* Get(int index, char *name = 0, int *data_type = 0, int *count = 0) const
00113     { return imAttribArrayGet(ptable, index, name, data_type, count); }
00114 
00115   /** For each attribute calls the user callback. If the callback returns 0 the function returns. */
00116   void ForEach(void* user_data, imAttribTableCallback attrib_func) const
00117     { imAttribTableForEach(ptable, user_data, attrib_func); }
00118 };
00119 
00120 #endif

Generated on Thu Oct 1 11:40:01 2009 for IM by  doxygen 1.6.1