Skip to content

Commit

Permalink
auto: overkill logging
Browse files Browse the repository at this point in the history
  • Loading branch information
multun committed Jul 10, 2018
1 parent bcd4462 commit 68bf686
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 21 deletions.
44 changes: 26 additions & 18 deletions auto.hh
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,22 @@

#include "type_utils.hxx"

template<class Auto>
struct NoopAutoLogger {
template<template<class> class NewState,
template<class> class OldState>
void log() {
}
};

template<template<class> class PInterface,
class AllowedTransitions,
template<class> class ...States>
class States,
template<class> class Logger = NoopAutoLogger>
class Auto {
using self_t = Auto<PInterface, AllowedTransitions, States...>;
using self_t = Auto<PInterface, AllowedTransitions, States, Logger>;
using intf_t = PInterface<self_t>;
using ttlist = TTList<States...>;
using ttlist = States;

template<template<class> class State>
struct Automaker {
Expand Down Expand Up @@ -53,6 +62,8 @@ class Auto {
StateData data_[2];
bool data_pos_;

Logger<self_t> logger;

StateData &cur_data() {
return data_[data_pos_];
}
Expand All @@ -63,6 +74,9 @@ class Auto {

Auto() : data_pos_(0) {}

Auto(Logger<self_t> &&log)
: data_pos_(0), logger(log) {}

public:
auto &get_state() {
return *cur_data().template get_data<intf_t>();
Expand All @@ -80,6 +94,14 @@ public:
return res;
}

template<template<class> class InitialState, class ...Args>
static Auto init(Logger<self_t> &&logger, Args&& ...args) {
auto res = Auto{std::move(logger)};
res.cur_data().template enter<InitialState, Args...>(
std::forward<Args>(args)...);
return res;
}

template<template<class> class NewState, class ...Args>
void enter(Args&& ...args) {
other_data().template enter<NewState, Args...>(
Expand All @@ -88,14 +110,6 @@ public:
data_pos_ = !data_pos_;
}

template<template<class> class NewState,
template<class> class OldState,
class Auto>
static void default_logger(Auto) {
std::clog << "transition from " << typeid(OldState<Auto>).name() << " to "
<< typeid(NewState<Auto>).name() << std::endl;
}

template<template<class> class NewState,
template<class> class OldState,
class ...Args>
Expand All @@ -108,13 +122,7 @@ public:
/* traits on variadic templates would be mandatory,
which is too annoying to be worth it */

#ifdef DEFAULT_LOG_TRANSITIONS
# define LOG_TRANSITIONS default_logger
#endif

#ifdef LOG_TRANSITIONS
LOG_TRANSITIONS<NewState, OldState>(this);
#endif
logger.template log<NewState, OldState>();

enter<NewState, Args...>(std::forward<Args>(args)...);
}
Expand Down
36 changes: 33 additions & 3 deletions test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,41 @@ struct StateB : ExInterface<Auto> {
};


using trans = TList<TPair<StateB, StateA>>;
using MyAuto = Auto<ExInterface, trans, StateA, StateB>;
template<class Auto>
struct OverkillLogger {
size_t log_count = 0;

int main() {
OverkillLogger(size_t msg_id)
: log_count{msg_id} {}

template<template<class> class NewState,
template<class> class OldState>
void log() {
std::clog << log_count++ << " transition from "
<< typeid(OldState<Auto>).name() << " to "
<< typeid(NewState<Auto>).name() << std::endl;
}
};

using Transitions = TList<TTPair<StateB, StateA>>;
using States = TTList<StateA, StateB>;
using MyAuto = Auto<ExInterface, Transitions, States>;
using MyLoggingAuto = Auto<ExInterface, Transitions, States, OverkillLogger>;

void logging_example() {
auto logger = OverkillLogger<MyLoggingAuto>{42};
auto a = MyLoggingAuto::template init<StateB>(std::move(logger), 1);
a.get_state().callback(a);
a.get_state().callback(a);
}

void silent_example() {
auto a = MyAuto::template init<StateB>(1);
a.get_state().callback(a);
a.get_state().callback(a);
}

int main() {
logging_example();
silent_example();
}

0 comments on commit 68bf686

Please sign in to comment.