473,785 Members | 2,811 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1612
On Oct 18, 8:41 pm, Christopher <cp...@austin.r r.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::st ring s) : m_s(s), p_res(0) { }
void access(IApplica tion& ia) const
{
ia.set(m_s);
}
void access(IRendere r& 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.r r.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::st ring s) : m_s(s), p_res(0) { }
* void access(IApplica tion& ia) const
* {
* * ia.set(m_s);
* }
* void access(IRendere r& 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

"Christophe r" <cp***@austin.r r.comwrote in message
news:d0******** *************** ***********@m36 g2000hse.google groups.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 IAdvancedRender er : public IRenderer

{

public:

virtual void render () = 0;

virtual void renderAdvanced( ) = 0;

};

class RendererImpl : public IAdvancedRender er

{

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(rend erer)

{}

void render()

{

_renderer.rende r();

}

private:

IRenderer& _renderer;

};

class AdvancedRenderi ngEngine

{

public:

AdvancedRenderi ngEngine( IAdvancedRender er& renderer)

:_renderer(rend erer)

{}

void renderAdvanced( )

{

_renderer.rende rAdvanced();

}

void render()

{

_renderer.rende r();

}

private:

IAdvancedRender er& _renderer;

};

IAdvancedRender er* createRenderer( )

{

return new RendererImpl();

}

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

{

RendererImpl renderer ;

AdvancedRenderi ngEngine 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(PubInterfa ce&);
};

void doIt(PubInterfa ce& i)
{
i.PubFunc();

PrivInterface* pi = dynamic_cast<Pr ivInterface*>(& 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
2712
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 page. HTTP_REFERER, while not completely reliable, would serve the purpose except for another problem. The hyperlink points to a JavaScript function which opens the popup. This yields HTTP_REFERER worthless. My other thought was to create a...
21
2031
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 implement multiple interfaces.
4
4642
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 or field public? Thanks, Dennis
0
1003
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 browsers. The site is open to the public, no authentication is required, and web application runs under default iis user account. Thanks!
10
1710
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 the URL could open them). I've run across a number of solutions on the web, none however seem to work for me. It's possible I'm just missing something stupid and obvious. (I hope that's it.) First, this page advised me to add a setting to IIS...
8
8747
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 the title field gets its information from a previouse page so its value can easily be over 40 chars. (I can not restrict the length on the previouse page.) The major dificulty is that there is no form on the aspx page, and I do not have access to...
2
2018
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' there is a global variable declared 'int GlobalVariable' and the same variable is extern in the file 'b.c'.
2
2108
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 forum website using php and mysql. in the root directory i have created a .htaccess file and whenever someone access a file which is not on the server i have created a user friendly message that the file does not exist instead of a 404
7
4048
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 page, problem occurs when a user authenticated themselves then they will be redirected on welcome page and he can access my files through welcome page on Browser by knowing my Folder Name. but i do'nt want to give permissions to access on welcome...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10330
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9952
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8976
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7500
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.