summaryrefslogtreecommitdiff
path: root/im/include/im_attrib.h
blob: 76302ef57dd4718fc7bbceb19b89a6f3f13d8000 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/** \file
 * \brief Attributes Table.
 *
 * See Copyright Notice in im_lib.h
 */

#ifndef __IM_ATTRIB_H_
#define __IM_ATTRIB_H_

#include "im_attrib_flat.h"

/** \brief Attributes Table.
 *
 * \par
 * All the attributes have a name, a type, a count and the data.\n
 * Names are usually strings with less that 30 chars.
 * \par
 * Attributes are stored in a hash table for fast access. \n
 * We use the hash function described in "The Pratice of Programming" of Kernighan & Pike.
 * \ingroup util */
class imAttribTable
{
  imAttribTablePrivate* ptable; 
public:

  /** Creates an empty table.
   * If size is zero the default size of 101 is used. Size must be a prime number. 
   * Other common values are 67, 599 and 1499.*/ 
  imAttribTable(int hash_size)
    { ptable = imAttribTableCreate(hash_size); }

  /** Destroys the table and all the attributes. */
  ~imAttribTable()
    { imAttribTableDestroy(ptable); ptable = 0; }

  /** Returns the number of elements in the table. */
  int Count() const
    { return imAttribTableCount(ptable); }

  /** Removes all the attributes in the table */
  void RemoveAll()
    { imAttribTableRemoveAll(ptable); }

  /** Copies the contents of the given table into this table. */
  void CopyFrom(const imAttribTable& table)
    { imAttribTableCopyFrom(ptable, table.ptable); }

  /** Inserts an attribute into the table. \n 
   * If data_type is BYTE then count can be -1 to indicate a NULL terminated string.
   * Data is duplicated if not NULL, else data is initialized with zeros.
   * See also \ref imDataType. */
  void Set(const char* name, int data_type, int count, const void* data)
    { imAttribTableSet(ptable, name, data_type, count, data); }

  /** Removes an attribute from the table given its name. */
  void UnSet(const char *name)
    { imAttribTableUnSet(ptable, name); }

  /** Finds an attribute in the table.
   * Returns the attribute if found, NULL otherwise. 
   * See also \ref imDataType. */
  const void* Get(const char *name, int *data_type = 0, int *count = 0) const
    { return imAttribTableGet(ptable, name, data_type, count); }

  /** For each attribute calls the user callback. If the callback returns 0 the function returns. */
  void ForEach(void* user_data, imAttribTableCallback attrib_func) const
    { imAttribTableForEach(ptable, user_data, attrib_func); }
};

/** \brief Attributes Table.
 *
 * \par
 * Same as \ref imAttribTable, but uses an array of fixed size.
 * \ingroup util */
class imAttribArray
{
  imAttribTablePrivate* ptable; 
public:

  /** Creates an empty array. */ 
  imAttribArray(int count)
    { ptable = imAttribArrayCreate(count); }

  /** Destroys the array and all the attributes. */
  ~imAttribArray()
    { imAttribTableDestroy(ptable); ptable = 0; }

  /** Returns the number of elements in the array. */
  int Count() const
    { return imAttribTableCount(ptable); }

  /** Removes all the attributes in the array */
  void RemoveAll()
    { imAttribTableRemoveAll(ptable); }

  /** Copies the contents of the given table into this table. */
  void CopyFrom(const imAttribArray& table)
    { imAttribArrayCopyFrom(ptable, table.ptable); }

  /** Inserts one attribute into the array. 
   * The attribute data is a simple array of data_type elements of count length. \n 
   * Data is duplicated if not NULL, else data is initialized with zeros. 
   * When NULL is specified use the Get method to retrieve a pointer to the data 
   * so you can initialize it with other values.
   * See also \ref imDataType. */
  void Set(int index, const char* name, int data_type, int count, const void* data)
    { imAttribArraySet(ptable, index, name, data_type, count, data); }

  /** Finds one attribute in the array.
   * Returns the attribute if found, NULL otherwise.
   * See also \ref imDataType. */
  const void* Get(int index, char *name = 0, int *data_type = 0, int *count = 0) const
    { return imAttribArrayGet(ptable, index, name, data_type, count); }

  /** For each attribute calls the user callback. If the callback returns 0 the function returns. */
  void ForEach(void* user_data, imAttribTableCallback attrib_func) const
    { imAttribTableForEach(ptable, user_data, attrib_func); }
};

#endif