1 : /* -*- Mode: c++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : * ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Mozilla Corporation code.
16 : *
17 : * The Initial Developer of the Original Code is Mozilla Foundation.
18 : * Portions created by the Initial Developer are Copyright (Sub) 2011
19 : * the Initial Developer. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Jeff Muizelaar <jmuizelaar@mozilla.com>
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 2 or later (the "GPL"), or
26 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #ifndef MOZILLA_GFX_USERDATA_H_
39 : #define MOZILLA_GFX_USERDATA_H_
40 :
41 : #include <stdlib.h>
42 : #include "mozilla/mozalloc.h"
43 :
44 : namespace mozilla {
45 : namespace gfx {
46 :
47 : struct UserDataKey {
48 : int unused;
49 : };
50 :
51 : /* this class is basically a clone of the user data concept from cairo */
52 : class UserData
53 : {
54 : typedef void (*destroyFunc)(void *data);
55 : public:
56 0 : UserData() : count(0), entries(NULL) {}
57 :
58 : /* Attaches untyped userData associated with key. destroy is called on destruction */
59 0 : void Add(UserDataKey *key, void *userData, destroyFunc destroy)
60 : {
61 : // XXX we should really warn if user data with key has already been added,
62 : // since in that case Get() will return the old user data!
63 :
64 : // We could keep entries in a std::vector instead of managing it by hand
65 : // but that would propagate an stl dependency out which we'd rather not
66 : // do (see bug 666609). Plus, the entries array is expect to stay small
67 : // so doing a realloc everytime we add a new entry shouldn't be too costly
68 0 : entries = static_cast<Entry*>(moz_xrealloc(entries, sizeof(Entry)*(count+1)));
69 :
70 0 : entries[count].key = key;
71 0 : entries[count].userData = userData;
72 0 : entries[count].destroy = destroy;
73 :
74 0 : count++;
75 0 : }
76 :
77 : /* Remove and return user data associated with key, without destroying it */
78 0 : void* Remove(UserDataKey *key)
79 : {
80 0 : for (int i=0; i<count; i++) {
81 0 : if (key == entries[i].key) {
82 0 : void *userData = entries[i].userData;
83 : // decrement before looping so entries[i+1] doesn't read past the end:
84 0 : --count;
85 0 : for (;i<count; i++) {
86 0 : entries[i] = entries[i+1];
87 : }
88 0 : return userData;
89 : }
90 : }
91 0 : return NULL;
92 : }
93 :
94 : /* Retrives the userData for the associated key */
95 0 : void *Get(UserDataKey *key)
96 : {
97 0 : for (int i=0; i<count; i++) {
98 0 : if (key == entries[i].key) {
99 0 : return entries[i].userData;
100 : }
101 : }
102 0 : return NULL;
103 : }
104 :
105 0 : ~UserData()
106 : {
107 0 : for (int i=0; i<count; i++) {
108 0 : entries[i].destroy(entries[i].userData);
109 : }
110 0 : free(entries);
111 0 : }
112 :
113 : private:
114 : struct Entry {
115 : const UserDataKey *key;
116 : void *userData;
117 : destroyFunc destroy;
118 : };
119 :
120 : int count;
121 : Entry *entries;
122 :
123 : };
124 :
125 : }
126 : }
127 :
128 : #endif /* MOZILLA_GFX_USERDATA_H_ */
|