Make UniquePersistent moveable in C++11

271 views
Skip to first unread message

Pavel Medvedev

unread,
Jul 14, 2014, 10:24:52 AM7/14/14
to v8-u...@googlegroups.com
Hello,

Is anybody storing UniquePersistent values in standard associative containers? When I tried to store UniquePersistent values in a std::unordered_map, I got an error with GCC:

#include <unordered_map>
#include <v8.h>

void add(v8::Isolate* isolate, int key, v8::Handle<v8::Value> value)
{
    std::unordered_map<int, v8::UniquePersistent<v8::Value>> map;

    v8::UniquePersistent<v8::Value> pvalue(isolate, value);
    map.insert(std::make_pair(key, std::move(pvalue)));
}

When I add move constructor and assignment, it works:

// Moveable unique persistent
template<typename T>
struct MoveablePersistent : public v8::UniquePersistent<T>
{
    typedef v8::UniquePersistent<T> base_class;

    MoveablePersistent()
        : base_class()
    {
    }

    template<typename S>
    MoveablePersistent(v8::Isolate* isolate, v8::Handle<S> const& handle)
        : base_class(isolate, handle)
    {
    }

    template<typename S>
    MoveablePersistent(v8::Isolate* isolate, v8::PersistentBase<S> const& handle)
        : base_class(isolate, handle)
    {
    }

    MoveablePersistent(MoveablePersistent&& src)
        : base_class(src.Pass())
    {
    }

    MoveablePersistent& operator=(MoveablePersistent&& src)
    {
        if (&src != this)
        {
            base_class::operator=(src.Pass());
        }
        return *this;
    }
};


void add(v8::Isolate* isolate, int key, v8::Handle<v8::Value> value)
{
    std::unordered_map<int, MoveablePersistent<v8::Value>> map;

    MoveablePersistent<v8::Value> pvalue(isolate, value);
    map.insert(std::make_pair(key, std::move(pvalue)));
}

Is it possible to add move semanics support for UniquePresistent values in C++11 mode?

Cheers, Pavel

Jochen Eisinger

unread,
Jul 14, 2014, 11:09:16 AM7/14/14
to v8-u...@googlegroups.com
Not all platforms we're targeting support C++11 yet :(

As a work around, we provide map and vector implementations in v8-util.h that can be used with persistent values.

best
-jochen


--
--
v8-users mailing list
v8-u...@googlegroups.com
http://groups.google.com/group/v8-users
---
You received this message because you are subscribed to the Google Groups "v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to v8-users+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Pavel Medvedev

unread,
Jul 14, 2014, 11:39:24 AM7/14/14
to v8-u...@googlegroups.com
Thank you Jochen,

Yes, I see PersistentValueMap and PersistentValueVector class templates there.

Unfortunately,  they have a couple of minor issues:

1. PersistentValueVector has no public function to get a v8::Isolate instance used for values in the vector.
2. It's not possible to enumerate all items stored in a PersistentValueMap, since its interface has no any kind of iterators.

Thus, I decided to use standard containers :) Move-only persistents work fine in standard containers even with pretty old compilers: Visual C++2010 and GCC 4.6.3
Reply all
Reply to author
Forward
0 new messages