vrecko
virtual reality framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BaseClass.h
Go to the documentation of this file.
1 
13 #ifndef BASECLASS_H
14 #define BASECLASS_H
15 
16 #include <vrecko/Export>
17 
18 #include <vector>
19 #include <string>
20 #include <map>
21 
22 #include <vrecko/MTLock.h>
23 #include <vrecko/Message.h>
24 
25 //#include <osg/Timer>
26 
27 //#include <xercesc/parsers/AbstractDOMParser.hpp>
28 //#include <xercesc/dom/DOMDocument.hpp>
29 
30 #include <xercesc/util/XercesDefs.hpp>
31 
32 #include <vrecko/Logger.h>
33 
34 XERCES_CPP_NAMESPACE_BEGIN
35  class DOMNode;
36  class DOMDocument;
37 XERCES_CPP_NAMESPACE_END
38 
39 
40 namespace vrecko {
41 
42 class VRECKO_EXPORT AutoPtrV;
43 class VRECKO_EXPORT World;
44 class VRECKO_EXPORT BaseClass;
45 
46 #define EVENTDISPATCHER_PRIORITY -1000000
47 
60 class IOPinBase : public osg::Referenced {
61 public:
69  std::string& getPinName() { return pinName; };
70 
81 protected:
82  std::string pinName;
84 };
85 
86 
87 class InputPinBase : public IOPinBase {
88 public:
95  virtual void processMessage(VreckoMessage *message) = 0;
96 };
97 
98 class OutputPinBase : public IOPinBase {
99 };
100 
101 
102 class IORequestPinBase : public IOPinBase {
103 public:
110 };
111 
113 public:
120  virtual osg::ref_ptr<VreckoMessage> processRequest(VreckoMessage *message) = 0;
121 };
122 
124 };
125 
126 
129 class VRECKO_EXPORT BaseClass {
130  protected:
132  typedef std::map<std::string, int> AttributeMap;
135 
136  public:
153  BaseClass(const char* startingSenderString);
154 
161  BaseClass(BaseClass &bc);
162 
170  virtual ~BaseClass();
171 
190  virtual bool initialize(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *parameters = NULL);
191 
201  virtual bool initializeFromString(const char *xmlString);
202 
203 
210  virtual void update(void) {};
211 
220  virtual XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *saveXMLParameters(XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *) {return NULL;}
221 
222 // /// Types of an input/output and parameter.
223 // OBSOLETE after the message revolution in summer 2011.
224 //
225 // typedef enum {
226 // UNKNOWN = 0,
227 // BOOL,
228 // INT,
229 // UINT,
230 // LONG_INT,
231 // ULONG,
232 // FLOAT,
233 // DOUBLE,
234 // STRING,
235 // MESSAGE,
236 // VEC2,
237 // VEC3,
238 // VEC4,
239 // MATRIX,
240 // QUAT,
241 // POINTER,
242 // VEC3ARRAY // = std::vector<osg::Vec3>
243 // } IO_Type;
244 //
245 // /// The struct for description of input.
246 // typedef struct {
247 // /// Name of the input.
248 // std::string name;
249 // /// Type of the input.
250 // IO_Type type;
251 // /// Possible value for the input.
253 // } INPUT_DESCRIPTION;
254 //
255 // /// The struct for description of output.
256 // typedef struct {
257 // /// Name of the output.
258 // std::string name;
259 // /// Type of the output.
260 // IO_Type type;
261 // /// Possible value for the output.
262 // //std::vector<std::string> value;
263 // /// Is it required?
264 // bool required;
265 // } OUTPUT_DESCRIPTION;
266 //
267 // /** Return vector with description of all inputs for the object. */
268 // inline std::vector<INPUT_DESCRIPTION> &getInput(void) {return input;}
269 // /** Return type of the input. */
270 // IO_Type getInputType(const std::string &name);
271 // /** Return vector with description of all outputs for the object. */
272 // inline std::vector<OUTPUT_DESCRIPTION> &getOutput(void) {return output;}
273 // /** Return type of the output. */
274 // IO_Type getOutputType(const std::string &name);
275 //
282  bool inputPresent(const std::string &name);
283 
290  bool outputPresent(const std::string &name);
291 
292 // /* Returns true if the given request input is present */
293 // bool requestInputPresent(const std::string &name);
294 // /* Returns true if the given request output is present */
295 // bool requestOutputPresent(const std::string &name);
296 //
297 // /* Return string of IO_Type. */
298 // static std::string getIO_TypeString(IO_Type type);
299 //
300 // /* Add a input to the existing list of the inputs. */
301 // void addInput(const char *name, IO_Type type);
302 // /* Remove the input from the list of inputs. */
303 // void removeInput(const char *name);
304 //
305 // /* Older way. Add a output to the existing list of the outputs. */
306 // void addOutput(const char *name, IO_Type type);
307 // /* Older way. Remove the output from the list of outputs. */
308 // void removeOutput(const char *name);
309 //
310 // /* Return type of the input. */
311 // VreckoMessage* getRequestInputType(const std::string &name);
312 // /* Return type of the output. */
313 // VreckoMessage* getRequestOutputType(const std::string &name);
314 // /* Add a input to the existing list of the inputs. */
315 // void addRequestInput(const char *name, IO_Type type);
316 // /* Remove the request input from the list of inputs. */
317 // void removeRequestInput(const char *name);
318 // /* Add a output to the existing list of the outputs. */
319 // void addRequestOutput(const char *name, IO_Type type);
320 // /* Remove the request output from the list of outputs. */
321 // void removeRequestOutput(const char *name);
322 
339  void addInputPin(InputPinBase *pin);
340 
351  void removeInputPin(InputPinBase *pin);
352 
363  void addOutputPin(OutputPinBase *pin);
364 
375  void removeOutputPin(OutputPinBase *pin);
376 
377 
388  void addRequestInputPin(RequestInputPinBase *pin);
389 
400  void removeRequestInputPin(RequestInputPinBase *pin);
401 
412  void addRequestOutputPin(RequestOutputPinBase *pin);
423  void removeRequestOutputPin(RequestOutputPinBase *pin);
424 
426  // bit flags
427  NOTIFICATION_DELETE_CLASS = 1,
429  NOTIFICATION_EO_REGISTERING = 2,
434  NOTIFICATION_EO_UNREGISTERING = 4,
439  NOTIFICATION_PROGRAM_TERMINATION = 8,
444  NOTIFICATION_ALL = 0xFFFFFFFF
445  };
446 
456  bool addNotification(BaseClass *receiver, unsigned long notificationTypes);
457 
474  bool removeNotification(BaseClass *receiver, unsigned long notificationTypes = NOTIFICATION_ALL);
475 
492  virtual void processEvent(const std::string& input_name, VreckoMessage *pMessage);
493 
509  virtual VreckoMessage *processRequest(const std::string& request_input_name, VreckoMessage *pMessage);
510 
516  virtual const std::string &getSenderString(void) {return senderString;}
517 
526  void reportError(const std::string &error);
527 
528  /* Sets pointer to the World. */
529  // OBSOLETE: Simply use the [vrecko::world] variable from World.h
530 // inline void setWorldPtr(World *ptr) { /* pWorld = ptr; */ static bool bAlreadyWritten = false; if (!bAlreadyWritten) { logger.debugLog("setWorldPtr() is obsolete, there is now a global \"vrecko::world\" variable holding pointer to World (include World.h to use it)"); bAlreadyWritten = true; } }
531 
532  /* Gets pointer to the World. */
533  // OBSOLETE: Simply use the [vrecko::world] variable from World.h
534 // inline World *getWorldPtr() const { static bool bAlreadyWritten = false; if (!bAlreadyWritten) { logger.debugLog("getWorldPtr() is becoming obsolete, there is now a global \"vrecko::world\" variable holding pointer to World (include World.h to use it)"); bAlreadyWritten = true; } return world; }
535 
555  inline void setPriority(long int pr) {priority = pr;}
556 
564  inline long int getPriority(void) const {return priority;}
565 
575  inline void setAttribute(const std::string &attr_name, int attr_value) { attributeLock.lockWrite(); attribute[attr_name] = attr_value; attributeLock.unlockWrite(); }
576 
585  inline int getAttribute(const std::string &attr_name) { attributeLock.lockRead(); int i = attribute[attr_name]; attributeLock.unlockRead(); return i; }
586 
595  inline int getAttribute(const char* attr_name) { return getAttribute(std::string(attr_name));}
596 
605  void deleteAttribute(const std::string &attr_name);
606 
607 
616  void setSceneFile(const std::string &sceneFileName);
617 
628  inline const std::string &getSceneFile() const { return *sceneFile; }
629 
630 
631 
637  virtual void preInitialize(void) {};
638 
644  virtual void postInitialize(void) {};
645 
654  virtual bool loadXMLParameters(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *) {return true;}
655 
664  virtual bool loadXMLParameters(const char *xmlString);
665 
672  InputPinBase* getInputPin(const char *strName);
673 
680  OutputPinBase* getOutputPin(const char *strName);
681 
688  RequestInputPinBase* getRequestInputPin(const char *strName);
689 
690 
697  RequestOutputPinBase* getRequestOutputPin(const char *strName);
698 
699 
700  protected:
702  std::string senderString;
703 
707  unsigned long notificationTypes;
708  };
709 
713  std::vector<NOTIFICATION_DESCRIPTION> notifications;
714 
715 
716 // /// Pointer to the World.
717 // World *pWorld; OBSOLETE, use [world] from World.h
718 
725  long int priority;
726 
731  const std::string * sceneFile;
732 
733  typedef std::vector<osg::ref_ptr<IOPinBase>> PinList;
734 
735 
739 
742  PinList outputPinsList; // we know that all of them are of type OutputPinBase
743 
747 
751 
754  bool sendNotifications(unsigned long notificationType, void *notificationData);
755 
766  virtual int processNotification(BaseClass *sender, unsigned long notificationType,
767  void *notificationData) { return 0; }
768 
776  inline IOPinBase* findPinInList(const char *strName, PinList* pinList) {
777  for (auto itr = pinList->begin(); itr != pinList->end(); ++itr) {
778  if ((*itr)->getPinName() == strName)
779  return (*itr);
780  }
781  return NULL;
782  }
783 };
784 
785 }
786 
787 #endif