Jump to content
  • Advertisement
Sign in to follow this  
Necrarch

Unity DLL Classes: Hiding data members and methods for client

This topic is 5409 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

As a followup on DLL-Problem: Stack Corruption the following: I want to put some generally used classes into a DLL, so that in future the only thing I need to do is to link my client-app with the DLL. I understand how it works, and I can manage to do this. BUT... :) I don't want my client-app to be aware of the protected or private data members/methods from the classes. I want to be able to tell the client-app which functions it can see or use. For exameple: CImage.h
class CImage
{
    //Public Methods

public:

    uint8* GetData(void) const { return data; }
    uint32 GetHeight(void) const { return height; }
    uint32 GetWidth(void) const { return width; }
    bool   LoadFromFile(char *filename) { blahblahblah; };

    //Private Methods

private:

    bool   DecodeData(void);
    void   SomePrivateFunc1(void);
    void   SomePrivateFunc2(void);

    //Private Attributes

private:

    uint8* data;
    uint32 height;
    uint32 width;
}







I just want my client to be able to see the public methods. It shouldn't be aware of the private data members or private methods. Does anyone have some information for me on how I can do this? I tried to search Internet for it, but I don't seem to be able to find anything about it. [Edited by - Necrarch on September 23, 2004 5:00:11 PM]

Share this post


Link to post
Share on other sites
Advertisement
Two choices: use the Pimpl idiom or an interface class with virtual functions.

Google for the Pimpl idiom if you don't know what it is.

An interface class would look like:


class IImage
{
//Public Methods

public:
virtual ~IImage() { } // Virtual destructor - V. important

virtual uint8* GetData(void) const =0;
virtual uint32 GetHeight(void) const =0;
virtual uint32 GetWidth(void) const =0;
//etc...
};

class CImage : public IImage
{
//Public Methods

public:

uint8* GetData(void) const { return data; }
uint32 GetHeight(void) const { return height; }
uint32 GetWidth(void) const { return width; }
bool LoadFromFile(char *filename) { blahblahblah; };

//Private Methods

private:

bool DecodeData(void);
void SomePrivateFunc1(void);
void SomePrivateFunc2(void);

//Private Attributes

private:

uint8* data;
uint32 height;
uint32 width;
};




Then to use it your dll will return an IImage pointer instead of A CImage pointer.

Share this post


Link to post
Share on other sites
Im a little new to this stuff, so it could be wrong(90% chances hehehe), but i think you should try inheritance... Try to use this as a base and derive that class to other, so the other class will acess the private functions of the base class inside the functions and the client will only use the functions of the derived class, not the base class... I hope it works, didnt payed attention to my OOP classes at University lol!

Share this post


Link to post
Share on other sites
Look into the pImpl idiom.

Also keep in mind this nugget from the C++ FAQ-LITE:

Quote:

[7.7] Is Encapsulation a Security device?
No.

Encapsulation != security.

Encapsulation prevents mistakes, not espionage.


In other words, if you're doing this to try and keep people from doing things that they want to do but you don't want them to do, you are in for failure.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
Look into the pImpl idiom.

Also keep in mind this nugget from the C++ FAQ-LITE:

Quote:

[7.7] Is Encapsulation a Security device?
No.

Encapsulation != security.

Encapsulation prevents mistakes, not espionage.


In other words, if you're doing this to try and keep people from doing things that they want to do but you don't want them to do, you are in for failure.


Yeah, I'm trying to prevent others from secretely spying at my million-dollar-nooby-code :) hehe nah. It's just for learning purposes, to prevent mistakes, and to keep stuff clean for developing on my client apps.

Share this post


Link to post
Share on other sites
If you want to create instances of your class, e.g. CImage, as local variables, your compiler has to know the exact size of the class. The compiler will then generate code to reserve that space on the stack (usually by subtracting a value from the stack pointer in the function prologue).

But if you change the declaration of the class, e.g. removing private data members, you also change the size of the class, which will make your program crash as discussed in the previous thread.

So let the dll create instances of the class, because the dll knows the correct size, and just return a pointer to some base interface. Your interface should consist solely of virtual (abstract) functions.

Another solution would be to overwrite the new operator for that class and implement it in the dll. But then take care of multiple inheritance.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!