473,406 Members | 2,954 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Interfaces, restricting access

I am surprised this hasn't come up for me more in the past, but the
situation is:

I need to have an interface that is usable for all
I need to have an interface that is only usable for some

I do not really know of a good way to achieve this. If I use friend
functions, I can no longer make methods virtual, right?

Example:

I am making a texture class for my graphics library

The only thing the application should have access to is a string
filename to create it with, perhaps dimensions, and a few other
things.

However, some classes inside the engine, such as say the renderer
should have access to the hardware level texture resource, which is in
this case a pointer to a struct from a 3rd party library. So, I need
accessors for my renderer to use, but do not want them to be available
to the application. Both are using the same class.
Oct 19 '08 #1
4 1581
On Oct 18, 8:41 pm, Christopher <cp...@austin.rr.comwrote:
I am surprised this hasn't come up for me more in the past, but the
situation is:

I need to have an interface that is usable for all
I need to have an interface that is only usable for some

I do not really know of a good way to achieve this. If I use friend
functions, I can no longer make methods virtual, right?

Example:

I am making a texture class for my graphics library

The only thing the application should have access to is a string
filename to create it with, perhaps dimensions, and a few other
things.

However, some classes inside the engine, such as say the renderer
should have access to the hardware level texture resource, which is in
this case a pointer to a struct from a 3rd party library. So, I need
accessors for my renderer to use, but do not want them to be available
to the application. Both are using the same class.
Provide access(...) using interfaces only

We need a dummy Resource,
a core Texture type (string member and a pointer)
application needs an interface: IApplication
renderer needs another interface: IRenderer
type Texture overloads access(...) based on interface type.

#include <iostream>

struct Resource
{
};

struct IApplication
{
virtual void set(const std::string&) = 0;
};

struct IRenderer
{
virtual void set(Resource* p) = 0;
};

class Texture
{
const std::string m_s;
Resource* p_res;
public:
Texture(std::string s) : m_s(s), p_res(0) { }
void access(IApplication& ia) const
{
ia.set(m_s);
}
void access(IRenderer& ir) const
{
ir.set(p_res);
}
void setp(Resource* p)
{
p_res = p;
}
};

class Application : public IApplication
{
std::string m_s;
public:
void set(const std::string& r_s)
{
m_s = r_s;
}
void display() const
{
std::cout << m_s << std::endl;
}
};

class Renderer : public IRenderer
{
Resource* p_res;
public:
void set(Resource* p)
{
p_res = p;
}
void display() const
{
std::cout << p_res << std::endl;
}
};

int main()
{
// setup an environment
Resource res;
Texture tex("some string");
tex.setp( &res );

// application's access
Application app;
tex.access( app );
app.display();

// renderer's access
Renderer rend;
tex.access( rend );
rend.display();
}

/*
some string
0x7fff4c23687f
*/
Oct 19 '08 #2
On Oct 18, 10:40*pm, Salt_Peter <pj_h...@yahoo.comwrote:
On Oct 18, 8:41 pm, Christopher <cp...@austin.rr.comwrote:


I am surprised this hasn't come up for me more in the past, but the
situation is:
I need to have an interface that is usable for all
I need to have an interface that is only usable for some
I do not really know of a good way to achieve this. If I use friend
functions, I can no longer make methods virtual, right?
Example:
I am making a texture class for my graphics library
The only thing the application should have access to is a string
filename to create it with, perhaps dimensions, and a few other
things.
However, some classes inside the engine, such as say the renderer
should have access to the hardware level texture resource, which is in
this case a pointer to a struct from a 3rd party library. So, I need
accessors for my renderer to use, but do not want them to be available
to the application. Both are using the same class.

Provide access(...) using interfaces only

We need a dummy Resource,
a core Texture type (string member and a pointer)
application needs an interface: IApplication
renderer needs another interface: IRenderer
type Texture overloads access(...) based on interface type.

#include <iostream>

struct Resource
{

};

struct IApplication
{
* virtual void set(const std::string&) = 0;

};

struct IRenderer
{
* virtual void set(Resource* p) = 0;

};

class Texture
{
* const std::string m_s;
* Resource* p_res;
public:
* Texture(std::string s) : m_s(s), p_res(0) { }
* void access(IApplication& ia) const
* {
* * ia.set(m_s);
* }
* void access(IRenderer& ir) const
* {
* * ir.set(p_res);
* }
* void setp(Resource* p)
* {
* * p_res = p;
* }

};

class Application : public IApplication
{
* std::string m_s;
public:
* void set(const std::string& r_s)
* {
* * m_s = r_s;
* }
* void display() const
* {
* * std::cout << m_s << std::endl;
* }

};

class Renderer : public IRenderer
{
* Resource* p_res;
public:
* void set(Resource* p)
* {
* * p_res = p;
* }
* void display() const
* {
* * std::cout << p_res << std::endl;
* }

};

int main()
{
* // setup an environment
* Resource res;
* Texture tex("some string");
* tex.setp( &res );

* // application's access
* Application app;
* tex.access( app );
* app.display();

* // renderer's access
* Renderer rend;
* tex.access( rend );
* rend.display();

}

/*
some string
0x7fff4c23687f
*/- Hide quoted text -

- Show quoted text -

Not sure I follow.
You now have an Application class that is a Texture or at least has
methods and data specific to the texture.
Maybe I am just getting confused on names and made it too specific.
class A
{
public:

// Can be called by anyone
virtual void Method1();
virtual void Method2();
virtual void Method3();

// Can only be called by a class C
virtual void Method4();
virtual void Method5();

private:

// All 5 methods manipulate this data a differant way
int * m_data;
};

