Skip to content

Commit

Permalink
use printf instead of cout
Browse files Browse the repository at this point in the history
  • Loading branch information
hit9 committed Apr 29, 2024
1 parent 35c40c7 commit cbd7a87
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 32 deletions.
7 changes: 3 additions & 4 deletions bt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "bt.h"

#include <algorithm> // for max
#include <iostream> // for cout, flush
#include <random> // for mt19937
#include <thread> // for this_thread::sleep_for

Expand All @@ -15,7 +14,7 @@ namespace bt {
/// TreeBlob
/////////////////

std::pair<void*, bool> ITreeBlob::make(const NodeId id, size_t size, const std::size_t cap) {
std::pair<void*, bool> ITreeBlob::make(const NodeId id, const size_t size, const std::size_t cap) {
if (cap) reserve(cap);
std::size_t idx = id - 1;
if (exist(idx)) return {get(idx), false};
Expand Down Expand Up @@ -492,11 +491,11 @@ Status RetryNode::Update(const Context& ctx) {
void RootNode::Visualize(ull seq) {
// CSI[2J clears screen.
// CSI[H moves the cursor to top-left corner
std::cout << "\x1B[2J\x1B[H" << std::flush;
printf("\x1B[2J\x1B[H");
// Make a string.
std::string s;
makeVisualizeString(s, 0, seq);
std::cout << s << std::flush;
printf("%s", s.c_str());
}

void RootNode::TickForever(Context& ctx, std::chrono::nanoseconds interval, bool visualize,
Expand Down
28 changes: 0 additions & 28 deletions bt.h
Original file line number Diff line number Diff line change
Expand Up @@ -834,19 +834,16 @@ class Builder : public _InternalBuilderBase {
root = &r;
onRootAttach(root, sizeof(D), sizeof(typename D::Blob));
}

// Creates a leaf node.
auto& attachLeafNode(Ptr<LeafNode> p) {
_InternalBuilderBase::attachLeafNode(std::move(p));
return *static_cast<D*>(this);
}

// Creates an internal node with optional children.
auto& attachInternalNode(Ptr<InternalNode> p) {
_InternalBuilderBase::attachInternalNode(std::move(p));
return *static_cast<D*>(this);
}

// make a new node onto this tree, returns the unique_ptr.
// Any node creation should use this function.
template <TNode T, typename... Args>
Expand All @@ -864,7 +861,6 @@ class Builder : public _InternalBuilderBase {
void End() {
while (stack.size()) pop(); // clears the stack
}

// Increases indent level to append node.
auto& _() {
level++;
Expand All @@ -888,7 +884,6 @@ class Builder : public _InternalBuilderBase {
else // InternalNode.
return attachInternalNode(make<T>(false, std::forward<Args>(args)...));
}

// Attach a node through move, rarely used.
template <TNode T>
auto& M(T&& inst) {
Expand All @@ -904,33 +899,26 @@ class Builder : public _InternalBuilderBase {
// A SequenceNode executes its children one by one sequentially,
// it succeeds only if all children succeed.
auto& Sequence() { return C<SequenceNode>("Sequence"); }

// A StatefulSequenceNode behaves like a sequence node, executes its children sequentially, succeeds if
// all children succeed, fails if any child fails. What's the difference is, a StatefulSequenceNode skips
// the succeeded children instead of always starting from the first child.
auto& StatefulSequence() { return C<StatefulSequenceNode>("Sequence*"); }

// A SelectorNode succeeds if any child succeeds, fails only if all children fail.
auto& Selector() { return C<SelectorNode>("Selector"); }

// A StatefulSelectorNode behaves like a selector node, executes its children sequentially, succeeds if
// any child succeeds, fails if all child fail. What's the difference is, a StatefulSelectorNode skips the
// failure children instead of always starting from the first child.
auto& StatefulSelector() { return C<StatefulSelectorNode>("Selector*"); }

// A ParallelNode executes its children parallelly.
// It succeeds if all children succeed, and fails if any child fails.
auto& Parallel() { return C<ParallelNode>("Parallel"); }

// A StatefulParallelNode behaves like a parallel node, executes its children parallelly, succeeds if all
// succeed, fails if all child fail. What's the difference is, a StatefulParallelNode will skip the
// "already success" children instead of executing every child all the time.
auto& StatefulParallel() { return C<StatefulParallelNode>("Parallel*"); }

// A RandomSelectorNode determines a child via weighted random selection.
// It continues to randomly select a child, propagating tick, until some child succeeds.
auto& RandomSelector() { return C<RandomSelectorNode>("RandomSelector"); }

// A StatefulRandomSelector behaves like a random selector node, the difference is, a
// StatefulRandomSelector will skip already failed children during a round.
auto& StatefulRandomSelector() { return C<StatefulRandomSelectorNode>("RandomSelector*"); }
Expand All @@ -947,7 +935,6 @@ class Builder : public _InternalBuilderBase {
auto& Action(Args&&... args) {
return C<Impl>(std::forward<Args>(args)...);
}

// Creates a ConditionNode from a lambda function.
// Code example::
// root
Expand All @@ -956,7 +943,6 @@ class Builder : public _InternalBuilderBase {
// ._().Action<A>()
// .End();
auto& Condition(ConditionNode::Checker checker) { return C<ConditionNode>(checker); }

// Creates a ConditionNode by providing implemented Condition class.
// Code example::
// root
Expand All @@ -980,15 +966,13 @@ class Builder : public _InternalBuilderBase {
// ._().Condition<A>()
// .End();
auto& Invert() { return C<InvertNode>("Invert"); }

// Alias to Invert, just named 'Not'.
// Code exapmle::
// root
// .Not()
// ._().Condition<A>()
// .End();
auto& Not() { return C<InvertNode>("Not"); }

// Creates a invert condition of given Condition class.
// Code exapmle::
// root
Expand All @@ -1000,7 +984,6 @@ class Builder : public _InternalBuilderBase {
auto& Not(ConditionArgs... args) {
return C<InvertNode>("Not", make<Condition>(false, std::forward<ConditionArgs>(args)...));
}

// Repeat creates a RepeatNode.
// It will repeat the decorated node for exactly n times.
// Providing n=-1 means to repeat forever.
Expand All @@ -1011,14 +994,12 @@ class Builder : public _InternalBuilderBase {
// ._().Action<A>()
// .End();
auto& Repeat(int n) { return C<RepeatNode>(n, "Repeat"); }

// Alias to Repeat.
// Code exapmle::
// root
// .Loop(3)
// ._().Action<A>();
auto& Loop(int n) { return C<RepeatNode>(n, "Loop"); }

// Timeout creates a TimeoutNode.
// It executes the decorated node for at most given duration.
// Code exapmle::
Expand All @@ -1027,7 +1008,6 @@ class Builder : public _InternalBuilderBase {
// ._().Action<A>()
// .End();
auto& Timeout(std::chrono::milliseconds duration) { return C<TimeoutNode>(duration, "Timeout"); }

// Delay creates a DelayNode.
// Wait for given duration before execution of decorated node.
// Code exapmle::
Expand All @@ -1036,7 +1016,6 @@ class Builder : public _InternalBuilderBase {
// ._().Action<A>()
// .End();
auto& Delay(std::chrono::milliseconds duration) { return C<DelayNode>(duration, "Delay"); }

// Retry creates a RetryNode.
// It executes the decorated node for at most n times.
// A retry will only be initiated if the decorated node fails.
Expand All @@ -1047,12 +1026,10 @@ class Builder : public _InternalBuilderBase {
// ._().Action<A>()
// .End();
auto& Retry(int n, std::chrono::milliseconds interval) { return C<RetryNode>(n, interval, "Retry"); }

// Alias for Retry(-1, interval)
auto& RetryForever(std::chrono::milliseconds interval) {
return C<RetryNode>(-1, interval, "RetryForever");
}

// If creates a ConditionalRunNode.
// It executes the decorated node only if the condition goes true.
// Code example::
Expand All @@ -1065,14 +1042,12 @@ class Builder : public _InternalBuilderBase {
auto condition = make<Condition>(false, std::forward<ConditionArgs>(args)...);
return C<ConditionalRunNode>(std::move(condition), "If");
}

// If creates a ConditionalRunNode from lambda function.
// Code example::
// root
// .If([=](const Context& ctx) { return false; })
// .End();
auto& If(ConditionNode::Checker checker) { return If<ConditionNode>(checker); }

// Switch is just an alias to Selector.
// Code example::
// root
Expand All @@ -1085,17 +1060,14 @@ class Builder : public _InternalBuilderBase {
// ._()._().Action<D>()
// .End();
auto& Switch() { return C<SelectorNode>("Switch"); }

// Stateful version `Switch` based on StatefulSelectorNode.
auto& StatefulSwitch() { return C<StatefulSelectorNode>("Switch*"); }

// Alias to If, for working alongs with Switch.
template <TCondition Condition, typename... ConditionArgs>
auto& Case(ConditionArgs&&... args) {
auto condition = make<Condition>(false, std::forward<ConditionArgs>(args)...);
return C<ConditionalRunNode>(std::move(condition), "Case");
}

// Case creates a ConditionalRunNode from lambda function.
auto& Case(ConditionNode::Checker checker) { return Case<ConditionNode>(checker); }

Expand Down
1 change: 1 addition & 0 deletions changelog
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

* Separates to bt.h and bt.cc.
* Remove template Clock, use std::chrono::steady_clock.
* Use printf instead of iostream

0.3.5
-----
Expand Down

0 comments on commit cbd7a87

Please sign in to comment.