Index: trunk/FACT++/src/StateMachineImp.cc
===================================================================
--- trunk/FACT++/src/StateMachineImp.cc	(revision 13833)
+++ trunk/FACT++/src/StateMachineImp.cc	(revision 13834)
@@ -515,13 +515,10 @@
 // --------------------------------------------------------------------------
 //
-//! Returns a pointer to a newly allocated object of base EventImp.
-//! It is meant to be overloaded by derived classes to create their
-//! own kind of events.
-//!
-//! @param targetstate
-//!    Defines the target state of the new transition. If \b must be
-//!    greater or equal zero. A negative target state is used to flag
-//!    commands which do not initiate a state transition. If this is
-//!    desired use AddEvent instead.
+//! Calling this function, a new (named) event is added to the state
+//! machine. Via a call to CreateEvent a new event is created with the
+//! given targetstate, name and format. 
+//!
+//! The allowed states are passed to the new event and a message
+//! is written to the output-stream.
 //!
 //! @param name
@@ -529,4 +526,8 @@
 //!    will be constructed with the name given to the constructor and this
 //!    name, e.g. "DRIVE/CHANGE_STATE_TO_NEW_STATE"
+//!
+//! @param states
+//!    A comma sepeareted list of ints, e.g. "1, 4, 5, 9" with states
+//!    in which this new state transition is allowed and will be accepted.
 //!
 //! @param fmt
@@ -536,7 +537,19 @@
 //!    received commands is properly extracted. No check is done.
 //