class B
{
public:
Foo()
{
A * a = new A();
a->Method1();

// Not allowed!
a->Method5();
}
};
class C
{
Bar()
{
A * a = new A();
a->Method1();

// Is OK
a->Method5();
}
};

Oct 19 '08 #3

"Christopher" <cp***@austin.rr.comwrote in message
news:d0**********************************@m36g2000 hse.googlegroups.com...
>I am surprised this hasn't come up for me more in the past, but the
situation is:

I need to have an interface that is usable for all
I need to have an interface that is only usable for some

I do not really know of a good way to achieve this. If I use friend
functions, I can no longer make methods virtual, right?

Example:

I am making a texture class for my graphics library

The only thing the application should have access to is a string
filename to create it with, perhaps dimensions, and a few other
things.

However, some classes inside the engine, such as say the renderer
should have access to the hardware level texture resource, which is in
this case a pointer to a struct from a 3rd party library. So, I need
accessors for my renderer to use, but do not want them to be available
to the application. Both are using the same class.

How about something like this below. I've created a class hierarchy based
upon an abstract interface for a "renderer" and
and "advanced" renderer subclass, and two rendering engines, an ordinary and
an advanced.
You create the "advanced" renderer and pass it to the two engines, the
ordinary engine is
expecting an ordinary renderer and so only uses methods of the ordinary
render, and the advanced
engine expects the Full Monty.

#include <iostream>

class IRenderer{

public:

virtual void render () = 0;

};

class IAdvancedRenderer : public IRenderer

{

public:

virtual void render () = 0;

virtual void renderAdvanced() = 0;

};

class RendererImpl : public IAdvancedRenderer

{

public:

RendererImpl(){}

virtual void render(){ std::cout << "Render" << std::endl;}

virtual void renderAdvanced(){ std::cout << "Advanced Render" <<
std::endl;}

};

class RenderingEngine

{

public:

RenderingEngine( IRenderer& renderer)

:_renderer(renderer)

{}

void render()

{

_renderer.render();

}

private:

IRenderer& _renderer;

};

class AdvancedRenderingEngine

{

public:

AdvancedRenderingEngine( IAdvancedRenderer& renderer)

:_renderer(renderer)

{}

void renderAdvanced()

{

_renderer.renderAdvanced();

}

void render()

{

_renderer.render();

}

private:

IAdvancedRenderer& _renderer;

};

IAdvancedRenderer* createRenderer()

{

return new RendererImpl();

}

int main(int argc, char *argv[])

{

RendererImpl renderer ;

AdvancedRenderingEngine advancedEngine( renderer);

RenderingEngine ordinaryEngine( renderer);

ordinaryEngine.render();

advancedEngine.renderAdvanced();

return 0;

}


Oct 19 '08 #4
On 2008-10-19 02:41, Christopher wrote:
I am surprised this hasn't come up for me more in the past, but the
situation is:

I need to have an interface that is usable for all
I need to have an interface that is only usable for some

I do not really know of a good way to achieve this. If I use friend
functions, I can no longer make methods virtual, right?
Not sure exactly what you mean by that, a friend function/class can call
a virtual function (as shown in this, somewhat contrived, example):

#include <iostream>

class PubInterface
{
public:
virtual void PubFunc() = 0;
};

class PrivInterface
{
virtual void PrivFunc() = 0;
friend void doIt(PubInterface&);
};

void doIt(PubInterface& i)
{
i.PubFunc();

PrivInterface* pi = dynamic_cast<PrivInterface*>(&i);
if (pi != 0)
pi->PrivFunc();
}

class Impl1 : public PubInterface
{
public:
void PubFunc()
{
std::cout << "PubFunc()\n";
}
};

class Impl2 : public PubInterface, public PrivInterface
{
void PrivFunc()
{
std::cout << "PrivFunc()\n";
}

public:
void PubFunc()
{
std::cout << "PubFunc()\n";
}
};
int main()
{
Impl1 i1;
Impl2 i2;

doIt(i1);
doIt(i2);

return 0;
}

--
Erik Wikström
Oct 19 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Xenophobe | last post by:
I have a popup window (required by the client) containing a form and would like to prevent users from accessing it directly. They are instead required to access the page via a hyperlink on another...
21
by: Franco Gustavo | last post by:
Hi, Please help me to understand this, because I don't see what I'm missing. I was reading a lot of examples on Internet that explain that C# doesn't implement multiple inheritance it...
4
by: Dennis C. Drumm | last post by:
Is there a way with C# to allow one class access to a method or field of another class, without making that method or field visible to all other classes, as would be the case when making the method...
0
by: WebMatrix | last post by:
Hello, What's the best way to keep email templates as html files on the server, so ASP.NET application can get file access to it, while restricting web users from accessing it through their...
10
by: KBTibbs | last post by:
Hi. I'm restricting access to my webpage with forms authentication, but I have some .htm files that I want to restrict as well (by default, ASP.NET does not restrict this, so anyone with access to...
8
by: sneddo | last post by:
Ok I am trying to do the above, I have got a script that will restrict the length but it requires the user to enter the field and hit a key, before it will work. This would normaly be find, but...
2
by: sant.tarun | last post by:
Hi, I am facing some some problem in restricting the access of a variable.... My question is described below..... Let I have two different C source files 'a.c' and 'b.c'. In the file 'a.c'...
2
by: runway27 | last post by:
i am using apache server and presently when i try accessing any folders of my website i am able to browse the files ex = www.website.com/images which is a serious security risk as i am building a...
7
by: shashi shekhar singh | last post by:
Respected Sir, I am really tired in solving of this issue that have been arises when i would like to restrict files to access only on my Test page , here i am retriving my files in iframe in Test...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.