]> git.mxchange.org Git - simgear.git/blobdiff - simgear/structure/subsystem_mgr.hxx
Tweak APi for GCC happiness.
[simgear.git] / simgear / structure / subsystem_mgr.hxx
index e763351459f7107935f12777937840ad704b1d88..1919db4b458d1a116a88a4bd2b8b37e9315ac86b 100644 (file)
@@ -1,3 +1,4 @@
+
 // Written by David Megginson, started 2000-12
 //
 // Copyright (C) 2000  David Megginson, david@megginson.com
 #include <string>
 #include <map>
 #include <vector>
-SG_USING_STD(map);
-SG_USING_STD(vector);
-SG_USING_STD(string);
 
-#include <simgear/props/props.hxx>
 #include <simgear/timing/timestamp.hxx>
-#include "SGSmplstat.hxx"
-
+#include <simgear/structure/SGSharedPtr.hxx>
+#include <simgear/misc/strutils.hxx>
 
 class TimingInfo
 {
 private:
-    string eventName;
+    std::string eventName;
     SGTimeStamp time;
 
 public: 
-    TimingInfo(string name, SGTimeStamp &t) { eventName = name; time = t;};
-    string getName() { return eventName; };
-    SGTimeStamp getTime() { return time; };
+    TimingInfo(const std::string& name, const SGTimeStamp &t) :
+        eventName(name), time(t)
+    { }
+    const std::string& getName() const { return eventName; }
+    const SGTimeStamp& getTime() const { return time; }
 };
 
-typedef vector<TimingInfo> eventTimeVec;
-typedef vector<TimingInfo>::iterator eventTimeVecIterator;
+class SampleStatistic;
 
+typedef std::vector<TimingInfo> eventTimeVec;
+typedef std::vector<TimingInfo>::iterator eventTimeVecIterator;
+
+typedef void (*SGSubsystemTimingCb)(void* userData, const std::string& name, SampleStatistic* pStatistic);
 
 \f
 /**
@@ -148,6 +150,13 @@ public:
    */
   virtual void init ();
 
+  typedef enum
+  {
+    INIT_DONE,      ///< subsystem is fully initialised
+    INIT_CONTINUE   ///< init should be called again
+  } InitStatus;
+  
+  virtual InitStatus incrementalInit ();
 
   /**
    * Initialize parts that depend on other subsystems having been initialized.
@@ -170,6 +179,16 @@ public:
   virtual void reinit ();
 
 
+  /**
+   * Shutdown the subsystem.
+   *
+   * <p>Release any state associated with subsystem. Shutdown happens in
+   * the reverse order to init(), so this is the correct place to do
+   * shutdown that depends on other subsystems.
+   * </p>
+   */
+  virtual void shutdown ();
+   
   /**
    * Acquire the subsystem's property bindings.
    *
@@ -218,7 +237,7 @@ public:
 
 
   /**
-   * Suspend or resum operation of this subsystem.
+   * Suspend or resume operation of this subsystem.
    *
    * @param suspended true if the subsystem should be suspended, false
    * otherwise.
@@ -244,43 +263,25 @@ public:
    */
   virtual bool is_suspended () const;
 
-
   /**
-   * Keep track of execution time.
-   *
-   * <p>This method keeps track of timing statistics for each subsystem.</p>
-   * 
-   * @param time execution time in ms of last call.
+   * Trigger the callback to report timing information for all subsystems.
    */
-  void updateExecutionTime(double time);
-
-  /**
-   * Print details of execution time.
-   *
-   * <p>For debugging purposes, developers can place stamp() calls
-   * at strategic points in the update() function of each subsystem, which 
-   * record the time between the successive calls to stamp. This method,
-   * printExecutionTime() is called after exectution of the subsystem
-   * update function itself to conduct a post-hoc analysis of excecution
-   * time</p>
-   */ 
-  void printTimingInformation();
+  void reportTiming(void);
 
   /**
    * Place time stamps at strategic points in the execution of subsystems 
    * update() member functions. Predominantly for debugging purposes.
    */
-  void stamp(string name);
-  
-
+  void stamp(const std::string& name);
 
 protected:
 
   bool _suspended;
 
   eventTimeVec timingInfo;
-  //int test;
 
+  static SGSubsystemTimingCb reportTimingCb;
+  static void* reportTimingUserData;
 };
 
 