-EventImp *StateMachineImp::CreateEvent(int targetstate, const char *, const char *)
-{
-    return new EventImp(targetstate);
+EventImp &StateMachineImp::AddEvent(const char *name, const char *states, const char *fmt)
+{
+    EventImp *evt = CreateEvent(name, fmt);
+
+    evt->AddAllowedStates(states);
+
+#ifdef DEBUG
+    Out() << ":   " << Time().GetAsStr("%H:%M:%S.%f");
+    Out() << " - Adding command " << evt->GetName();
+    Out() << endl;
+#endif
+
+    fListOfEvents.push_back(evt);
+
+    return *evt;
 }
 
@@ -544,15 +557,6 @@
 //
 //! Calling this function, a new (named) event is added to the state
-//! machine. Via a call to CreateEvent a new event is created with the
-//! given targetstate, name and format. 
-//!
-//! The allowed states are passed to the new event and a message
-//! is written to the output-stream.
-//!
-//! @param targetstate
-//!    Defines the target state (or name) of the new event. If \b must be
-//!    greater or equal zero. A negative target state is used to flag
-//!    commands which do not initiate a state transition. If this is
-//!    desired use the unnamed version of AddEvent instead.
+//! machine. Therefore an instance of type DimEvent is created and added
+//! to the list of available commands fListOfEvents.
 //!
 //! @param name
@@ -561,7 +565,25 @@
 //!    name, e.g. "DRIVE/CHANGE_STATE_TO_NEW_STATE"
 //!
-//! @param states
-//!    A comma sepeareted list of ints, e.g. "1, 4, 5, 9" with states
-//!    in which this new state transition is allowed and will be accepted.
+//! @param s1, s2, s3, s4, s5
+//!    A list of states from which a transition to targetstate is allowed
+//!    by this command.
+//
+EventImp &StateMachineImp::AddEvent(const char *name, int s1, int s2, int s3, int s4, int s5)
+{
+    ostringstream str;
+    str << s1 << ' '  << s2 << ' ' << s3 << ' ' << s4 << ' ' << s5;
+    return AddEvent(name, str.str().c_str(), "");
+}
+
+// --------------------------------------------------------------------------
+//
+//! Calling this function, a new (named) event is added to the state
+//! machine. Therefore an instance of type DimEvent is created and added
+//! to the list of available commands fListOfEvents.
+//!
+//! @param name
+//!    The command name which should initiate the transition. The DimCommand
+//!    will be constructed with the name given to the constructor and this
+//!    name, e.g. "DRIVE/CHANGE_STATE_TO_NEW_STATE"
 //!
 //! @param fmt
@@ -570,40 +592,4 @@
 //!    DimCommand object. However, the user must make sure that the data of
 //!    received commands is properly extracted. No check is done.
-//
-EventImp &StateMachineImp::AddEvent(int targetstate, const char *name, const char *states, const char *fmt)
-{
-    EventImp *evt = CreateEvent(targetstate, name, fmt);
-
-    evt->AddAllowedStates(states);
-
-#ifdef DEBUG
-    Out() << ":   " << Time().GetAsStr("%H:%M:%S.%f");
-    Out() << " - Adding command " << evt->GetName();
-    if (evt->GetTargetState()>=0)
-        Out() << " (transition to " << GetStateDescription(evt->GetTargetState()) << ")";
-    Out() << endl;
-#endif
-
-    fListOfEvents.push_back(evt);
-
-    return *evt;
-}
-
-// --------------------------------------------------------------------------
-//
-//! Calling this function, a new (named) event is added to the state
-//! machine. Therefore an instance of type DimEvent is created and added
-//! to the list of available commands fListOfEvents.
-//!
-//! @param targetstate
-//!    Defines the target state (or name) of the new event. If \b must be
-//!    greater or equal zero. A negative target state is used to flag
-//!    commands which do not initiate a state transition. If this is
-//!    desired use the unnamed version of AddEvent instead.
-//!
-//! @param name
-//!    The command name which should initiate the transition. The DimCommand
-//!    will be constructed with the name given to the constructor and this
-//!    name, e.g. "DRIVE/CHANGE_STATE_TO_NEW_STATE"
 //!
 //! @param s1, s2, s3, s4, s5
@@ -611,118 +597,23 @@
 //!    by this command.
 //
-EventImp &StateMachineImp::AddEvent(int targetstate, const char *name, int s1, int s2, int s3, int s4, int s5)
+EventImp &StateMachineImp::AddEvent(const char *name, const char *fmt, int s1, int s2, int s3, int s4, int s5)
 {
     ostringstream str;
     str << s1 << ' '  << s2 << ' ' << s3 << ' ' << s4 << ' ' << s5;
-    return AddEvent(targetstate, name, str.str().c_str(), "");
-}
-
-// --------------------------------------------------------------------------
-//
-//! Calling this function, a new (named) event is added to the state
-//! machine. Therefore an instance of type DimEvent is created and added
-//! to the list of available commands fListOfEvents.
-//!
-//! @param targetstate
-//!    Defines the target state (or name) of the new event. If \b must be
-//!    greater or equal zero. A negative target state is used to flag
-//!    commands which do not initiate a state transition. If this is
-//!    desired use the unnamed version of AddEvent instead.
-//!
-//! @param name
-//!    The command name which should initiate the transition. The DimCommand
-//!    will be constructed with the name given to the constructor and this
-//!    name, e.g. "DRIVE/CHANGE_STATE_TO_NEW_STATE"
-//!
-//! @param fmt
-//!    A format as defined by the dim system can be given for the command.
-//!    However, it has no real meaning except that it is stored within the
-//!    DimCommand object. However, the user must make sure that the data of
-//!    received commands is properly extracted. No check is done.
-//!
-//! @param s1, s2, s3, s4, s5
-//!    A list of states from which a transition to targetstate is allowed
-//!    by this command.
-//
-EventImp &StateMachineImp::AddEvent(int targetstate, const char *name, const char *fmt, int s1, int s2, int s3, int s4, int s5)
-{
-    ostringstream str;
-    str << s1 << ' '  << s2 << ' ' << s3 << ' ' << s4 << ' ' << s5;
-    return AddEvent(targetstate, name, str.str().c_str(), fmt);
-}
-
-// --------------------------------------------------------------------------
-//
-//! This function calls AddEvent with a target-state of -1 (unnamed
-//! event). This shell be used for configuration commands. As well as
-//! in AddEvent the states in which such a configuration command is
-//! accepted can be given.
-//!
-//! @param name
-//!    The command name which should initiate the transition. The DimCommand
-//!    will be constructed with the name given to the constructor and this
-//!    name, e.g. "DRIVE/CHANGE_STATE_TO_NEW_STATE"
-//!
-//! @param states
-//!    A comma sepeareted list of ints, e.g. "1, 4, 5, 9" with states
-//!    in which this new state transition is allowed and will be accepted.
-//!
-//! @param fmt
-//!    A format as defined by the dim system can be given for the command.
-//!    However, it has no real meaning except that it is stored within the
-//!    DimCommand object. However, the user must make sure that the data of
-//!    received commands is properly extracted. No check is done.
-//!
-EventImp &StateMachineImp::AddEvent(const char *name, const char *states, const char *fmt)
-{
-    return AddEvent(-1, name, states, fmt);
-}
-
-// --------------------------------------------------------------------------
-//
-//! This function calls AddEvent with a target-state of -1 (unnamed
-//! event). This shell be used for configuration commands. As well as
-//! in AddEvent the states in which such a configuration command is
-//! accepted can be given.
-//!
-//! @param name
-//!    The command name which should initiate the transition. The DimCommand
-//!    will be constructed with the name given to the constructor and this
-//!    name, e.g. "DRIVE/CHANGE_STATE_TO_NEW_STATE"
-//!
-//! @param s1, s2, s3, s4, s5
-//!    A list of states from which a transition to targetstate is allowed
-//!    by this command.
-//
-EventImp &StateMachineImp::AddEvent(const char *name, int s1, int s2, int s3, int s4, int s5)
-{
-    return AddEvent(-1, name, s1, s2, s3, s4, s5);
-}
-
-// --------------------------------------------------------------------------
-//
-//! This function calls AddEvent with a target-state of -1 (unnamed
-//! event). This shell be used for configuration commands. As well as
-//! in AddEvent the states in which such a configuration command is
-//! accepted can be given.
-//!
-//! @param name
-//!    The command name which should initiate the transition. The DimCommand
-//!    will be constructed with the name given to the constructor and this
-//!    name, e.g. "DRIVE/CHANGE_STATE_TO_NEW_STATE"
-//!
-//! @param fmt
-//!    A format as defined by the dim system can be given for the command.
-//!    However, it has no real meaning except that it is stored within the
-//!    DimCommand object. However, the user must make sure that the data of
-//!    received commands is properly extracted. No check is done.
-//!
-//! @param s1, s2, s3, s4, s5
-//!    A list of states from which a transition to targetstate is allowed
-//!    by this command.
-//
-EventImp &StateMachineImp::AddEvent(const char *name, const char *fmt, int s1, int s2, int s3, int s4, int s5)
-{
-    return AddEvent(-1, name, fmt, s1, s2, s3, s4, s5);
+    return AddEvent(name, str.str().c_str(), fmt);
+}
+
+EventImp *StateMachineImp::CreateService(const char *)
+{
+    return new EventImp();
+}
+
+// --------------------------------------------------------------------------
+//
+EventImp &StateMachineImp::Subscribe(const char *name)
+{
+    EventImp *evt = CreateService(name);
+    fListOfEvents.push_back(evt);
+    return *evt;
 }
 
@@ -900,5 +791,5 @@
 //
 bool StateMachineImp::HandleNewState(int newstate, const EventImp *evt,
-                                     const char *txt, const char *alt)
+                                     const char *txt)
 {
     if (newstate==kSM_FatalError)
@@ -908,8 +799,5 @@
         return true;
 
-    if (!evt || !alt || newstate==evt->GetTargetState())
-        SetCurrentState(newstate, txt, evt ? evt->GetName() : "");
-    else
-        SetCurrentState(newstate, alt, evt->GetName());
+    SetCurrentState(newstate, txt, evt ? evt->GetName() : "");
 
     return true;
@@ -954,36 +842,26 @@
 bool StateMachineImp::HandleEvent(const EventImp &evt)
 {
+    if (!evt.HasFunc())
+    {
+        Warn(evt.GetName()+": No function assigned... ignored.");
+        return true;
+
+    }
+
+#ifdef DEBUG
     ostringstream out;
     out << "Handle: " << evt.GetName() << "[" << evt.GetSize() << "]";
     Debug(out);
-
-    // Get the new state from the command
-    const int commandstate = evt.GetTargetState();
+#endif
 
     // Check if the received command is allow in the current state
     if (!evt.IsStateAllowed(fCurrentState))
     {
-        ostringstream msg;
-        msg << evt.GetName() << ": Not allowed in state ";
-        msg << GetStateDescription() << "... ignored.";
-        Warn(msg);
+        Warn(evt.GetName()+": Not allowed in state "+GetStateDescription()+"... ignored.");
         return true;
     }
 
-    if (evt.HasFunc())
-        return HandleNewState(evt.ExecFunc(), &evt,
-                              "by ExecFunc function-call");
-
-    // Check if this is a configuration command (a command which
-    // intention is not to change the state of our state-machine
-    if (commandstate<0)
-        return HandleNewState(Configure(evt), &evt, "by Configure-command");
-    else
-        return HandleNewState(Transition(evt), &evt,
-                              "by Transition-command (expected)",
-                              "by Transition-command (unexpected)");
-
-    // This is a fatal error, because it can never happen
-    return false;
+    return HandleNewState(evt.ExecFunc(), &evt,
+                          "by ExecFunc function-call");
 }
 
@@ -1095,5 +973,5 @@
 
             // Execute a step in the current state of the state machine
-            if (!HandleNewState(Execute(), "by Execute-command"))
+            if (!HandleNewState(Execute(), 0, "by Execute-command"))
                 break;
 
Index: trunk/FACT++/src/StateMachineImp.h
===================================================================
--- trunk/FACT++/src/StateMachineImp.h	(revision 13833)
+++ trunk/FACT++/src/StateMachineImp.h	(revision 13834)
@@ -51,21 +51,24 @@
     Event *PopEvent();
 
-    bool HandleNewState(int newstate, const EventImp *evt, const char *txt, const char *alt=0);
-    bool HandleNewState(int newstate, const char *txt)
-    {
-        return HandleNewState(newstate, 0, txt);
-    }
-
-    virtual EventImp *CreateEvent(int targetstate, const char *name, const char *fmt);
+    bool HandleNewState(int newstate, const EventImp *evt, const char *txt);
+
+    virtual EventImp *CreateEvent(const char *name, const char *fmt) = 0;
+    virtual EventImp *CreateService(const char *);
 
     /// Is called continously to execute actions in the current state
     virtual int Execute() { return fCurrentState; }
     /// Is called when a configuration event is to be processed (no transition of state)
-    virtual int Configure(const Event &) { return kSM_FatalError; }
+    //virtual int Configure(const Event &) { return kSM_FatalError; }
     /// Is called when a transition change event is to be processed (from one state to another) is received
-    virtual int Transition(const Event &) { return kSM_FatalError; }
+    //virtual int Transition(const Event &) { return kSM_FatalError; }
 
     virtual void Lock() { }
     virtual void UnLock() { }
+
+    int Wrapper(const std::function<void(const EventImp &)> &f, const EventImp &imp)
+    {
+        f(imp);
+        return GetCurrentState();
+    }
 
 protected:
@@ -77,8 +80,4 @@
     virtual std::string SetCurrentState(int state, const char *txt="", const std::string &cmd="");
 
-    EventImp &AddEvent(int targetstate, const char *name, const char *states, const char *fmt);
-    EventImp &AddEvent(int targetstate, const char *name, int s1=-1, int s2=-1, int s3=-1, int s4=-1, int s5=-1);
-    EventImp &AddEvent(int targetstate, const char *name, const char *fmt, int s1=-1, int s2=-1, int s3=-1, int s4=-1, int s5=-1);
-
     EventImp &AddEvent(const char *name, const char *states, const char *fmt);
     EventImp &AddEvent(const char *name, int s1=-1, int s2=-1, int s3=-1, int s4=-1, int s5=-1);
@@ -93,5 +92,12 @@
     ~StateMachineImp();
 
+    std::function<int(const EventImp &)> Wrap(const std::function<void(const EventImp &)> &func)
+    {
+        return bind(&StateMachineImp::Wrapper, this, func, std::placeholders::_1);
+    }
+
     const std::string &GetName() const { return fName; }
+
+    EventImp &Subscribe(const char *name);
 
     /// return the current state of the machine
