Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   C++ Design question (http://www.velocityreviews.com/forums/t459814-c-design-question.html)

denis 01-14-2007 09:42 AM

C++ Design question
 
Hi All,

I'm making a class which derives(implements) from 5 interfaces:

class myClass: public base1, public base2, public base3,.......

I want to be able to ask from any interface about a pointer to another
interface
What I mean is:

myClass _myImpObj;

base1 *b1 = &_myImpObj;
base *b2 = b1->QueryInterafce("b2");
base *b3 = b2->QueryInterafce("b3");
base *b1N = b3->QueryInterface("b1");

Its is very simialir to COM mechanism.Anyone knows how to implement it?

Thanks,
Denis


alon 01-14-2007 10:34 AM

Re: C++ Design question
 
void* myClass::QueryInterface(char* str)
{
if (!strcmp(str, "b1"))
return (base1*)this;
....
....
}

Is this what you meant?


denis wrote:
> Hi All,
>
> I'm making a class which derives(implements) from 5 interfaces:
>
> class myClass: public base1, public base2, public base3,.......
>
> I want to be able to ask from any interface about a pointer to another
> interface
> What I mean is:
>
> myClass _myImpObj;
>
> base1 *b1 = &_myImpObj;
> base *b2 = b1->QueryInterafce("b2");
> base *b3 = b2->QueryInterafce("b3");
> base *b1N = b3->QueryInterface("b1");
>
> Its is very simialir to COM mechanism.Anyone knows how to implement it?
>
> Thanks,
> Denis



kwikius 01-14-2007 10:48 AM

Re: C++ Design question
 

denis wrote:
> Hi All,
>
> I'm making a class which derives(implements) from 5 interfaces:
>
> class myClass: public base1, public base2, public base3,.......
>
> I want to be able to ask from any interface about a pointer to another
> interface
> What I mean is:
>
> myClass _myImpObj;
>
> base1 *b1 = &_myImpObj;
> base *b2 = b1->QueryInterafce("b2");
> base *b3 = b2->QueryInterafce("b3");
> base *b1N = b3->QueryInterface("b1");
>
> Its is very simialir to COM mechanism.Anyone knows how to implement it?


You can use dynamic_cast:

#include <iostream>

template <int N>
struct base{
virtual ~base(){}
};

struct my12 : base<1>, base<2>{};

struct my34 : base<3>, base<4>{};

struct my5 : base<5>{};

struct my12345 : base<1>,base<2>, my34 ,my5{};

template <int N, typename T>
bool has_interface(T * t)
{
bool is = dynamic_cast< base<N> * > (t) !=0;
std::cout << typeid(T).name() << " "
<< ((is)?"Is":"Is not")<< " a base<" << N << ">\n";
return is;
}

template <int N>
struct interfaces;

template <>
struct interfaces<1>{
template <typename T>
void operator()( T * t)const
{
has_interface<1>(t);
}
};

template <int N>
struct interfaces{
template <typename T>
void operator()( T * t)const
{
interfaces<N-1> f;
f(t);
has_interface<N>(t);
}
};

int main()
{
//test 5 interfaces
interfaces<5> test;

//test some expressions
my12 var12;
test(&var12);

std::cout <<"---------\n\n";

my34 var34;
test(&var34);

std::cout <<"---------\n\n";

my5 var5;
test(&var5);

std::cout <<"---------\n\n";

my12345 var12345;
test(&var12345);

}





regards
Andy Little


kwikius 01-14-2007 10:56 AM

Re: C++ Design question
 
kwikius wrote:

//forgot crosscast test:

/*
....
*/

> std::cout <<"---------\n\n";
>
> my12345 var12345;
> test(&var12345);



std::cout <<"---------\n\n";

//crosscast test
base<1>* bb = &var12345;

test(bb);

>
> }


regards
Andy Little


Jorgen Grahn 01-19-2007 11:46 AM

Re: C++ Design question
 
On 14 Jan 2007 01:42:35 -0800, denis <denis_browne@hotmail.com> wrote:
> Hi All,
>
> I'm making a class which derives(implements) from 5 interfaces:
>
> class myClass: public base1, public base2, public base3,.......
>
> I want to be able to ask from any interface about a pointer to another
> interface
> What I mean is:
>
> myClass _myImpObj;
>
> base1 *b1 = &_myImpObj;
> base *b2 = b1->QueryInterafce("b2");
> base *b3 = b2->QueryInterafce("b3");
> base *b1N = b3->QueryInterface("b1");
>
> Its is very simialir to COM mechanism.Anyone knows how to implement it?


Not knowing what you want to accomplish, this sounds like a really bad idea.
Both the code itself, and the wish to emulate COM.

/Jorgen

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.dyndns.org> R'lyeh wgah'nagl fhtagn!


All times are GMT. The time now is 06:58 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.