# Boost :

From: Simonson, Lucanus J (lucanus.j.simonson_at_[hidden])
Date: 2007-10-10 18:30:16

Fernando wrote:
>What would I need to do in GTL?
>And, MORE importantly, what does GTL do, exactly, to adapt my "p" objet
>there?

Just typing off the top of my head. I am not yet allowed to copy and
paste the code I wrote which Intel owns.

typedef int Unit;

struct MyPoint {int x, int y};

template <>
class PointConceptMap<MyPoint> {
public:
static inline Unit PointGet(const MyPoint& t, Orientation2D
orient) {
return orient == HORIZONTAL ? t.x: t.y; }

static inline void PointSet(MyPoint& t, Orientation2D orient,
Unit value) {
if(orient == HORIZONTAL) t.x = value; else t.y = value; }

static inline MyPoint PointConstruct(Unit x, Unit y) {
MyPoint tmp; tmp.x = x; tmp.y = y; return tmp; }
private:
PointConceptMap(); //construction disallowed
}

template <class T>
class PointConcept : public T {
public:
static inline PointConcept& mimic(T& t) {
return static_cast<PointConcept&>(t); }
static inline const PointConcept& mimicConst(T& t) {
return static_cast<const PointConcept&>(t); }
inline T& yield() { return *this; }
inline const T& yieldConst() { return *this; }

inline PointConcept() {}

template <class T2>
inline PointConcept& operator=(PointConcept<T2>& that) {
set(HORIZONTAL, that.get(HORIZONTAL);
return set(VERTICAL, that.get(VERTICAL); }

Unit get(Orientation2D orient) const { return get_(orient); }
PointConcept& set(Orientation2D orient, Unit value) {
set_(orient, value); return *this; }

private:
inline Unit get_(Orientation2D orient) const {
return PointConceptMap<T>::PointGet(yieldConst(),
orient); }
inline void set_(Orientation2D orient, Unit value) {
PointConceptMap<T>::PointSet(yield(), orient, value); }
static inline T construct_(Unit x, Unit y) {
return PointConceptMap<T>::PointConstruct(x, y); }
};

template <class T>
void foo(T& t) {
PointConcept<T>& tref = PointConcept<T>::mimic(t);
tref.set(HORIZONTAL, tref.get(VERTICAL);
}

That is the complete design pattern. It probably has syntax errors
because I was typing off the top of my head, but you should get the idea
quite clearly.

Luke