472,811 Members | 1,615 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Subclass as a dynamic library

I'm developing a robot control architecture, and am trying to maximize platform independence (robot hardware, not OS) by generating my individual behaviors as dynamic libraries. The individual behaviors are subclasses of the Leaf class, which is in turn a subclass of the Behavior class. I want to keep the Behavior and Leaf objects in the main program but generate the individual behaviors (e.g. Wander) in shared object files so I can load the specific behavior at run time. I want to access the generated objects through the superclass. That is, if included in the code, I would create the object using
Expand|Select|Wrap|Line Numbers
  1. Behavior * myBeh = new Wander;
. This works fine. When using dlopen, I use
Expand|Select|Wrap|Line Numbers
  1. void *hndl = dlopen(filename.c_str(), RTLD_LAZY);  // dynamically load the library, we'll try to handle errors later.
  2.             if(hndl == NULL)
  3.             {
  4.                 cerr << "File Boo Boo: " <<  dlerror() << endl;
  5.                 exit(-1);
  6.             }
  7.             cout << "loaded " << name << " behavior." << endl;
  8.             create_t* create_type = (create_t*) dlsym(hndl, "maker");  // run maker function to generate instance
  9.             if (!create_type)
  10.             {
  11.                 cerr << "Symbol Boo Boo: " << dlerror() << '\n';
  12.                 exit(1);
  13.             }
  14.             beh = create_type();
I keep running into the "Symbol Boo Boo". I suspect the problem is in the creation of my library. The (pertinent) Wander code is
Expand|Select|Wrap|Line Numbers
  1. #include "Wander.h"
  2.  
  3. Wander::Wander()
  4. {
  5.     ...
  6. }
  7.  
  8. Wander::~Wander()
  9. {
  10.     ...
  11. }
  12.  
  13. Behavior * Wander::maker()
  14.     {
  15.         return new Wander;
  16.     };
  17.  
and the Wander.h file is
Expand|Select|Wrap|Line Numbers
  1. #ifndef _WANDER_H
  2. #define _WANDER_H
  3.  
  4. #include <pthread.h>
  5. #include <string>
  6.  
  7. #include "../../State.h"
  8. #include "../Action.h"
  9. #include "../Leaf.h"
  10.  
  11. using namespace std;
  12.  
  13. #define PI 3.14159265359
  14.  
  15. class Wander : public Leaf
  16. {
  17. private:
  18.     pthread_mutex_t mutex;
  19.     pthread_t behaviorthread;
  20.  
  21.     State* curr_state;
  22.     Action* curr_action;
  23.  
  24.     int turnCounter;
  25.     double velocity;
  26.     double turnrate;
  27.  
  28.     void Lock();
  29.     void Unlock();
  30.  
  31.     static void* DummyMain(void*);
  32.  
  33. public:
  34.     Behavior * maker();
  35.     Wander();
  36.     virtual ~Wander();
  37.  
  38.     void StartThread();
  39.     void StopThread();
  40.  
  41.     void Main();
  42.  
  43.     Action* updAction(State* curr_state);
  44.     Action* genAction();
  45. };
  46.  
  47. #endif
  48.  
my Leaf.h file is:
Expand|Select|Wrap|Line Numbers
  1. #ifndef LEAF_H
  2. #define LEAF_H
  3.  
  4. #include "Behavior.h"
  5. #include "Action.h"
  6. /**
  7.  * A simple behavior, that is, one that is comprised of only a
  8.  * single goal.
  9.  */
  10. class Leaf: public Behavior
  11. {
  12. public:
  13.  
  14.     virtual Behavior * maker()
  15.     {
  16.         return NULL;
  17.     };
  18.     Leaf(){}
  19.     virtual ~Leaf()
  20.     {}
  21.  
  22.     virtual Action* genAction()
  23.     {
  24.         return NULL ;
  25.     }
  26.     virtual void StartThread()
  27.     {};
  28.     virtual void StopThread()
  29.     {};
  30. };
  31. #endif /* LEAF_H */
  32.  
Finally, my Behavior.h file is
Expand|Select|Wrap|Line Numbers
  1. #ifndef _BEHAVIOR_H
  2. #define _BEHAVIOR_H
  3.  
  4. #include    <vector>
  5. #include <string>
  6.  
  7. #include    "Action.h"
  8.  
  9. using namespace std;
  10. /**
  11.  * Base class for the robot behaviors.
  12.  */
  13. class Behavior
  14. {
  15. protected:
  16.     string name;
  17. public:
  18.     virtual Behavior * maker()
  19.     {
  20.         return NULL;
  21.     };
  22.     Behavior()
  23.     {}
  24.     virtual ~Behavior()
  25.     {}
  26.  
  27.     /**
  28.      * Generate an Action based on the current State.
  29.      */
  30.     virtual Action* genAction()
  31.     {
  32.         return NULL ;
  33.     }
  34.     virtual void StartThread()
  35.     {}
  36.     ;
  37.     virtual void StopThread()
  38.     {}
  39.     ;
  40. };
  41.  
  42. typedef Behavior * create_t();
  43.  
  44. #endif /* _BEHAVIOR_H */
  45.  
any ideas?
Jan 11 '08 #1
0 1318

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Angelos Karantzalis | last post by:
Hi guys. I've come across a problem when I tried to serialize a class into xml, only to discover that the parent class's XML Serialization properties weren't included in the output xml. ...
3
by: K.S.Liang | last post by:
Hi all, 1> If there are more than one dynamic linking libraries in the file system, how do I know which one is loaded into system? Any C library or system call can tell me which *.so or *.sl is...
6
by: MikeY | last post by:
Hi Everyone, Does anyone know where I can get my hands on a sample with source code of a simple dynamic button control in C# Windows form. I am looking for a sample that uses a class library...
15
by: Nak | last post by:
Hi there, Is it possible to load a web service dynamically? i.e. If the web service were to exist on a system with a non static IP address, an application could download a "locator" file that...
7
by: Mike Livenspargar | last post by:
We have an application converted from v1.1 Framework to v2.0. The executable references a class library which in turn has a web reference. The web reference 'URL Behavior' is set to dynamic. We...
8
by: Lou Pecora | last post by:
I've been scanning Python in a Nutshell, but this seems to be either undoable or so subtle that I don't know how to do it. I want to subclass a base class that is returned from a Standard Library...
6
by: Me | last post by:
I need to be able to acces non-virtual members of sublcasses via a base class pointer...and without the need for an explicit type cast. I thought a pure virtual getPtr() that acts as a type cast...
4
by: Kurt Smith | last post by:
Hi List: Class inheritance noob here. For context, I have the following base class and subclass: class Base(object): def __init__(self, val): self.val = val
3
by: =?Utf-8?B?YzY3NjIyOA==?= | last post by:
Hi all, If I open the Dynamic help window in VS, why it doesn't show anything when I move a mouse to a system class say, dictionary. it says "No links are available for the current select"....
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.