Commit 3ccbabda by Szeberényi Imre

v0

parents
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="CPPfsm" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/CPPfsm" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-g" />
</Compiler>
</Target>
<Target title="Release">
<Option output="bin/Release/CPPfsm" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O2" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-pedantic-errors" />
<Add option="-pedantic" />
<Add option="-Wall" />
</Compiler>
<Unit filename="fsm.hpp" />
<Unit filename="ly.cpp" />
<Unit filename="ly.h" />
<Extensions>
<code_completion />
<envvars />
<debugger />
<lib_finder disable_auto="1" />
</Extensions>
</Project>
</CodeBlocks_project_file>
/**
* \file fsm.hpp
*
* Generikus inputot kap finite state machine (FSM)
* fsm tervezsi minta felhasznlsval
*
*/
/// Eldeklarci
template <typename T>
class Entity;
/// llapotok absztrakt alposztlya
template <typename T>
class EntityState {
public:
virtual void Input(Entity<T>*, T) = 0;
virtual void Enter(Entity<T>*) = 0;
virtual void Leave(Entity<T>*) = 0;
virtual ~EntityState() {}
};
/// Entits alaposztlya
template <typename T>
class Entity {
EntityState<T>* currentState;
public:
Entity(EntityState<T>* st) : currentState(st) {}
void setState(EntityState<T>* st) {
currentState->Leave(this);
currentState = st;
currentState->Enter(this);
}
void Input(T data) {
currentState->Input(this, data);
}
virtual ~Entity() {}
};
/**
* \file ly.cpp
*
* Ly szmll s llapotainak megvalstsa
* fsm + singleton tervezsi minta felhasznlsval
*
*/
#include <iostream>
#include "ly.h"
/// Ly szmll osztly
class Ly : public Entity<char>{
int sz;
public:
Ly() : Entity<char>(Alap::getInstance()), sz(0) {}
int get() const { return sz; }
void add(int i) { sz += i; }
};
/// Alap llapot inputja
void Alap::Input(Entity<char>* ly, char ch) {
if (ch == 'l' || ch == 'L') {
ly->setState(Ljott::getInstance());
}
}
/// Ljott llapot inputja
void Ljott::Input(Entity<char>* ly, char ch) {
if (ch == 'l' || ch == 'L') {
ly->setState(LLjott::getInstance());
} else {
if (ch == 'y' || ch == 'Y') dynamic_cast<Ly*>(ly)->add(1);
ly->setState(Alap::getInstance());
}
}
/// LLjott llapot inputja
void LLjott::Input(Entity<char>* ly, char ch) {
if (ch == 'y' || ch == 'Y')
dynamic_cast<Ly*>(ly)->add(2);
if (ch != 'l' && ch != 'L')
ly->setState(Alap::getInstance());
}
int main() {
Ly ly;
char ch;
std::cout << "Johet a duma:\n";
while (std::cin >> std::noskipws >> ch)
ly.Input(ch);
std::cout << "ly-ok szama:" << ly.get() << std::endl;
return 0;
}
/**
* \file ly.cpp
*
* Ly számláló állapota és entitása.
* fsm + singleton tervezési minta felhasználásával
*
*/
#include "fsm.hpp"
/// LyState
/// Ly számláló állapotosztályának alapja
class LyState : public EntityState<char> {
public:
void Input(Entity<char>*, char) {}
void Enter(Entity<char>*) {}
void Leave(Entity<char>*) {}
};
/// Alap állapot
class Alap : public LyState {
Alap() {}
Alap(const Alap&);
Alap& operator=(const Alap&);
public:
static Alap* getInstance() {
static Alap singleton;
return &singleton;
}
void Input(Entity<char>* ly, char ch);
};
/// Ljott állapot
class Ljott : public LyState {
Ljott() {}
Ljott(const Ljott&);
Ljott& operator=(const Ljott&);
public:
static Ljott* getInstance() {
static Ljott singleton;
return &singleton;
}
void Input(Entity<char>* ly, char ch);
};
/// LLjott állapot
class LLjott : public LyState {
LLjott() {}
LLjott(const LLjott&);
LLjott& operator=(const LLjott&);
public:
static LLjott* getInstance() {
static LLjott singleton;
return &singleton;
}
void Input(Entity<char>* ly, char ch);
};
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment