From 73782c19b8c0327f5be1f2add292f61ac01f81aa Mon Sep 17 00:00:00 2001 From: Koji Yano Date: Sat, 11 Jan 2020 20:27:33 +0900 Subject: [PATCH] Change StateMachine.NET. - Remove Context::ThreadType enum. - Add AsyncContext class which inherits from Context. --- StateMachine.NET.TestConsole/Program.cs | 3 +- StateMachine.NET.UnitTest/Class1.cs | 54 ++++++++++++++++--------- StateMachine.NET.UnitTest/Class2.cs | 5 ++- StateMachine.NET/GenericObject.h | 14 ++++++- StateMachine.NET/StateMachine.NET.cpp | 22 ++-------- StateMachine.NET/StateMachine.NET.h | 28 ++++++------- 6 files changed, 69 insertions(+), 57 deletions(-) diff --git a/StateMachine.NET.TestConsole/Program.cs b/StateMachine.NET.TestConsole/Program.cs index 58e5348..a25f07c 100644 --- a/StateMachine.NET.TestConsole/Program.cs +++ b/StateMachine.NET.TestConsole/Program.cs @@ -40,9 +40,8 @@ static void Main(string[] args) } } - class Context : tsm_NET.Generic.Context + class Context : tsm_NET.Generic.AsyncContext { - public Context() : base(ThreadType.Native) { } } class State : tsm_NET.Generic.State diff --git a/StateMachine.NET.UnitTest/Class1.cs b/StateMachine.NET.UnitTest/Class1.cs index 3c79f8d..b28451f 100644 --- a/StateMachine.NET.UnitTest/Class1.cs +++ b/StateMachine.NET.UnitTest/Class1.cs @@ -6,27 +6,25 @@ namespace StateMachine.NET.UnitTest.Generic { - public class Context : Context + [TestFixture] + public class TestCase { - public Context() { } - public Context(ThreadType threadType) : base(threadType) { } - } + public class Context : Context + { + } - public class Event : Event - { - public static Event Null { get; } = null; - } + public class Event : Event + { + public static Event Null { get; } = null; + } - public class State : State - { - public static State Null { get; } = null; - } + public class State : State + { + public static State Null { get; } = null; + } - [TestFixture] - public class GenericTestCase - { [Test] - public void SyncContextTest() + public void BasicTest() { var mockEvent = Substitute.For(); var mockInitialState = Substitute.For(); @@ -88,16 +86,36 @@ public void SyncContextTest() mockStateMonitor.DidNotReceive().onIdle(Arg.Any()); mockStateMonitor.DidNotReceive().onWorkerThreadExit(Arg.Any(), Arg.Any()); } + } + + [TestFixture] + public class AsyncTestCase + { + public class Context : AsyncContext + { + // StateMachine should run on managed thread to test on NUnit. + public Context() : base(true) { } + } + + public class Event : Event + { + public static Event Null { get; } = null; + } + + public class State : State + { + public static State Null { get; } = null; + } [Test] public void BasicTest() { - var mockEvent = Substitute.For(); + var mockEvent = Substitute.For(); var mockInitialState = Substitute.For(); var mockNextState = Substitute.For(); var mockStateMonitor = Substitute.For>(); - var c = new Context(Context.ThreadType.Managed); + var c = new Context(); c.StateMonitor = mockStateMonitor; Assert.That(c.CurrentState, Is.EqualTo(null), "Context has no initial state when created."); diff --git a/StateMachine.NET.UnitTest/Class2.cs b/StateMachine.NET.UnitTest/Class2.cs index ea11961..9110967 100644 --- a/StateMachine.NET.UnitTest/Class2.cs +++ b/StateMachine.NET.UnitTest/Class2.cs @@ -7,7 +7,7 @@ namespace StateMachine.NET.UnitTest { [TestFixture] - public class TestCase + public class AsyncTestCase { [Test] public void BasicTest() @@ -17,7 +17,8 @@ public void BasicTest() var mockNextState = Substitute.For(); var mockStateMonitor = Substitute.For(); - var c = new Context(Context.ThreadType.Managed); + // StateMachine should run on managed thread to test on NUnit. + var c = new AsyncContext(true); c.StateMonitor = mockStateMonitor; Assert.That(c.CurrentState, Is.EqualTo(null), "Context has no initial state when created."); diff --git a/StateMachine.NET/GenericObject.h b/StateMachine.NET/GenericObject.h index 299c788..2f41be4 100644 --- a/StateMachine.NET/GenericObject.h +++ b/StateMachine.NET/GenericObject.h @@ -43,9 +43,11 @@ public ref class StateMonitorCaller : public tsm_NET::StateMonitorCaller generic public ref class Context : public tsm_NET::Context { +protected: + Context(bool isAsync, bool useNativeThread) : tsm_NET::Context(isAsync, useNativeThread), m_stateMonitor(nullptr) {} + public: - Context() : tsm_NET::Context(), m_stateMonitor(nullptr) {} - Context(ThreadType threadType) : tsm_NET::Context(threadType), m_stateMonitor(nullptr) {} + Context() : tsm_NET::Context(false, false), m_stateMonitor(nullptr) {} virtual ~Context() {} HResult setup(S initialState, E event) { return (HResult)tsm_NET::Context::setup((tsm_NET::State^)initialState, (tsm_NET::Event^)event); } @@ -72,6 +74,14 @@ public ref class Context : public tsm_NET::Context StateMonitorCaller^ m_stateMonitorCaller; }; +generic +public ref class AsyncContext : public Context +{ +public: + AsyncContext() : Context(true, false) {} + AsyncContext(bool useNativeThread) : Context(true, useNativeThread) {} +}; + generic where C : tsm_NET::Context where E : tsm_NET::Event diff --git a/StateMachine.NET/StateMachine.NET.cpp b/StateMachine.NET/StateMachine.NET.cpp index 10bcced..50fd8e5 100644 --- a/StateMachine.NET/StateMachine.NET.cpp +++ b/StateMachine.NET/StateMachine.NET.cpp @@ -60,25 +60,9 @@ void StateMonitorCaller::onWorkerThreadExitCallback(tsm::IContext* context, HRES } //-------------- Managed Context class. --------------------// -void Context::construct(ThreadType threadType) -{ - bool isAsync; - switch(threadType) { - case ThreadType::None: - default: - isAsync = false; - m_useNativeThread = false; - break; - case ThreadType::Native: - isAsync = true; - m_useNativeThread = false; - break; - case ThreadType::Managed: - isAsync = true; - m_useNativeThread = true; - break; - } - +void Context::construct(bool isAsync, bool useNativeThread) +{ + m_useNativeThread = useNativeThread; m_nativeContext = new native::Context(this, isAsync); } diff --git a/StateMachine.NET/StateMachine.NET.h b/StateMachine.NET/StateMachine.NET.h index 4503a40..7ce4859 100644 --- a/StateMachine.NET/StateMachine.NET.h +++ b/StateMachine.NET/StateMachine.NET.h @@ -83,22 +83,13 @@ public ref class StateMonitorCaller public ref class Context { -public: - // Worker thread type on which StateMachine runs. - enum class ThreadType - { - // Description Thread creation StateMachine class - None, // No worker thread. None tsm::StateMachine - Native, // Use native thread. CreateThread() Win32 API tsm::AsyncStateMachine - Managed, // Use managed thiread System::Threading::Thread tsm::AsyncStateMachine - }; + void construct(bool isAsync, bool useNativeThread); -private: - void construct(ThreadType threadType); +protected: + Context(bool isAsync, bool useNativeThread) { construct(isAsync, useNativeThread); } public: - Context() { construct(ThreadType::None); } - Context(ThreadType threadType) { construct(threadType); } + Context() { construct(false, false); } virtual ~Context(); !Context(); @@ -131,8 +122,17 @@ public ref class Context protected: NativeType* m_nativeContext; bool m_useNativeThread; - tsm_NET::IStateMonitor^ m_stateMonitor; StateMonitorCaller^ m_stateMonitorCaller; + tsm_NET::IStateMonitor^ m_stateMonitor; +}; + +public ref class AsyncContext : public Context +{ +public: + AsyncContext() : Context(true, false) {} + AsyncContext(bool useNativeThread) : Context(true, useNativeThread) {} + +protected: }; public ref class State