沈
沈世鈞
Guest
translating c++ program into c#
when assign struct to a variable in a class for name "Event", do not know how to assign
public class RowClass
{
// Start Event Next Action
public string Start { get; set; }
public string Next { get; set; }
public object Event { get; set; } <--
public void Action { get; set; }
}
struct play {};
struct open_close {};
struct cd_detected {};
struct pause {};
struct stop {};
public static IList<T> FillRow<T>() where T : new()
{
IList<T> rows = new List<T>();
// Start Event Next Action
// +---------+-------------------+---------+------------------+
string[] lines = System.IO.File.ReadAllLines(@"D:\Data\My Documents\Visual Studio 2008\Projects\TestDynSMCSharp\TestDynSMCSharp\bin\Debug\Test.txt");
foreach (string line in lines)
{
string[] cols = line.Split(',');
int count = 1;
RowClass rc = new RowClass();
foreach (string col in cols)
{
Console.Write(col + " ");
if (count == 1)
{
if (col.Trim() == "Stopped")
{
rc.Start = "Stopped";
}
else if (col.Trim() == "Open")
{
rc.Start = "Open";
}
else if (col.Trim() == "Empty")
{
rc.Start = "Empty";
}
else if (col.Trim() == "Playing")
{
rc.Start = "Playing";
}
else if (col.Trim() == "Paused")
{
rc.Start = "Paused";
}
}
else if (count == 2)
{
if (col.Trim() == "play")
{
rc.Event = play; <----
}
else if (col.Trim() == "open_close")
{
}
else if (col.Trim() == "cd_detected")
{
}
else if (col.Trim() == "stop")
{
}
else if (col.Trim() == "pause")
{
}
}
else if (count == 3)
{
}
else if (count == 4)
{
}
count += 1;
}
Console.WriteLine("");
}
//State1Delegate s1 = new State1Delegate(function1);
//rc.Action = State1Delegate("1");
return rows;
}
#include <boost/mpl/fold.hpp>
#include <boost/mpl/filter_view.hpp>
#include <boost/type_traits/is_same.hpp>
#include <vector>
#include <ctime>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/placeholders.hpp>
#include <boost/mpl/assert.hpp>
#include <boost/static_assert.hpp>
namespace mpl = boost::mpl;
using namespace mpl:laceholders;
#include <cassert>
template<
class Transition
, class Next
>
struct event_dispatcher
{
typedef typename Transition::fsm_t fsm_t;
typedef typename Transition::event event;
static int dispatch(
fsm_t& fsm, int state, event const& e)
{
if (state == Transition::current_state)
{
Transition::execute(fsm, e);
return Transition::next_state;
}
else // move on to the next node in the chain.
{
return Next::dispatch(fsm, state, e);
}
}
};
template <class Derived> class state_machine;
struct default_event_dispatcher
{
template<class FSM, class Event>
static int dispatch(
state_machine<FSM>& m, int state, Event const& e)
{
return m.call_no_transition(state, e);
}
};
template<class Table, class Event>
struct generate_dispatcher;
template<class Derived>
class state_machine
{
// ...
protected:
template<
int CurrentState
, class Event
, int NextState
, void (*action)(Derived&, Event const&)
>
struct row
{
// for later use by our metaprogram
enum { current_state = CurrentState };
enum { next_state = NextState };
typedef Event event;
typedef Derived fsm_t;
// do the transition action.
static void execute(Derived& fsm, Event const& e)
{
(*action)(fsm, e);
}
};
friend class default_event_dispatcher;
template <class Event>
int call_no_transition(int state, Event const& e)
{
return static_cast<Derived*>(this) // CRTP downcast
->no_transition(state, e);
}
//
public:
template<class Event>
int process_event(Event const& evt)
{
// generate the dispatcher type.
typedef typename generate_dispatcher<
/*typename*/ Derived::transition_table, Event
>::type dispatcher;
// dispatch the event.
this->state = dispatcher::dispatch(
*static_cast<Derived*>(this) // CRTP downcast
, this->state
, evt
);
// return the new state
return this->state;
}
// ...
protected:
state_machine()
: state(Derived::initial_state)
{
}
private:
int state;
// ...
// ...
public:
template <class Event>
int no_transition(int state, Event const& e)
{
assert(false);
return state;
}
// ...
////
};
template <class Event>
struct is_same_event
{
template <class Transition> struct apply
: boost::is_same<Event,typename Transition::event>
{
};
};
template<class Table, class Event>
struct generate_dispatcher
: mpl::fold<
mpl::filter_view< // select rows triggered by Event
Table
, is_same_event<Event>
>
, default_event_dispatcher
, event_dispatcher<_2,_1>
>
{};
struct play {};
struct open_close {};
struct cd_detected {
cd_detected(char const*, std::vector<clock_t> const&) {}
};
#ifdef __GNUC__ // in which pause seems to have a predefined meaning
# define pause pause_
#endif
struct pause {};
struct stop {};
// concrete FSM implementation
class player : public state_machine<player>
{
public:
// the list of FSM states
enum states {
Empty, Open, Stopped, Playing, Paused
, initial_state = Empty
};
#ifdef __MWERKS__
public: // Codewarrior bug workaround. Tested at 0x3202
#endif
static void start_playback(player&, play const&);
static void open_drawer(player&, open_close const&);
static void close_drawer(player&, open_close const&);
static void store_cd_info(player&, cd_detected const&);
static void stop_playback(player&, stop const&);
static void pause_playback(player&, pause const&);
static void resume_playback(player&, play const&);
static void stop_and_open(player&, open_close const&);
#ifdef __MWERKS__
private:
#endif
friend class state_machine<player>;
typedef player p; // makes transition table cleaner
// transition table
struct transition_table : mpl::vector11<
// Start Event Next Action
// +---------+-------------------+---------+------------------+
row < Stopped , play const , Playing , &start_playback >,
row < Stopped , open_close const , Open , &open_drawer >,
// +---------+-------------------+---------+------------------+
row < Open , open_close const , Empty , &close_drawer >,
// +---------+-------------------+---------+------------------+
row < Empty , open_close const , Open , &open_drawer >,
row < Empty , cd_detected const , Stopped , &store_cd_info >,
// +---------+-------------------+---------+------------------+
row < Playing , stop const , Stopped , &stop_playback >,
row < Playing , pause const , Paused , &pause_playback >,
row < Playing , open_close const , Open , &stop_and_open >,
// +---------+-------------------+---------+------------------+
row < Paused , play const , Playing , &resume_playback >,
row < Paused , stop const , Stopped , &stop_playback >,
row < Paused , open_close const , Open , &stop_and_open >
// +---------+-------------------+---------+------------------+
> {};
typedef
event_dispatcher<
row<Stopped, play const, Playing, &start_playback>
, event_dispatcher<
row<Paused, play const, Playing, &resume_playback>
, default_event_dispatcher
>
>
dummy;
};
void player::start_playback(player&, play const&){}
void player:pen_drawer(player&, open_close const&){}
void player::close_drawer(player&, open_close const&){}
void player::store_cd_info(player&, cd_detected const&){}
void player::stop_playback(player&, stop const&){}
void player:ause_playback(player&, pause const&){}
void player::resume_playback(player&, play const&){}
void player::stop_and_open(player&, open_close const&){}
int main()
{
player p; // An instance of the FSM
p.process_event(open_close()); // user opens CD player
p.process_event(open_close()); // inserts CD and closes
p.process_event( // CD is detected
cd_detected(
"louie, louie"
, std::vector<clock_t>( /* track lengths */ )
)
);
p.process_event(play()); // etc.
p.process_event(pause());
p.process_event(play());
p.process_event(stop());
return 0;
}
我要你知道,這個世界上有一個人會永遠等著你
Continue reading...
when assign struct to a variable in a class for name "Event", do not know how to assign
public class RowClass
{
// Start Event Next Action
public string Start { get; set; }
public string Next { get; set; }
public object Event { get; set; } <--
public void Action { get; set; }
}
struct play {};
struct open_close {};
struct cd_detected {};
struct pause {};
struct stop {};
public static IList<T> FillRow<T>() where T : new()
{
IList<T> rows = new List<T>();
// Start Event Next Action
// +---------+-------------------+---------+------------------+
string[] lines = System.IO.File.ReadAllLines(@"D:\Data\My Documents\Visual Studio 2008\Projects\TestDynSMCSharp\TestDynSMCSharp\bin\Debug\Test.txt");
foreach (string line in lines)
{
string[] cols = line.Split(',');
int count = 1;
RowClass rc = new RowClass();
foreach (string col in cols)
{
Console.Write(col + " ");
if (count == 1)
{
if (col.Trim() == "Stopped")
{
rc.Start = "Stopped";
}
else if (col.Trim() == "Open")
{
rc.Start = "Open";
}
else if (col.Trim() == "Empty")
{
rc.Start = "Empty";
}
else if (col.Trim() == "Playing")
{
rc.Start = "Playing";
}
else if (col.Trim() == "Paused")
{
rc.Start = "Paused";
}
}
else if (count == 2)
{
if (col.Trim() == "play")
{
rc.Event = play; <----
}
else if (col.Trim() == "open_close")
{
}
else if (col.Trim() == "cd_detected")
{
}
else if (col.Trim() == "stop")
{
}
else if (col.Trim() == "pause")
{
}
}
else if (count == 3)
{
}
else if (count == 4)
{
}
count += 1;
}
Console.WriteLine("");
}
//State1Delegate s1 = new State1Delegate(function1);
//rc.Action = State1Delegate("1");
return rows;
}
#include <boost/mpl/fold.hpp>
#include <boost/mpl/filter_view.hpp>
#include <boost/type_traits/is_same.hpp>
#include <vector>
#include <ctime>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/placeholders.hpp>
#include <boost/mpl/assert.hpp>
#include <boost/static_assert.hpp>
namespace mpl = boost::mpl;
using namespace mpl:laceholders;
#include <cassert>
template<
class Transition
, class Next
>
struct event_dispatcher
{
typedef typename Transition::fsm_t fsm_t;
typedef typename Transition::event event;
static int dispatch(
fsm_t& fsm, int state, event const& e)
{
if (state == Transition::current_state)
{
Transition::execute(fsm, e);
return Transition::next_state;
}
else // move on to the next node in the chain.
{
return Next::dispatch(fsm, state, e);
}
}
};
template <class Derived> class state_machine;
struct default_event_dispatcher
{
template<class FSM, class Event>
static int dispatch(
state_machine<FSM>& m, int state, Event const& e)
{
return m.call_no_transition(state, e);
}
};
template<class Table, class Event>
struct generate_dispatcher;
template<class Derived>
class state_machine
{
// ...
protected:
template<
int CurrentState
, class Event
, int NextState
, void (*action)(Derived&, Event const&)
>
struct row
{
// for later use by our metaprogram
enum { current_state = CurrentState };
enum { next_state = NextState };
typedef Event event;
typedef Derived fsm_t;
// do the transition action.
static void execute(Derived& fsm, Event const& e)
{
(*action)(fsm, e);
}
};
friend class default_event_dispatcher;
template <class Event>
int call_no_transition(int state, Event const& e)
{
return static_cast<Derived*>(this) // CRTP downcast
->no_transition(state, e);
}
//
public:
template<class Event>
int process_event(Event const& evt)
{
// generate the dispatcher type.
typedef typename generate_dispatcher<
/*typename*/ Derived::transition_table, Event
>::type dispatcher;
// dispatch the event.
this->state = dispatcher::dispatch(
*static_cast<Derived*>(this) // CRTP downcast
, this->state
, evt
);
// return the new state
return this->state;
}
// ...
protected:
state_machine()
: state(Derived::initial_state)
{
}
private:
int state;
// ...
// ...
public:
template <class Event>
int no_transition(int state, Event const& e)
{
assert(false);
return state;
}
// ...
////
};
template <class Event>
struct is_same_event
{
template <class Transition> struct apply
: boost::is_same<Event,typename Transition::event>
{
};
};
template<class Table, class Event>
struct generate_dispatcher
: mpl::fold<
mpl::filter_view< // select rows triggered by Event
Table
, is_same_event<Event>
>
, default_event_dispatcher
, event_dispatcher<_2,_1>
>
{};
struct play {};
struct open_close {};
struct cd_detected {
cd_detected(char const*, std::vector<clock_t> const&) {}
};
#ifdef __GNUC__ // in which pause seems to have a predefined meaning
# define pause pause_
#endif
struct pause {};
struct stop {};
// concrete FSM implementation
class player : public state_machine<player>
{
public:
// the list of FSM states
enum states {
Empty, Open, Stopped, Playing, Paused
, initial_state = Empty
};
#ifdef __MWERKS__
public: // Codewarrior bug workaround. Tested at 0x3202
#endif
static void start_playback(player&, play const&);
static void open_drawer(player&, open_close const&);
static void close_drawer(player&, open_close const&);
static void store_cd_info(player&, cd_detected const&);
static void stop_playback(player&, stop const&);
static void pause_playback(player&, pause const&);
static void resume_playback(player&, play const&);
static void stop_and_open(player&, open_close const&);
#ifdef __MWERKS__
private:
#endif
friend class state_machine<player>;
typedef player p; // makes transition table cleaner
// transition table
struct transition_table : mpl::vector11<
// Start Event Next Action
// +---------+-------------------+---------+------------------+
row < Stopped , play const , Playing , &start_playback >,
row < Stopped , open_close const , Open , &open_drawer >,
// +---------+-------------------+---------+------------------+
row < Open , open_close const , Empty , &close_drawer >,
// +---------+-------------------+---------+------------------+
row < Empty , open_close const , Open , &open_drawer >,
row < Empty , cd_detected const , Stopped , &store_cd_info >,
// +---------+-------------------+---------+------------------+
row < Playing , stop const , Stopped , &stop_playback >,
row < Playing , pause const , Paused , &pause_playback >,
row < Playing , open_close const , Open , &stop_and_open >,
// +---------+-------------------+---------+------------------+
row < Paused , play const , Playing , &resume_playback >,
row < Paused , stop const , Stopped , &stop_playback >,
row < Paused , open_close const , Open , &stop_and_open >
// +---------+-------------------+---------+------------------+
> {};
typedef
event_dispatcher<
row<Stopped, play const, Playing, &start_playback>
, event_dispatcher<
row<Paused, play const, Playing, &resume_playback>
, default_event_dispatcher
>
>
dummy;
};
void player::start_playback(player&, play const&){}
void player:pen_drawer(player&, open_close const&){}
void player::close_drawer(player&, open_close const&){}
void player::store_cd_info(player&, cd_detected const&){}
void player::stop_playback(player&, stop const&){}
void player:ause_playback(player&, pause const&){}
void player::resume_playback(player&, play const&){}
void player::stop_and_open(player&, open_close const&){}
int main()
{
player p; // An instance of the FSM
p.process_event(open_close()); // user opens CD player
p.process_event(open_close()); // inserts CD and closes
p.process_event( // CD is detected
cd_detected(
"louie, louie"
, std::vector<clock_t>( /* track lengths */ )
)
);
p.process_event(play()); // etc.
p.process_event(pause());
p.process_event(play());
p.process_event(stop());
return 0;
}
我要你知道,這個世界上有一個人會永遠等著你
Continue reading...