My problem has probably been came across by a lot of people, but tutorials and things I've seen don't address it (usually too basic).
My problem is that I would like to use Abstraction for a "plug-in" like interface to classes.
Expand|Select|Wrap|Line Numbers
- class ThreadHandle
- {
- /* stuff here not yet dealing with threads */
- /** Method which loops inside thread, when implemented */
- virtual void update() = 0;
- virtual void startThread() = 0;
- virtual void stopThread() = 0;
- }; // End ThreadHandle class
- class ThreadTypeA : public ThreadHandle
- {
- /* stuff dealing with threads for linux */
- protected:
- /** Thread function. */
- static void *updateThread(void *input)
- { /* "input" refers to this class.
- * Loops input->update() function here */ }
- public:
- virtual void startThread() { /* stuff specific to linux */ }
- virtual void stopThread() { /* stuff specific to linux */ }
- }; // End ThreadTypeA class
- class ThreadTypeB : public ThreadHandle
- {
- /* stuff dealing with threads for windows */
- public:
- virtual void startThread() { /* stuff specific to windows */ }
- virtual void stopThread() { /* stuff specific to windows */ }
- }; // End ThreadTypeB class
- class ThreadTypeC : public ThreadHandle
- {
- /* stuff dealing with emulated/fake thread */
- static vector <ThreadTypeC *>emuThreads;
- protected:
- /** Thread function. Runs all update()'s when ran */
- static void *updateThread(void *input)
- { /* "input" is NULL. Loops through all elements
- * in emuThreads, running update() */ }
- virtual void startThread() { /* stuff specific to faking */ }
- virtual void stopThread() { /* stuff specific to faking */ }
- }; // End ThreadTypeC class
- class Thread : public ThreadTypeA, public ThreadTypeB,
- public ThreadTypeC
- { /** Stuff. This is probably a useless class. */ };
- class BaseSystem : ThreadHandle
- {
- public:
- /** Implements what update() does */
- void update() { /* Important threading stuff */ }
- }; // End BaseSystem class
- int main()
- {
- BaseSystem testThreadTypeA, testThreadTypeB,
- testThreadTypeC;
- ThreadHandle *handleA = new ThreadHandleA();
- ThreadHandle *handleB = new ThreadHandleB();
- ThreadHandle *handleC = new ThreadHandleC();
- // &testThreadTypeA = *handleA; // Error of course, but
- // functionality I want.
- // &testThreadTypeB = *handleB; // Error of course
- // &testThreadTypeC = *handleC; // Error of course
- return(1); // unsuccess because I'm stuck : P
- }; // End main() Function
//..../...|...\........|
//...A...B...C...BaseSystem
//.....................|
//..................Derived, if any
I am wishing to make BaseSystem mimic a linux ThreadTypeA, windows ThreadTypeB, or emulated ThreadTypeC. The threads A-C need to use the virtual method update(), as well as BaseSystem needs to implement it.
Would making threadHandle inherit BaseSystem instead of the other way around take care of my problem?
But I need the end user (me and/or my wife) to have another class inherit BaseSystem, not ThreadHandle and have access to the thread... oh. virtual functions would take care of that. But still have problems
Here's an alternative way I'm still stuck...
//.......BaseSystem
//........./...........\
//....Thread....GraphicsSystem
//.......|
//.Types-of-Threads
Expand|Select|Wrap|Line Numbers
- class BaseSystem
- {
- public: // virtual for anything ThreadHandle and
- // BaseSystem needs access to.
- virtual void startThread() = 0;
- virtual void stopThread() = 0;
- /** Implements what update() does */
- virtual void update() = 0;
- }; // End BaseSystem class
- /** Now ThreadHandle could be any name... */
- class ThreadHandle : public BaseSystem
- {
- /* stuff here not yet dealing with threads */
- }; // End ThreadHandle class
- class ThreadTypeA : public ThreadHandle
- {
- /* stuff dealing with threads for linux */
- protected:
- /** Thread function. */
- static void *updateThread(void *input)
- { /* "input" refers to this class.
- * Loops input->update() function here */ }
- public:
- virtual void startThread() { /* stuff specific to linux */ }
- virtual void stopThread() { /* stuff specific to linux */ }
- }; // End ThreadTypeA class
- class ThreadTypeB : public ThreadHandle
- {
- /* stuff dealing with threads for windows */
- public:
- virtual void startThread() { /* stuff specific to windows */ }
- virtual void stopThread() { /* stuff specific to windows */ }
- }; // End ThreadTypeB class
- class ThreadTypeC : public ThreadHandle
- {
- /* stuff dealing with emulated/fake thread */
- static vector <ThreadTypeC *>emuThreads;
- protected:
- /** Thread function. Runs all update()'s when ran */
- static void *updateThread(void *input)
- { /* "input" is NULL. Loops through all elements
- * in emuThreads, running update() */ }
- virtual void startThread() { /* stuff specific to faking */ }
- virtual void stopThread() { /* stuff specific to faking */ }
- }; // End ThreadTypeC class
- class GraphicsUpdatingSystem : public BaseSystem
- {
- int foo;
- public:
- GraphicsUpdatingSystem() { foo = 5; }
- virtual void update() { /* finally implement update() */ }
- int getFoo() const { return(foo); }
- }; // End GraphicsUpdatingSystem class
- int main()
- {
- GraphicsUpdatingSystem *gus;
- // Error because gus inherits BaseSystem, but
- // so does ThreadHandleA...
- gus = new ThreadHandleA();
- return(1); // unsuccess because I'm stuck : P
- }; // End main() Function
//........./...........\
//....Thread....GraphicsSystem
//.......|
//.Types-of-Threads
Is a template the only way to solve this? That makes the executable so much bigger though. And getting GraphicsSystem to still implement update() is a problem too.
The diagram which I need is:
//...SystemA.....SystemB...SystemC......SystemZ
//.................\..........|.............|....... ........./
//..................\.........|.............|....... ......../
//...................-----BaseSystem--------
//..................................|
//......................-----Threadhandle----
//...................../.........|...........|..............\
//..................../..........|...........|...............\
//.......ThreadA...ThreadB....ThreadC.....ThreadZ
[HTML]
// where SystemA *systemA_ptr = new ThreadC();
// for example. But SystemA-Z is independent
// from one another, and ThreadA-Z is independent
// from one another.
[/HTML]
Thank you for any replies. Even if it's not helpful/relevant to solving this problem--a problem I've had for at least 5 years, but one way or another been able to avoid (mostly with less effective code).
And again, I am sorry for the length of this post. Any help on how I post (being a forum noob) would be appreciated too (like with formatting).
I'm currently running Gentoo Linux while I'm running samba to my wifes computer which is running micro$oft. She's in college while I've been out for a year. Both of us CS majors with some basic CIS classes, too.
My compilers are MinGW and GNU gcc. My IDE's are CodeBlocks, dev-cpp, gleany (linux), and notepad/xemacs.