PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > can I have trait based friendship for a class
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
can I have trait based friendship for a class

Réponse
 
LinkBack Outils de la discussion
Vieux 27/02/2008, 06h39   #1
abir
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut can I have trait based friendship for a class

Hi, I have a class which once created , can not be changed. However as
it has a few states, which need to be set with a few function call, I
thought to activate(enable) those functions only a certain trait is
present in the caller class.

eg,
class Stroke{
private:
typedef std::pair<uint32_t,uint32_t> PointPair;
PointPair points_;
StrokeType type_; ///< StrokeType is an enum;
Dir dir_; //< another enum
std::size_t drawingID_ ; //some stroke id
...
public:
Stroke(Drawing& d,PointPair& points, std::size_t id);
void setDir(Dir d);
};

now my constructor sets a few fields, when the class is created.
The remaining things i want to set only using some classes, which has
certain traits.
Like I want a class which has a trait can_set_dir, has setDir enabled
(using boost::enable_if or equivalent), But the class which doesn't
have the trait can't call the function (it will be removed from the
definition).
This is kind of selective friendship I want to achieve, without using
friend keyword which is intrusive in nature.

Is it possible to make this kind of selective function calling based
on trait ? ( I am free to modify signature of setDir for that purpose)

thanks
abir
  Réponse avec citation
Vieux 27/02/2008, 08h14   #2
Triple-DES
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: can I have trait based friendship for a class

On 27 Feb, 07:39, abir <abirba...@gmail.com> wrote:
> Hi, I have a class which once created , can not be changed. However as
> it has a few states, which need to be set with a few function call, I
> thought to activate(enable) those functions only a certain trait is
> present in the caller class.
>
> eg,
> class Stroke{
> private:
> typedef std::pair<uint32_t,uint32_t> PointPair;
> PointPair points_;
> StrokeType type_; ///< StrokeType is an enum;
> Dir dir_; //< another enum
> std::size_t drawingID_ ; //some stroke id
> ...
> public:
> Stroke(Drawing& d,PointPair& points, std::size_t id);
> void setDir(Dir d);
>
> };
>
> now my constructor sets a few fields, when the class is created.
> The remaining things i want to set only using some classes, which has
> certain traits.
> Like I want a class which has a trait can_set_dir, has setDir enabled
> (using boost::enable_if or equivalent), But the class which doesn't
> have the trait can't call the function (it will be removed from the
> definition).
> This is kind of selective friendship I want to achieve, without using
> friend keyword which is intrusive in nature.
>
> Is it possible to make this kind of selective function calling based
> on trait ? ( I am free to modify signature of setDir for that purpose)
>


It isn't possible with 'natural' syntax. You have to pass the caller
class to the function somehow. If this is acceptable:

template<typename Caller>
class Stroke
{
// ...
};

It would be possible to "disable" functions for a specific caller at
compile time.
  Réponse avec citation
Vieux 27/02/2008, 08h47   #3
abir
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: can I have trait based friendship for a class

On Feb 27, 1:14 pm, Triple-DES <DenPlettf...@gmail.com> wrote:
> On 27 Feb, 07:39, abir <abirba...@gmail.com> wrote:
>
>
>
> > Hi, I have a class which once created , can not be changed. However as
> > it has a few states, which need to be set with a few function call, I
> > thought to activate(enable) those functions only a certain trait is
> > present in the caller class.

>
> > eg,
> > class Stroke{
> > private:
> > typedef std::pair<uint32_t,uint32_t> PointPair;
> > PointPair points_;
> > StrokeType type_; ///< StrokeType is an enum;
> > Dir dir_; //< another enum
> > std::size_t drawingID_ ; //some stroke id
> > ...
> > public:
> > Stroke(Drawing& d,PointPair& points, std::size_t id);
> > void setDir(Dir d);

>
> > };

