Home » U++ Library support » U++ Core » NEW: generic Toupel grouper
|
|
|
|
|
|
Re: NEW: generic Toupel grouper [message #29247 is a reply to message #29243] |
Wed, 13 October 2010 09:32 |
mdelfede
Messages: 1307 Registered: September 2007
|
Ultimate Contributor |
|
|
luzr wrote on Wed, 13 October 2010 09:07 | I am afraid that this is not as easy to make Touple moveable, because that would imply all elements are moveable too, which is not always the case....
It is a little bit troublesome situation. I have no problem requiring all elements to be moveable, however right now I do not see how to add check without breaking POD capability... (the fine method to add check is in destructor, but that would make Touple non-pod).
|
I was afraid about that
Anyways, I think that most Tuple usage is, as you said, to spare a struct definition, and usually with moveable content.
A nice way could be some way to make Tuple moveable on request, but I don't know if that can be implemented (and MoveableTuple would be horrible, of course )
Max
|
|
|
|
Re: NEW: generic Toupel grouper [message #29253 is a reply to message #29248] |
Wed, 13 October 2010 11:50 |
|
kohait00
Messages: 939 Registered: July 2009 Location: Germany
|
Experienced Contributor |
|
|
found sth to work..
NAMESPACE_UPP
//this would be best, but
//compile ERROR, preprocessor does misunderstand ','
#if 1
NTL_MOVEABLE(Tuple2<Point, Size>);
#endif
//compile ERROR anyway
#if 0
NTL_MOVEABLE((Tuple2<Point, Size>));
#endif
//this DOES compile
#if 0
typedef Tuple2<Point, Size> BLAA;
NTL_MOVEABLE(BLAA);
#endif
//BOTH compile (got MSC9 and TDMGCC)
#if 0
inline void AssertMoveable0(Tuple2<Point, Size> *) {}
#elif 0
template<> inline void AssertMoveable<Tuple2<Point, Size> >(Tuple2<Point, Size> *) {};
#endif
END_UPP_NAMESPACE
....
//cpp
Vector<Tuple2<Point, Size> > vv;
Tuple2<Point, Size>& tps = vv.Add();
tps.a.x = 100;
tps.b.cx = 100;
|
|
|
Re: NEW: generic Toupel grouper [message #29255 is a reply to message #29253] |
Wed, 13 October 2010 12:24 |
|
kohait00
Messages: 939 Registered: July 2009 Location: Germany
|
Experienced Contributor |
|
|
i think the typedef is an easy way, so we can use NTL_MOVEABLE
here a short idea.. if anyone knows how to specify the salt for the name in a better way, welcome..
with this,
//V1
#if 0
#define TUPLE2_MOVEABLE(T1, T2) \
typedef Tuple2<T1,T2> COMBINE3(TD_,T1,T2); \
NTL_MOVEABLE(COMBINE3(TD_,T1,T2)); \
#endif
//V2
#if 1
#define TUPLE2_MOVEABLE(T1, T2, salt) \
typedef Tuple2<T1,T2> TD_salt; \
NTL_MOVEABLE(TD_salt); \
#endif
//V1
#if 0
TUPLE2_MOVEABLE(Point, Size);
#endif
//V2
#if 1
TUPLE2_MOVEABLE(Point, Size, 123);
#endif
//this enables Vector<Tuple2<Point, Size> > to be used
|
|
|
|
Re: NEW: generic Toupel grouper [message #29264 is a reply to message #29255] |
Wed, 13 October 2010 18:48 |
|
mirek
Messages: 13975 Registered: November 2005
|
Ultimate Member |
|
|
kohait00 wrote on Wed, 13 October 2010 06:24 | i think the typedef is an easy way, so we can use NTL_MOVEABLE
here a short idea.. if anyone knows how to specify the salt for the name in a better way, welcome..
with this,
//V1
#if 0
#define TUPLE2_MOVEABLE(T1, T2) \
typedef Tuple2<T1,T2> COMBINE3(TD_,T1,T2); \
NTL_MOVEABLE(COMBINE3(TD_,T1,T2)); \
#endif
//V2
#if 1
#define TUPLE2_MOVEABLE(T1, T2, salt) \
typedef Tuple2<T1,T2> TD_salt; \
NTL_MOVEABLE(TD_salt); \
#endif
//V1
#if 0
TUPLE2_MOVEABLE(Point, Size);
#endif
//V2
#if 1
TUPLE2_MOVEABLE(Point, Size, 123);
#endif
//this enables Vector<Tuple2<Point, Size> > to be used
|
Too verbose, we need something more sexy...
|
|
|
|
Re: NEW: generic Toupel grouper [message #29284 is a reply to message #29264] |
Wed, 13 October 2010 22:35 |
|
luzr wrote on Wed, 13 October 2010 18:48 | Too verbose, we need something more sexy...
|
There is a way... a sexy way What would you say to this:template <typename A, typename B>
struct Tuple2 : PossiblyMoveable<Tuple2<A,B>,A,B>{
...
} Is that sexy enough? The idea is that PossiblyMoveable<T,A,B,...> evaluates to Moveable<T> if A,B,... are all moveable and to EmptyClass otherwise.
I can't give you the code right now, but I have it implemented (it needs only some cosmetic changes) and I will post it here tonight or tomorrow. Word of warning in advance: It is heavy "template metaprogramming" (Just found that term on wiki, but it fits )
kohait00 wrote on Wed, 13 October 2010 22:03 | in any case, the user needs to take care of that all by himself. it's just to make the process to do so a bit easier.
|
As you could already guess from above, what you say is not true It can be done automatically, by abusing the compiler/language just a bit
Honza
[Updated on: Wed, 13 October 2010 22:39] Report message to a moderator
|
|
|
Re: NEW: generic Toupel grouper [message #29287 is a reply to message #29284] |
Thu, 14 October 2010 02:14 |
|
Hi,
As I promised, here is the code I mentioned in my previous post...
Disclaimer: I am aware that the implementation is verbose, bulky, tricky, possibly deceiving and abuses compiler to do things it was never meant to do. Yet I am very happy to see the result - simple template that is able to determine moveability of it's parameters.
Majority of the ugly code is in Topt.h. Since U++ uses two different way how to mark a type moveable (Moveable<> and NTL_MOVEABLE), the code has two ugly parts, one checking each method and ORing both together. Also I had to add some more code to NTL_MOVEABLE macro to be able to determine the moveability correctly. In the Tuple.h, the only change is the PossiblyMoveable<> base for each size of Tuple.
PossiblyMoveable might be useful in some other context too. I just can't think of any right now. Also IsMoveable<T> and IsBase<Base,Derived> (can determine if Base is really base class of Derived) could be helpful for someone, but they would need to be little more general for common user to use...
Tell me what you think about this, but don't tell me I'm crazy I'm actually quite realistic and don't expect this to get into U++. I wrote it mainly for fun of discovering new tricks and pushing the limits
Best regards,
Honza
-
Attachment: Topt.h
(Size: 19.66KB, Downloaded 165 times)
-
Attachment: Tuple.h
(Size: 3.19KB, Downloaded 271 times)
|
|
|
|
|
Re: NEW: generic Toupel grouper [message #29339 is a reply to message #29322] |
Fri, 15 October 2010 16:24 |
|
luzr wrote on Fri, 15 October 2010 13:46 | Why we like POD:
static Tuple2<int, const char *> data[] = {
{ 1, "Kvuli" },
{ 2, "Tomuhle" },
};
|
Point taken... I'll have to read bit more about how to keep a type POD.
|
|
|
|
|
Goto Forum:
Current Time: Wed May 08 05:27:25 CEST 2024
Total time taken to generate the page: 0.02254 seconds
|