Generic Friendship Policies

E

Erik

private and protected members can only be accessed by deriving a class
or by giving friendship. The problem with friendship is that friend
statements are written inside the class. A FriendShip Policy solves the
problem.
There are different proposals how to solve the problem: The first idea
is to provide the class with a macro
---------------------------------------------------------------------
#define FRIENDS_OF_FOO class A; class B; class C

class foo
{
friends FRIENDS_OF_FOO;
private:
int pfoo;
};

The classes A,B and C can now access the private member pfoo;
-----------------------------------------------------------------------------------------------------------------------------
Because I don't like macros I was looking for an generic
implementation. My first attempt looks like that:

template <class T>
class foo
{
friends T;
private: int pfoo;
};

class A
{
foo<A> myFoo;
};

class B
{
foo<B> myFoo;
}

class D
{
A a;
B b;
}

The solution above has several disadvantage:
- The compiler generates a class for each different template parameter
- class A,B cannot share class foo. because there are different types
of foo.
- To provide the class foo with more template parameter seemed not to
be an adequat way
- This is not legal C++ (some compilers refuse that code)
-----------------------------------------------------------------------------------------------------------------------------
The attemped is to use TYPELSIST from the Loki Library (Reference:
Modern C++ Design by Andrei Alexandrescu) hopefully removes the
restriction on friendship.
The policy is provided with a macro and a Typlist as template parameter

//header of friendship.hpp
#include "Loki/TypeList.h"
#include "Loki/EmptyType.h"

#define MAKE_FRIENDS(x= x::parm1 ; friend x::parm2; friend x::parm3;
..... friend x::parm51

template <class TList=Loki::EmptyType> class FriendShip
{
public:
typedef typename TList::parmList;
typedef typename Loki::TL::TypeAtNonStrict<TList, 0,
Loki::EmptyType>::Result Parm1;
typedef typename Loki::TL::TypeAtNonStrict<TList, 2,
Loki::EmptyType>::Result Parm2;
...
typedef typename Loki::TL::TypeAtNonStrict<TList, 50,
Loki::EmptyType>::Result Parm51;
};

//end of header friendship.hpp
-----------------------------------------------------------------------------------------------------------------------------
How can the policy be used? First we have to modify class foo:

template <class Friends_Of_Foo>
class foo
{
friends MAKE_FRIENDS(Friends_Of_Foo);
private:
int pfoo;
};

See below how foo can be used:

typedef FriendShip<TYPELIST_3(A,B,C)> friends_of_foo;

class A
{
foo<friends_of_foo> Foo;
};

class B
{
foo<friends_of_foo> Foo;
};

class D
{
A a;
B b;
};

Now the class foo is adaptable and can be generically used.
I used friendship policies to encapsulate functors in dll-Libraries.
I'm not sure, if this is legal C++ code. The compilers I used accept
this code.
-----------------------------------------------------------------------------------------------------------------------------
 
A

Alf P. Steinbach

* Erik:
#define FRIENDS_OF_FOO class A; class B; class C

class foo
{
friends FRIENDS_OF_FOO;
private:
int pfoo;
};

C++ has no keyword 'friends'.

I'm not sure, if this is legal C++ code. The compilers I used accept
this code.

It's not syntactically correct C++.
 
E

Erik

Sorry, this was a spelling error. You have to write friend instead of
friends.
This is only an extract I summarised, not the original code.
 
A

Alf P. Steinbach

* Erik:
Sorry, this was a spelling error. You have to write friend instead of
friends.
This is only an extract I summarised, not the original code.

It won't work with 'friend' either; the syntax for a 'friend'
declaration doesn't support multiple classes in one declaration.

What does that tell you?
 
E

Erik

Ok, you are right. I'm too sloppy. Maybe I should show the original
code for the template class.

// start of header
//-----------------------------------------------------------------------------------------------------------------------------------------------------------


#ifndef FriendShip_17_10_2004_HPP
#define FriendShip_17_10_2004_HPP

#include <maya/defines.hpp>
#include <loki/TypeList.h>
#include <loki/EmptyType.h>

/*------------------------------------------------------------------------------
@history
<TABLE>
\Author Date Vers. Comment
------------ -------- --------
--------------------------------------------
E. Grießmann 17.10.04 1.00 R01 created
</TABLE>
*-----------------------------------------------------------------------------*/



#define MAKE_FRIENDS(x) x::parm1 ; friend x::parm2 ; friend x::parm3 ;
friend x::parm4 ; friend x::parm5 ; \
friend x::parm6 ; friend x::parm7 ; friend
x::parm8 ; friend x::parm9 ; friend x::parm10; \
friend x::parm11; friend x::parm12; friend
x::parm13; friend x::parm14; friend x::parm15; \
friend x::parm16; friend x::parm17; friend
x::parm18; friend x::parm19; friend x::parm20; \
friend x::parm21; friend x::parm22; friend
x::parm23; friend x::parm24; friend x::parm25; \
friend x::parm26; friend x::parm27; friend
x::parm28; friend x::parm29; friend x::parm30; \
friend x::parm31; friend x::parm32; friend
x::parm33; friend x::parm34; friend x::parm35; \
friend x::parm36; friend x::parm37; friend
x::parm38; friend x::parm39; friend x::parm40; \
friend x::parm41; friend x::parm42; friend
x::parm43; friend x::parm44; friend x::parm45; \
friend x::parm46; friend x::parm47; friend
x::parm48; friend x::parm49; friend x::parm50; \
friend x::parm51


