2012-01-13 18:47:58 +04:00
|
|
|
/* -*- Mode: c++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2012-05-21 15:12:37 +04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2012-01-13 18:47:58 +04:00
|
|
|
|
2012-03-02 12:28:59 +04:00
|
|
|
#ifndef MOZILLA_GFX_USERDATA_H_
|
|
|
|
#define MOZILLA_GFX_USERDATA_H_
|
2012-01-13 18:47:58 +04:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
2012-08-15 23:24:44 +04:00
|
|
|
#include "Types.h"
|
2012-04-12 04:03:07 +04:00
|
|
|
#include "mozilla/Assertions.h"
|
2012-01-13 18:47:58 +04:00
|
|
|
|
|
|
|
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:
|
2012-08-14 22:06:12 +04:00
|
|
|
UserData() : count(0), entries(nullptr) {}
|
2012-01-13 18:47:58 +04:00
|
|
|
|
|
|
|
/* Attaches untyped userData associated with key. destroy is called on destruction */
|
|
|
|
void Add(UserDataKey *key, void *userData, destroyFunc destroy)
|
|
|
|
{
|
2012-07-17 21:03:50 +04:00
|
|
|
for (int i=0; i<count; i++) {
|
|
|
|
if (key == entries[i].key) {
|
|
|
|
if (entries[i].destroy) {
|
|
|
|
entries[i].destroy(entries[i].userData);
|
|
|
|
}
|
|
|
|
entries[i].userData = userData;
|
|
|
|
entries[i].destroy = destroy;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2012-03-02 12:28:59 +04:00
|
|
|
|
2012-01-13 18:47:58 +04:00
|
|
|
// 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
|
2012-04-12 04:03:07 +04:00
|
|
|
entries = static_cast<Entry*>(realloc(entries, sizeof(Entry)*(count+1)));
|
|
|
|
|
|
|
|
if (!entries) {
|
2016-05-13 23:34:41 +03:00
|
|
|
MOZ_CRASH("GFX: UserData::Add");
|
2012-04-12 04:03:07 +04:00
|
|
|
}
|
2012-01-13 18:47:58 +04:00
|
|
|
|
|
|
|
entries[count].key = key;
|
|
|
|
entries[count].userData = userData;
|
|
|
|
entries[count].destroy = destroy;
|
|
|
|
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
2012-03-02 12:28:59 +04:00
|
|
|
/* Remove and return user data associated with key, without destroying it */
|
|
|
|
void* Remove(UserDataKey *key)
|
|
|
|
{
|
|
|
|
for (int i=0; i<count; i++) {
|
|
|
|
if (key == entries[i].key) {
|
|
|
|
void *userData = entries[i].userData;
|
|
|
|
// decrement before looping so entries[i+1] doesn't read past the end:
|
|
|
|
--count;
|
|
|
|
for (;i<count; i++) {
|
|
|
|
entries[i] = entries[i+1];
|
|
|
|
}
|
|
|
|
return userData;
|
|
|
|
}
|
|
|
|
}
|
2012-08-14 22:06:12 +04:00
|
|
|
return nullptr;
|
2012-03-02 12:28:59 +04:00
|
|
|
}
|
2012-01-13 18:47:58 +04:00
|
|
|
|
|
|
|
/* Retrives the userData for the associated key */
|
2013-07-02 20:27:17 +04:00
|
|
|
void *Get(UserDataKey *key) const
|
2012-01-13 18:47:58 +04:00
|
|
|
{
|
|
|
|
for (int i=0; i<count; i++) {
|
|
|
|
if (key == entries[i].key) {
|
|
|
|
return entries[i].userData;
|
|
|
|
}
|
|
|
|
}
|
2012-08-14 22:06:12 +04:00
|
|
|
return nullptr;
|
2012-01-13 18:47:58 +04:00
|
|
|
}
|
|
|
|
|
2012-08-04 04:38:44 +04:00
|
|
|
bool Has(UserDataKey *key)
|
|
|
|
{
|
|
|
|
for (int i=0; i<count; i++) {
|
|
|
|
if (key == entries[i].key) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-07-17 21:03:50 +04:00
|
|
|
void Destroy()
|
2012-01-13 18:47:58 +04:00
|
|
|
{
|
|
|
|
for (int i=0; i<count; i++) {
|
2012-07-17 21:03:50 +04:00
|
|
|
if (entries[i].destroy) {
|
|
|
|
entries[i].destroy(entries[i].userData);
|
|
|
|
}
|
2012-01-13 18:47:58 +04:00
|
|
|
}
|
|
|
|
free(entries);
|
2012-08-14 22:06:12 +04:00
|
|
|
entries = nullptr;
|
2012-07-17 21:03:50 +04:00
|
|
|
count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
~UserData()
|
|
|
|
{
|
|
|
|
Destroy();
|
2012-01-13 18:47:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct Entry {
|
|
|
|
const UserDataKey *key;
|
|
|
|
void *userData;
|
|
|
|
destroyFunc destroy;
|
|
|
|
};
|
|
|
|
|
|
|
|
int count;
|
|
|
|
Entry *entries;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace gfx
|
|
|
|
} // namespace mozilla
|
2012-01-13 18:47:58 +04:00
|
|
|
|
2012-03-02 12:28:59 +04:00
|
|
|
#endif /* MOZILLA_GFX_USERDATA_H_ */
|