Skip to content

Commit

Permalink
yasmine 0.1.1
Browse files Browse the repository at this point in the history
Release 0.1.1 of yasmine C++11 finite state machine.
  • Loading branch information
SeadexTM committed Sep 19, 2016
1 parent 763d5d0 commit 415bac8
Show file tree
Hide file tree
Showing 231 changed files with 3,577 additions and 11,353 deletions.
43 changes: 6 additions & 37 deletions yasmine/classic_farmroad/i_detector_callback.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,44 +19,13 @@ namespace sxy

class i_detector_callback
{


public:
i_detector_callback
(
) = default;


virtual ~i_detector_callback
(
) = default;


i_detector_callback
(
const i_detector_callback&
) = delete;


i_detector_callback&
operator=
(
const i_detector_callback&
) = delete;


virtual void
detector_on
(
) = 0;


virtual void
detector_off
(
) = 0;


i_detector_callback() = default;
virtual ~i_detector_callback() = default;
i_detector_callback( const i_detector_callback& ) = delete;
i_detector_callback& operator=( const i_detector_callback& ) = delete;
virtual void detector_on() = 0;
virtual void detector_off() = 0;
};


Expand Down
51 changes: 16 additions & 35 deletions yasmine/classic_farmroad/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,24 @@


#ifdef WIN32
void
wait_for_quit_input
(
)
{
void wait_for_quit_input()
{
const HANDLE input_handle = GetStdHandle( STD_INPUT_HANDLE );

bool continue_running = TRUE;
while( continue_running )
{
INPUT_RECORD input_record;
DWORD number_read;
ReadConsoleInput
(
input_handle,
&input_record,
1,
&number_read
);
ReadConsoleInput( input_handle, &input_record, 1, &number_read );

switch( input_record.EventType )
{
case KEY_EVENT:
case KEY_EVENT:
if( input_record.Event.KeyEvent.uChar.AsciiChar == 'q' )
{
{
continue_running = FALSE;
}

break;

default:
Expand All @@ -55,15 +46,12 @@ wait_for_quit_input
}
}


#else
void
wait_for_quit_input
(
)
void wait_for_quit_input()
{
while( true )
{

std::string a = "";
std::cin >> a;
if( a == "q" )
Expand All @@ -72,38 +60,31 @@ wait_for_quit_input
}
}
}


#endif


int
main
(
)
int main()
{
auto l_error_code = 0;

sxy::utils::set_window_size( 250, 9999 );
sxy::utils::maximize_window();

sxy::t_log_manager& log_manager = sxy::t_log_manager::get_instance();
log_manager.set_log_level( sxy::t_log_level::LL_DEBUG );
log_manager.add_logger( std::make_unique< sxy::t_cout_logger >() );

log_manager.start();

yasmine::version::log_version();


try
{
sxy::t_intersection l_intersection;

if( l_intersection.start() )
{
std::cout << "To quit press 'q'." << std::endl;

wait_for_quit_input();

l_intersection.stop();
}
else
Expand All @@ -112,13 +93,13 @@ main
l_error_code = 1;
}
}
catch( const std::exception& p_exception )
{
catch ( const std::exception& p_exception )
{
Y_LOG( sxy::t_log_level::LL_FATAL, "Unhandled exception: '%'.", p_exception.what() );
l_error_code = 2;
}
catch( ... )
{
catch ( ... )
{
Y_LOG( sxy::t_log_level::LL_FATAL, "Unknown exception!" );
l_error_code = 3;
}
Expand Down
71 changes: 27 additions & 44 deletions yasmine/classic_farmroad/t_detector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace
{


constexpr unsigned int g_detector_off_lower_extremity( 1500 );
constexpr unsigned int g_detector_off_lower_extremity( 1500 );
constexpr unsigned int g_detector_off_upper_extremity( 12000 );
constexpr unsigned int g_detector_on_lower_extremity( 1500 );
constexpr unsigned int g_detector_on_upper_extremity( 3000 );
Expand All @@ -35,87 +35,70 @@ namespace sxy
{


t_detector::t_detector
(
i_detector_callback& p_detector_callback
):
m_detector_callback( p_detector_callback ),
m_is_on(false),
m_generate_random_detector_events(),
m_run(false),
m_mutex(),
m_condition_variable()
t_detector::t_detector( i_detector_callback& p_detector_callback )
: m_detector_callback( p_detector_callback ),
m_is_on( false ),
m_generate_random_detector_events(),
m_run( false ),
m_mutex(),
m_condition_variable()
{
// Nothing to do...
}


t_detector::~t_detector
(
) = default;
t_detector::~t_detector() = default;


void
t_detector::start
(
)
void t_detector::start()
{
m_run = true;
m_generate_random_detector_events = std::make_unique<std::thread>( [ this ] () { this->generate_detector_events(); } );
m_generate_random_detector_events =
std::make_unique< std::thread >( [ this ] () { this->generate_detector_events(); } );
}


void
t_detector::stop
(
)
{
void t_detector::stop()
{
{
std::unique_lock< std::mutex > l_lock( m_mutex );
m_run = false;
}
m_condition_variable.notify_all();

Y_ASSERT( m_generate_random_detector_events->joinable(), "Event generator thread is not joinable!" );
m_generate_random_detector_events->join();
m_generate_random_detector_events->join();
m_generate_random_detector_events.reset();
}


bool
t_detector::is_on
(
)
{
bool t_detector::is_on()
{
std::unique_lock< std::mutex > l_lock( m_mutex );
return( m_is_on );
return( m_is_on );
}


void
t_detector::generate_detector_events
(
)
{

void t_detector::generate_detector_events()
{
std::unique_lock< std::mutex > l_lock( m_mutex );
while( m_run )
{
{
std::random_device r;
std::default_random_engine e( r() );

std::default_random_engine e( r() );
m_is_on = false;
{
std::uniform_int_distribution<int> uniform_dist( g_detector_off_lower_extremity, g_detector_off_upper_extremity );
std::uniform_int_distribution< int > uniform_dist( g_detector_off_lower_extremity,
g_detector_off_upper_extremity );
auto time_to_wait = std::chrono::milliseconds( uniform_dist( e ) );
m_condition_variable.wait_for( l_lock, time_to_wait );
m_detector_callback.detector_off();
}

if( m_run )
{
m_is_on = true;
{
std::uniform_int_distribution<int> uniform_dist( g_detector_on_lower_extremity, g_detector_on_upper_extremity );
std::uniform_int_distribution< int > uniform_dist( g_detector_on_lower_extremity,
g_detector_on_upper_extremity );
auto time_to_wait = std::chrono::milliseconds( uniform_dist( e ) );
m_condition_variable.wait_for( l_lock, time_to_wait );
m_detector_callback.detector_on();
Expand Down
65 changes: 12 additions & 53 deletions yasmine/classic_farmroad/t_detector.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
// The same information is available on the www @ http://yasmine.seadex.de/License.html. //
// //
//////////////////////////////////////////////////////////////////////////////////////////////////////


#ifndef T_DETECTOR_5EBE86D2_647F_4029_94D8_B5521F641349
#define T_DETECTOR_5EBE86D2_647F_4029_94D8_B5521F641349
Expand All @@ -28,67 +28,26 @@ class i_detector_callback;

class t_detector final
{
public:
explicit t_detector( i_detector_callback& p_detector_callback );
~t_detector();
t_detector( const t_detector& ) = delete;
t_detector& operator=( const t_detector& ) = delete;
void start();
void stop();
bool is_on();


public:
explicit t_detector
(
i_detector_callback& p_detector_callback
);


~t_detector
(
);


t_detector
(
const t_detector&
) = delete;


t_detector&
operator=
(
const t_detector&
) = delete;


void
start
(
);


void
stop
(
);


bool
is_on
(
);


private:
void
generate_detector_events
(
);
private:
void generate_detector_events();


i_detector_callback& m_detector_callback;
bool m_is_on;

std::unique_ptr<std::thread> m_generate_random_detector_events;
std::unique_ptr< std::thread > m_generate_random_detector_events;
bool m_run;
std::mutex m_mutex;
std::condition_variable m_condition_variable;


};


Expand Down
Loading

0 comments on commit 415bac8

Please sign in to comment.