BEGIN_OF_NAMESPACE_MAYA

template <class TList> class FriendShip;

namespace friend_ship {
typedef maya::FriendShip<TYPELIST_1(Loki::EmptyType)> none;
}



template <class T = Loki::EmptyType>
struct Append
{
typedef typename T SingleFriend;
};


template <class TList = friend_ship::none>
class FriendShip
{
public:
typedef typename TList ParmList;
typedef typename Loki::TL::TypeAtNonStrict<TList, 0,
Loki::EmptyType>::Result Parm1;
typedef typename Loki::TL::TypeAtNonStrict<TList, 1,
Loki::EmptyType>::Result Parm2;
typedef typename Loki::TL::TypeAtNonStrict<TList, 2,
Loki::EmptyType>::Result Parm3;
typedef typename Loki::TL::TypeAtNonStrict<TList, 3,
Loki::EmptyType>::Result Parm4;
typedef typename Loki::TL::TypeAtNonStrict<TList, 4 ,
Loki::EmptyType>::Result Parm5;
typedef typename Loki::TL::TypeAtNonStrict<TList, 5 ,
Loki::EmptyType>::Result Parm6 ;
typedef typename Loki::TL::TypeAtNonStrict<TList, 6 ,
Loki::EmptyType>::Result Parm7 ;
typedef typename Loki::TL::TypeAtNonStrict<TList, 7 ,
Loki::EmptyType>::Result Parm8 ;
typedef typename Loki::TL::TypeAtNonStrict<TList, 8 ,
Loki::EmptyType>::Result Parm9 ;
typedef typename Loki::TL::TypeAtNonStrict<TList, 9 ,
Loki::EmptyType>::Result Parm10;
typedef typename Loki::TL::TypeAtNonStrict<TList, 10,
Loki::EmptyType>::Result Parm11;
typedef typename Loki::TL::TypeAtNonStrict<TList, 11,
Loki::EmptyType>::Result Parm12;
typedef typename Loki::TL::TypeAtNonStrict<TList, 12,
Loki::EmptyType>::Result Parm13;
typedef typename Loki::TL::TypeAtNonStrict<TList, 13,
Loki::EmptyType>::Result Parm14;
typedef typename Loki::TL::TypeAtNonStrict<TList, 14,
Loki::EmptyType>::Result Parm15;
typedef typename Loki::TL::TypeAtNonStrict<TList, 15,
Loki::EmptyType>::Result Parm16;
typedef typename Loki::TL::TypeAtNonStrict<TList, 16,
Loki::EmptyType>::Result Parm17;
typedef typename Loki::TL::TypeAtNonStrict<TList, 17,
Loki::EmptyType>::Result Parm18;
typedef typename Loki::TL::TypeAtNonStrict<TList, 18,
Loki::EmptyType>::Result Parm19;
typedef typename Loki::TL::TypeAtNonStrict<TList, 19,
Loki::EmptyType>::Result Parm20;
typedef typename Loki::TL::TypeAtNonStrict<TList, 20,
Loki::EmptyType>::Result Parm21;
typedef typename Loki::TL::TypeAtNonStrict<TList, 21
,Loki::EmptyType>::Result Parm22;
typedef typename Loki::TL::TypeAtNonStrict<TList, 22
,Loki::EmptyType>::Result Parm23;
typedef typename Loki::TL::TypeAtNonStrict<TList, 23
,Loki::EmptyType>::Result Parm24;
typedef typename Loki::TL::TypeAtNonStrict<TList, 24
,Loki::EmptyType>::Result Parm25;
typedef typename Loki::TL::TypeAtNonStrict<TList, 25
,Loki::EmptyType>::Result Parm26;
typedef typename Loki::TL::TypeAtNonStrict<TList, 26
,Loki::EmptyType>::Result Parm27;
typedef typename Loki::TL::TypeAtNonStrict<TList, 27,
Loki::EmptyType>::Result Parm28;
typedef typename Loki::TL::TypeAtNonStrict<TList, 28,
Loki::EmptyType>::Result Parm29;
typedef typename Loki::TL::TypeAtNonStrict<TList, 29,
Loki::EmptyType>::Result Parm30;
typedef typename Loki::TL::TypeAtNonStrict<TList, 30,
Loki::EmptyType>::Result Parm31;
typedef typename Loki::TL::TypeAtNonStrict<TList, 31,
Loki::EmptyType>::Result Parm32;
typedef typename Loki::TL::TypeAtNonStrict<TList, 32,
Loki::EmptyType>::Result Parm33;
typedef typename Loki::TL::TypeAtNonStrict<TList, 33,
Loki::EmptyType>::Result Parm34;
typedef typename Loki::TL::TypeAtNonStrict<TList, 34,
Loki::EmptyType>::Result Parm35;
typedef typename Loki::TL::TypeAtNonStrict<TList, 35,
Loki::EmptyType>::Result Parm36;
typedef typename Loki::TL::TypeAtNonStrict<TList, 36,
Loki::EmptyType>::Result Parm37;
typedef typename Loki::TL::TypeAtNonStrict<TList, 37,
Loki::EmptyType>::Result Parm38;
typedef typename Loki::TL::TypeAtNonStrict<TList, 38,
Loki::EmptyType>::Result Parm39;
typedef typename Loki::TL::TypeAtNonStrict<TList, 39,
Loki::EmptyType>::Result Parm40;
typedef typename Loki::TL::TypeAtNonStrict<TList, 40,
Loki::EmptyType>::Result Parm41;
typedef typename Loki::TL::TypeAtNonStrict<TList, 41,
Loki::EmptyType>::Result Parm42;
typedef typename Loki::TL::TypeAtNonStrict<TList, 42
,Loki::EmptyType>::Result Parm43;
typedef typename Loki::TL::TypeAtNonStrict<TList, 43
,Loki::EmptyType>::Result Parm44;
typedef typename Loki::TL::TypeAtNonStrict<TList, 44
,Loki::EmptyType>::Result Parm45;
typedef typename Loki::TL::TypeAtNonStrict<TList, 45
,Loki::EmptyType>::Result Parm46;
typedef typename Loki::TL::TypeAtNonStrict<TList, 46
,Loki::EmptyType>::Result Parm47;
typedef typename Loki::TL::TypeAtNonStrict<TList, 47
,Loki::EmptyType>::Result Parm48;
typedef typename Loki::TL::TypeAtNonStrict<TList, 48,
Loki::EmptyType>::Result Parm49;
typedef typename Loki::TL::TypeAtNonStrict<TList, 49,
Loki::EmptyType>::Result Parm50;
typedef typename Loki::TL::TypeAtNonStrict<TList, 50,
Loki::EmptyType>::Result Parm51;
};


