js/public/WeakMapPtr.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/public/WeakMapPtr.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,48 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
     1.5 + * vim: set ts=8 sts=4 et sw=4 tw=99:
     1.6 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#ifndef js_WeakMapPtr_h
    1.11 +#define js_WeakMapPtr_h
    1.12 +
    1.13 +#include "jspubtd.h"
    1.14 +
    1.15 +#include "js/TypeDecls.h"
    1.16 +
    1.17 +namespace JS {
    1.18 +
    1.19 +// A wrapper around the internal C++ representation of SpiderMonkey WeakMaps,
    1.20 +// usable outside the engine.
    1.21 +//
    1.22 +// The supported template specializations are enumerated in WeakMapPtr.cpp. If
    1.23 +// you want to use this class for a different key/value combination, add it to
    1.24 +// the list and the compiler will generate the relevant machinery.
    1.25 +template <typename K, typename V>
    1.26 +class JS_PUBLIC_API(WeakMapPtr)
    1.27 +{
    1.28 +  public:
    1.29 +    WeakMapPtr() : ptr(nullptr) {};
    1.30 +    bool init(JSContext *cx);
    1.31 +    bool initialized() { return ptr != nullptr; };
    1.32 +    void destroy();
    1.33 +    virtual ~WeakMapPtr() { MOZ_ASSERT(!initialized()); }
    1.34 +    void trace(JSTracer *tracer);
    1.35 +
    1.36 +    V lookup(const K &key);
    1.37 +    bool put(JSContext *cx, const K &key, const V &value);
    1.38 +
    1.39 +    static void keyMarkCallback(JSTracer *trc, K key, void *data);
    1.40 +
    1.41 +  private:
    1.42 +    void *ptr;
    1.43 +
    1.44 +    // WeakMapPtr is neither copyable nor assignable.
    1.45 +    WeakMapPtr(const WeakMapPtr &wmp) MOZ_DELETE;
    1.46 +    WeakMapPtr &operator=(const WeakMapPtr &wmp) MOZ_DELETE;
    1.47 +};
    1.48 +
    1.49 +} /* namespace JS */
    1.50 +
    1.51 +#endif  /* js_WeakMapPtr_h */

mercurial