Best way to create true wrapper?

J

Jan Lellmann

Hello,

I'm currently trying to interface an existing (non-modifiable) class to
other code and wondering what's the best way to create the wrappers. Let's
say I have the following code:

----
class Vec {
public:
virtual double& el(int i) = 0;
virtual double el(int i) const = 0;
};

class ThirdpartyVec; // does not inherit from Vec and cannot be changed

void constfunc(const Vec& v) {
cout << "constfunc: " << v.el(0) << endl;
}

void nonconstfunc(Vec& v) {
v.el(0) = 1;
}

class Wrapper : public Vec {
private:
const ThirdpartyVector& inner;
public:
Wrapper(const ThirdpartyVector& _inner) : inner(_inner) {};
virtual double& el(int i) { return ThirdPartyGetRef(inner,i); };
virtual double el(int i) const { return ThirdPartyGetValue(inner,i); };
}
----

Now ideally I would like the user to be able to write

ThirdpartyVector tpv;
constfunc(tpv);
nonconstfunc(tpv);

and the wrapper objects should be created accordingly. As an alternative, I
could imagine providing a wrap() function, so the code could read

ThirdpartyVector tpv;
constfunc(wrap(tpv));
nonconstfunc(wrap(tpv));

However the above approaches cannot work for the call to nonconstfunc, as
C++ does not allow to cast the temporary to a nonconst reference.

Do you know of any good approach to this problem?

Regards,
Jan
 
P

Pavel

Jan said:
Hello,

I'm currently trying to interface an existing (non-modifiable) class to
other code and wondering what's the best way to create the wrappers.
Let's say I have the following code:

----
class Vec {
public:
virtual double& el(int i) = 0;
virtual double el(int i) const = 0;
};

class ThirdpartyVec; // does not inherit from Vec and cannot be changed

void constfunc(const Vec& v) {
cout << "constfunc: " << v.el(0) << endl;
}

void nonconstfunc(Vec& v) {
v.el(0) = 1;
}

class Wrapper : public Vec {
private:
const ThirdpartyVector& inner;
public:
Wrapper(const ThirdpartyVector& _inner) : inner(_inner) {};
virtual double& el(int i) { return ThirdPartyGetRef(inner,i); };
virtual double el(int i) const { return ThirdPartyGetValue(inner,i); };
}
----

Now ideally I would like the user to be able to write

ThirdpartyVector tpv;
constfunc(tpv);
nonconstfunc(tpv);

and the wrapper objects should be created accordingly. As an
alternative, I could imagine providing a wrap() function, so the code
could read

ThirdpartyVector tpv;
constfunc(wrap(tpv));
nonconstfunc(wrap(tpv));

However the above approaches cannot work for the call to nonconstfunc,
as C++ does not allow to cast the temporary to a nonconst reference.

Do you know of any good approach to this problem?

Regards,
Jan
I guess I am confused about the purpose of the exercise. As your (ideal)
goal is to interface to the functions constfunc() and nonconstfunc and
you seem to be ok with their taking the "raw" ThirdpartyVector
parameter, why do you need a wrapper at all?

-Pavel
 
M

Michael Tsang

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jan said:
Hello,

I'm currently trying to interface an existing (non-modifiable) class to
other code and wondering what's the best way to create the wrappers. Let's
say I have the following code:
(skipped)

Now ideally I would like the user to be able to write

ThirdpartyVector tpv;
constfunc(tpv);
nonconstfunc(tpv);

and the wrapper objects should be created accordingly. As an alternative,
I could imagine providing a wrap() function, so the code could read

ThirdpartyVector tpv;
constfunc(wrap(tpv));
nonconstfunc(wrap(tpv));
You can write the wrap function to return an l-value reference of Wrapper.
However the above approaches cannot work for the call to nonconstfunc, as
C++ does not allow to cast the temporary to a nonconst reference.

Do you know of any good approach to this problem?

Regards,
Jan
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEARECAAYFAkr31ZMACgkQG6NzcAXitM9lmwCglQQBDd+xbjx33kiNn3FPvTGo
7IQAnjE5nzXLCbzddsv5b1p13WOJtmzw
=qvcy
-----END PGP SIGNATURE-----
 
J

Jan Lellmann

I guess I am confused about the purpose of the exercise. As your (ideal)
goal is to interface to the functions constfunc() and nonconstfunc and
you seem to be ok with their taking the "raw" ThirdpartyVector
parameter, why do you need a wrapper at all?

I guess I wasn't 100% clear. The constfunc and nonconstfunc are fixed to
take a "const Vec&" / "Vec&" (which is not a base class of ThirdpartyVec, so
we cannot just pass ThirdpartyVec). I was wondering if there was a way to
convert this without much hassle for the user to a Wrapper, so the optimum
(which I'm pretty sure is not possible in C++) would be that the compiler sees

nonconstfunc(tpv);

and somehow figures out there is an implicit conversion from
ThirdpartyVector to Wrapper and cast that to Vec. I do not think this is
possible, that's why I put the alternate form

nonconstfunc(wrap(tpv));

Best Regards,
Jan
 
J

Jan Lellmann

ThirdpartyVector tpv;
You can write the wrap function to return an l-value reference of Wrapper.

Thanks, I tried this approach and did not get it to work with the
nonconstfunc, as the compiler will not convert the temporary Wrapper to a
nonconst reference. Any ideas?

Regards,
Jan
 
B

Bart van Ingen Schenau

Thanks, I tried this approach and did not get it to work with the
nonconstfunc, as the compiler will not convert the temporary Wrapper to a
nonconst reference. Any ideas?

Regards,
   Jan

You can create a set of overloaded functions, like this:

void constfunc(const ThirdPartyVector& tpv) {
const Wrapper wrap(tpv);
constfunc(wrap);
}

void nonconstfunc(ThirdPartyVector& tpv) {
Wrapper wrap(tpv);
nonconstfunc(wrap);
}

Bart v Ingen Schenau
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top