@@ -295,9 +296,11 @@ public:
     SGSubsystemGroup ();
     virtual ~SGSubsystemGroup ();
 
-    virtual void init ();
+    virtual void init();
+    virtual InitStatus incrementalInit ();
     virtual void postinit ();
     virtual void reinit ();
+    virtual void shutdown ();
     virtual void bind ();
     virtual void unbind ();
     virtual void update (double delta_time_sec);
@@ -305,41 +308,37 @@ public:
     virtual void resume ();
     virtual bool is_suspended () const;
 
-    virtual void set_subsystem (const string &name,
+    virtual void set_subsystem (const std::string &name,
                                 SGSubsystem * subsystem,
                                 double min_step_sec = 0);
-    virtual SGSubsystem * get_subsystem (const string &name);
-    virtual void remove_subsystem (const string &name);
-    virtual bool has_subsystem (const string &name) const;
+    virtual SGSubsystem * get_subsystem (const std::string &name);
+    virtual void remove_subsystem (const std::string &name);
+    virtual bool has_subsystem (const std::string &name) const;
 
-    void collectDebugTiming(bool collect);
+    void reportTiming(void);
 
+    /**
+     *
+     */
+    void set_fixed_update_time(double fixed_dt);
+        
+       /**
+        * retrive list of member subsystem names
+        */ 
+    string_list member_names() const;
+        
 private:
 
-    struct Member {
-
-        Member ();
-        Member (const Member &member);
-        virtual ~Member ();
-
-        virtual void update (double delta_time_sec);
-       void printTimingInformation(double time);
-        void printTimingStatistics();
-        void updateExecutionTime(double time);
-        double getTimeWarningThreshold();
-        void collectDebugTiming (bool collect) { collectTimeStats = collect; };
-
-        SampleStatistic timeStat;
-        string name;
-        SGSubsystem * subsystem;
-        double min_step_sec;
-        double elapsed_sec;
-        bool collectTimeStats;
-    };
-
-    Member * get_member (const string &name, bool create = false);
+    class Member;
+    Member* get_member (const std::string &name, bool create = false);
 
-    vector<Member *> _members;
+    std::vector<Member *> _members;
+    
+    double _fixedUpdateTime;
+    double _updateTimeRemainder;
+    
+  /// index of the member we are currently init-ing
+    unsigned int _initPosition;
 };
 
 
@@ -371,6 +370,10 @@ public:
     enum GroupType {
         INIT = 0,
         GENERAL,
+        FDM,        ///< flight model, autopilot, instruments that run coupled
+        POST_FDM,   ///< certain subsystems depend on FDM data
+        DISPLAY,    ///< view, camera, rendering updates
+        SOUND/*I want to be last!*/,  ///< needs to run AFTER display, to allow concurrent GPU/sound processing
         MAX_GROUPS
     };
 
@@ -378,8 +381,10 @@ public:
     virtual ~SGSubsystemMgr ();
 
     virtual void init ();
+    virtual InitStatus incrementalInit ();
     virtual void postinit ();
     virtual void reinit ();
+    virtual void shutdown ();
     virtual void bind ();
     virtual void unbind ();
     virtual void update (double delta_time_sec);
@@ -392,17 +397,25 @@ public:
                       GroupType group = GENERAL, 
                       double min_time_sec = 0);
 
+    /**
+     * remove a subsystem, and return a pointer to it.
+     * returns NULL if the subsystem was not found.
+     */
+    virtual SGSubsystem* remove(const char* name);
+
     virtual SGSubsystemGroup * get_group (GroupType group);
 
-    virtual SGSubsystem * get_subsystem(const string &name);
+    virtual SGSubsystem * get_subsystem(const std::string &name) const;
 
-   void collectDebugTiming(bool collect);
+    void reportTiming();
+    void setReportTimingCb(void* userData,SGSubsystemTimingCb cb) {reportTimingCb = cb;reportTimingUserData = userData;}
 
 private:
-
-    SGSubsystemGroup _groups[MAX_GROUPS];
-    map<string,SGSubsystem *> _subsystem_map;
-
+    SGSubsystemGroup* _groups[MAX_GROUPS];
+    unsigned int _initPosition;
+  
+    typedef std::map<std::string, SGSubsystem*> SubsystemDict;
+    SubsystemDict _subsystem_map;
 };