Commit 6a8be0a1 authored by Chris Toshok's avatar Chris Toshok

add a variadic template ctor that delegates to T's constructor (making things...

add a variadic template ctor that delegates to T's constructor (making things potentially a bit more compact for constructing rooted boxes)
parent 1a21d2dd
...@@ -24,8 +24,9 @@ namespace pyston { ...@@ -24,8 +24,9 @@ namespace pyston {
template <class T> class StackRoot { template <class T> class StackRoot {
public: public:
StackRoot(T* t) : t(t) {} explicit StackRoot(T* t) : t(t) {}
StackRoot(const StackRoot& other) : t(other.t) {} StackRoot(const StackRoot& other) : t(other.t) {}
template <class... Args> StackRoot(Args&&... args) : t(new T(std::forward(args...))) {}
~StackRoot() { GC_KEEP_ALIVE(t); } ~StackRoot() { GC_KEEP_ALIVE(t); }
T& operator*() const { return *t; } T& operator*() const { return *t; }
...@@ -44,13 +45,14 @@ typedef StackRoot<Box> RootedBox; ...@@ -44,13 +45,14 @@ typedef StackRoot<Box> RootedBox;
typedef StackRoot<BoxedString> RootedBoxedString; typedef StackRoot<BoxedString> RootedBoxedString;
// //
// the above types can be used whenever we need to explicitly root a Box subclass within some lexical scope. // the above types can be used whenever we want to explicitly root a Box subclass within some lexical scope.
// //
// { // {
// RootedBoxedString sub(new BoxedString("hello world")); // RootedBoxedString sub("hello world");
// for (auto c : sub->s) { // for (auto c : sub->s) {
// doSomethingThatCouldTriggerACollection(); // doSomethingThatCouldTriggerACollection();
// } // }
// callWithString(sub); // pass RootedBoxedString to a function taking BoxedString*
// // sub will be rooted conservatively until here // // sub will be rooted conservatively until here
// } // }
// //
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment