I am trying to implement something similar to the Python with statement in C++. As I plan to use it mainly with Qt-OpenGL the methods are called bind
and release
(in Python __enter__
, __exit__
).
Code I came up with:
header:
#include <iostream>
#include <vector>
class With
{
public:
class A
{
public:
virtual ~A() { }
};
template <typename T>
class B : public A
{
public:
B(T& _t) : t(_t)
{
t.bind();
}
virtual ~B()
{
t.release();
}
T& t;
};
template <typename... Args>
With(Args&... args)
{
set(args...);
}
~With();
template <typename T, typename... Args>
void set(T& t, Args&... args)
{
set(t);
set(args...);
}
template <typename T>
void set(T& t)
{
a.push_back(dynamic_cast<A*>(new B<T>(t)));
}
std::vector<A*> a;
};
cpp:
With::~With()
{
for (auto it = a.begin(); it != a.end(); ++it)
{
delete *it;
}
}
Usage:
class X
{
public:
void bind() { std::cout << "bind x" << std::endl; }
void release() { std::cout << "release x" << std::endl; }
};
class Y
{
public:
void bind() { std::cout << "bind y" << std::endl; }
void release() { std::cout << "release y" << std::endl; }
};
int main()
{
X y;
Y y;
std::cout << "start" << std::endl;
{
With w(x, y);
std::cout << "with" << std::endl;
}
std::cout << "done" << std::endl;
return 0;
}
Questions:
- Needing
class A
andclass B
feels a bit clumsy. Is there a better alternative? - Are there any draw backs in using
&&
instead of&
? It would make the usage of tempory objects possible (e.g.With w(X(), y);
)
ScopeGuard
(ignore the code project link an look for the drdobbs article) alternatively useshared_ptr
with a custom deleter.... – Nobileunique_ptr
overshared_ptr
, though. – Serpentineshared_ptr
is much nicer than inunique_ptr
as in the former the deleter is effectively type-erased from the type, allowing the use of, for example, lambda expressions whose names are unutterable. – Nobiledecltype
for that, although it's not as clean. – Bistortauto
, but I don't think you can usedecltype
to solve the problem. – Nobileauto up(make_unique<...>([](...){...}));
. – Serpentineunique_ptr
: You can use an intermediate template andauto
. – Nobilemake_unique
is not part of the standard, and it is not trivial to design/implement it correctly (where would you pass the deleter?). As a matter of fact,std::make_shared
does not have support for passing a deleter either (in the case ofstd::make_shared
the problem is yet a tad more complex than it would be formake_unique
, as the latter does not need to manage the count object. – Nobile