Bug 717921. Add UserData to DrawTarget. r=bas

* * *
Bug 717921. Fix bustage about moz_xrealloc.

--HG--
extra : rebase_source : 62d38e561d1e64c725587b1de4d1ae609b828557
This commit is contained in:
Jeff Muizelaar 2012-01-13 09:47:58 -05:00
parent a290bd3e5f
commit 3fadf04e1c
3 changed files with 117 additions and 1 deletions

View File

@ -41,7 +41,7 @@
#include "Point.h"
#include "Rect.h"
#include "Matrix.h"
#include "UserData.h"
// This RefPtr class isn't ideal for usage in Azure, as it doesn't allow T**
// outparams using the &-operator. But it will have to do as there's no easy
// solution.
@ -744,7 +744,14 @@ public:
*/
virtual void *GetNativeSurface(NativeSurfaceType aType) { return NULL; }
void AddUserData(UserDataKey *key, void *userData, void (*destroy)(void*)) {
mUserData.Add(key, userData, destroy);
}
void *GetUserData(UserDataKey *key) {
return mUserData.Get(key);
}
protected:
UserData mUserData;
Matrix mTransform;
bool mTransformDirty : 1;

View File

@ -62,6 +62,7 @@ EXPORTS_mozilla/gfx = \
Matrix.h \
Rect.h \
Types.h \
UserData.h \
$(NULL)
CPPSRCS = \

108
gfx/2d/UserData.h Normal file
View File

@ -0,0 +1,108 @@
/* -*- Mode: c++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla Corporation code.
*
* The Initial Developer of the Original Code is Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (Sub) 2011
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Jeff Muizelaar <jmuizelaar@mozilla.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include <stdlib.h>
#include "mozilla/mozalloc.h"
namespace mozilla {
namespace gfx {
struct UserDataKey {
int unused;
};
/* this class is basically a clone of the user data concept from cairo */
class UserData
{
typedef void (*destroyFunc)(void *data);
public:
UserData() : count(0), entries(NULL) {}
/* Attaches untyped userData associated with key. destroy is called on destruction */
void Add(UserDataKey *key, void *userData, destroyFunc destroy)
{
// We could keep entries in a std::vector instead of managing it by hand
// but that would propagate an stl dependency out which we'd rather not
// do (see bug 666609). Plus, the entries array is expect to stay small
// so doing a realloc everytime we add a new entry shouldn't be too costly
entries = static_cast<Entry*>(moz_xrealloc(entries, sizeof(Entry)*(count+1)));
entries[count].key = key;
entries[count].userData = userData;
entries[count].destroy = destroy;
count++;
}
//XXX: we probably want to add a way to remove Keys
/* Retrives the userData for the associated key */
void *Get(UserDataKey *key)
{
for (int i=0; i<count; i++) {
if (key == entries[i].key) {
return entries[i].userData;
}
}
return NULL;
}
~UserData()
{
for (int i=0; i<count; i++) {
entries[i].destroy(entries[i].userData);
}
free(entries);
}
private:
struct Entry {
const UserDataKey *key;
void *userData;
destroyFunc destroy;
};
int count;
Entry *entries;
};
}
}