Skip to content

Commit

Permalink
Conditional can take a lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
badaix committed Feb 24, 2021
1 parent 77e8690 commit fd4a341
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
12 changes: 12 additions & 0 deletions aixlog_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,16 @@ int main(int /*argc*/, char** /*argv*/)
LOG(INFO) << every_x << "4th will not be logged\n";
LOG(INFO) << every_x << "5th will not be logged\n";
LOG(INFO) << every_x << "6th will be logged\n";

AixLog::Conditional not_every_3(AixLog::Conditional::EvalFunc([] {
static size_t n(0);
return (++n % 3 != 0);
}));

LOG(INFO) << not_every_3 << "1st will be logged\n";
LOG(INFO) << not_every_3 << "2nd will be logged\n";
LOG(INFO) << not_every_3 << "3rd will not be logged\n";
LOG(INFO) << not_every_3 << "4th will be logged\n";
LOG(INFO) << not_every_3 << "5th will be logged\n";
LOG(INFO) << not_every_3 << "6th will not be logged\n";
}
14 changes: 10 additions & 4 deletions include/aixlog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,23 +269,29 @@ struct TextColor
*/
struct Conditional
{
Conditional() : Conditional(true)
using EvalFunc = std::function<bool()>;

Conditional() : func_([](void) { return true; })
{
}

Conditional(const EvalFunc& func) : func_(func)
{
}

Conditional(bool value) : is_true_(value)
Conditional(bool value) : func_([value](void) { return value; })
{
}

virtual ~Conditional() = default;

virtual bool is_true() const
{
return is_true_;
return func_();
}

protected:
bool is_true_;
EvalFunc func_;
};

/**
Expand Down

0 comments on commit fd4a341

Please sign in to comment.