>
> > now my constructor sets a few fields, when the class is created.
> > The remaining things i want to set only using some classes, which has
> > certain traits.
> > Like I want a class which has a trait can_set_dir, has setDir enabled
> > (using boost::enable_if or equivalent), But the class which doesn't
> > have the trait can't call the function (it will be removed from the
> > definition).
> > This is kind of selective friendship I want to achieve, without using
> > friend keyword which is intrusive in nature.

>
> > Is it possible to make this kind of selective function calling based
> > on trait ? ( I am free to modify signature of setDir for that purpose)

>
> It isn't possible with 'natural' syntax. You have to pass the caller
> class to the function somehow. If this is acceptable:

It's true, i can pass the caller.
>
> template<typename Caller>
> class Stroke
> {
> // ...
>
> };
>
> It would be possible to "disable" functions for a specific caller at
> compile time.


I can't make Stroke class a template, as that will make many
incompatible Stroke object for each caller.
However what I am thinking is to have a function like
class Stroke{
public:
template<typename Caller>
setDir(Dir d, boost::enable_if_c<Caller::cal_change_dir>) ... kind of
thing.

thanks for reply..
abir
};
  Réponse avec citation
Vieux 27/02/2008, 09h28   #4
Triple-DES
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: can I have trait based friendship for a class

On 27 Feb, 09:47, abir <abirba...@gmail.com> wrote:
> On Feb 27, 1:14 pm, Triple-DES <DenPlettf...@gmail.com> wrote:
>
>
>
> > On 27 Feb, 07:39, abir <abirba...@gmail.com> wrote:

>
> > > Hi, I have a class which once created , can not be changed. However as
> > > it has a few states, which need to be set with a few function call, I
> > > thought to activate(enable) those functions only a certain trait is
> > > present in the caller class.

>
> > > eg,
> > > class Stroke{
> > > private:
> > > typedef std::pair<uint32_t,uint32_t> PointPair;
> > > PointPair points_;
> > > StrokeType type_; ///< StrokeType is an enum;
> > > Dir dir_; //< another enum
> > > std::size_t drawingID_ ; //some stroke id
> > > ...
> > > public:
> > > Stroke(Drawing& d,PointPair& points, std::size_t id);
> > > void setDir(Dir d);

>
> > > };

>
> > > now my constructor sets a few fields, when the class is created.
> > > The remaining things i want to set only using some classes, which has
> > > certain traits.
> > > Like I want a class which has a trait can_set_dir, has setDir enabled
> > > (using boost::enable_if or equivalent), But the class which doesn't
> > > have the trait can't call the function (it will be removed from the
> > > definition).
> > > This is kind of selective friendship I want to achieve, without using
> > > friend keyword which is intrusive in nature.

>
> > > Is it possible to make this kind of selective function calling based
> > > on trait ? ( I am free to modify signature of setDir for that purpose)

>
> > It isn't possible with 'natural' syntax. You have to pass the caller
> > class to the function somehow. If this is acceptable:

>
> It's true, i can pass the caller.
>
>
>
> > template<typename Caller>
> > class Stroke
> > {
> > // ...

>
> > };

>
> > It would be possible to "disable" functions for a specific caller at
> > compile time.

>
> I can't make Stroke class a template, as that will make many
> incompatible Stroke object for each caller.
> However what I am thinking is to have a function like
> class Stroke{
> public:
> template<typename Caller>
> setDir(Dir d, boost::enable_if_c<Caller::cal_change_dir>) ... kind of
> thing.
>
> thanks for reply..


I understand. This will work, but it may not be the most elegant
approach:

class Stroke
{
public:
typedef int Dir;
template<typename Caller>
void setDir(Dir d, const Caller* c)
{
// CT is the traits class
setDir< CT<Caller>::can_set_dir>(d);
}
private:
template<bool enabled>
void setDir(Dir d);
template<>
void setDir<true>(Dir d) { /* actual set dir code */ };
};

Caller code would look like this:

Stroke s;
s.setDir(42, this);

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h28.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13697 seconds with 12 queries