END_OF_NAMESPACE_MAYA

#endif
// end of header
//-----------------------------------------------------------------------------------------------------------------------------------------------------------
 
P

Peter Julian

Erik said:
private and protected members can only be accessed by deriving a class
or by giving friendship. The problem with friendship is that friend
statements are written inside the class. A FriendShip Policy solves the
problem.

I disagree, private and protected members can be accessed by providing
accessors. The problem with friendship is that it usually, but not always,
depicts a misdesigned interface (except in the case of operators or tightly
coupled classes). I can understand the use of friend with a deeply complex
and intertwined inheritence hierarchy, like STL's iostream. What i can't
fathom is the ideology that friend solves all issues. In my humble opinion,
the best policy is to only use friend as a last resort.
 
B

BigBrian

The problem with friendship is that it usually depicts a misdesigned interface...

This is your opinion, with which I disagree. Public accessors which
return a reference to private and protected members are not much better
than having public member data. With a friend you can specify exactly
which classes have access to the data, instead of giving access to
EVERYBODY, either through an accessor or the data itself.
 
E

Erik

To use friends or not is a design decission, not a question if I like
it or not.
My design decision was to grant friendship.
I used the friendship policy for a special purpose.
1. Performance: Access private members is faster than to use a get or
set method (accessors)
2. Composition: I want to use pointer of class members in a wrapper
class dedicated for the user of the library. If I have a public set or
get function, the library user can indirectly access the private class
members. This was not my intention. Access given
by friendship are implementation details. (I know that you can also use
private inheritance)
3. Usability: Public Inheritance result in wide interface (A class with
a lot of methods). Besides, public inheritance is only useful if you
have a is-relationship. I also considered
private inheritance, but this proposal also result in restrictions I
don't want to have.
4. I also have the posibility to make constructors private. Thus I can
prevent the library
user to create an instance of a class member.

What I want to have is a class like this.

class Person
{
private:
//....
public:
class CName * name;
class CAddress * address;
class CWork * work;
class CData * data;
class CAnamnesis * anamnesis;
......
};

instead of such a class which is more difficult to use, to reuse and to
maintain.

class Person
{
private:
//....
public:
const std::string & get_last_name();
void set_last_name(const std::string &);
const std::string & get_first_name();
void set_first_name(const std::string &);
const std::string & get_maiden_name();
void set_maiden_name(const std::string &);
const std::string & get_address_street();
void set_address_street(const std::string &);
......
};

My initial question was, if the template class I propose is leagal C++.
I know that
friendship has also disadvantages, which must be considered.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top