mirror of
https://github.com/Naios/continuable.git
synced 2026-02-09 11:16:40 +08:00
test
This commit is contained in:
parent
fd2aa5d5dc
commit
840fccf428
@ -21,7 +21,7 @@
|
|||||||
|
|
||||||
#include "Callback.h"
|
#include "Callback.h"
|
||||||
|
|
||||||
template <typename _CTy>
|
template <typename... _ATy>
|
||||||
struct Continuable;
|
struct Continuable;
|
||||||
|
|
||||||
template <typename... _ATy>
|
template <typename... _ATy>
|
||||||
@ -39,7 +39,7 @@ public:
|
|||||||
: _callback_insert(std::forward<ForwardFunction>(callback_insert)) { }
|
: _callback_insert(std::forward<ForwardFunction>(callback_insert)) { }
|
||||||
|
|
||||||
template <typename _CTy>
|
template <typename _CTy>
|
||||||
Continuable<Callback<_ATy...>>& then(_CTy&& callback)
|
Continuable<Callback<_ATy...>>& then(_CTy&&)
|
||||||
{
|
{
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@ -66,6 +66,14 @@ namespace detail
|
|||||||
|
|
||||||
} // detail
|
} // detail
|
||||||
|
|
||||||
|
/// Wraps a functional object which expects a r-value callback as argument into a continuable.
|
||||||
|
/// The callable is invoked when the continuable shall continue.
|
||||||
|
/// For example:
|
||||||
|
/// make_continuable([](Callback<int>&& callback)
|
||||||
|
/// {
|
||||||
|
/// /* Continue here */
|
||||||
|
/// callback(5);
|
||||||
|
/// });
|
||||||
template <typename _FTy>
|
template <typename _FTy>
|
||||||
inline auto make_continuable(_FTy&& functional)
|
inline auto make_continuable(_FTy&& functional)
|
||||||
-> decltype(typename detail::continuable_factory_t<_FTy>::CreateFrom(std::declval<_FTy>()))
|
-> decltype(typename detail::continuable_factory_t<_FTy>::CreateFrom(std::declval<_FTy>()))
|
||||||
|
|||||||
@ -8,6 +8,7 @@
|
|||||||
|
|
||||||
#include "functional_unwrap.hpp"
|
#include "functional_unwrap.hpp"
|
||||||
|
|
||||||
|
/*
|
||||||
class fluent_step
|
class fluent_step
|
||||||
{
|
{
|
||||||
bool released;
|
bool released;
|
||||||
@ -82,6 +83,7 @@ struct ProtoContinueable
|
|||||||
return ProtoContinueable();
|
return ProtoContinueable();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
*/
|
||||||
|
|
||||||
void do_export();
|
void do_export();
|
||||||
|
|
||||||
|
|||||||
158
test.cpp
158
test.cpp
@ -1,6 +1,4 @@
|
|||||||
|
|
||||||
#include "fluent++.hpp"
|
|
||||||
|
|
||||||
#include "Callback.h"
|
#include "Callback.h"
|
||||||
#include "WeakCallbackContainer.h"
|
#include "WeakCallbackContainer.h"
|
||||||
#include "Continuable.h"
|
#include "Continuable.h"
|
||||||
@ -18,45 +16,11 @@ enum SpellCastResult
|
|||||||
SPELL_FAILED_ALREADY_BEING_TAMED = 5
|
SPELL_FAILED_ALREADY_BEING_TAMED = 5
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
// Continuable<Callback<SpellCastResult>>
|
||||||
ProtoContinueable ProtoCastSpell(int id)
|
int CastSpell(int id)
|
||||||
{
|
{
|
||||||
std::cout << "Cast " << id << std::endl;
|
return 1;
|
||||||
|
make_continuable([=](Callback<SpellCastResult>&& callback)
|
||||||
// on success call true
|
|
||||||
return ProtoContinueable();
|
|
||||||
}
|
|
||||||
|
|
||||||
ProtoContinueable ProtoMoveTo(int point)
|
|
||||||
{
|
|
||||||
std::cout << "Move to point " << point << std::endl;
|
|
||||||
|
|
||||||
// on success call true
|
|
||||||
return ProtoContinueable();
|
|
||||||
}
|
|
||||||
|
|
||||||
void ProtoCastSpell(int id, Callback<SpellCastResult> const& callback)
|
|
||||||
{
|
|
||||||
std::cout << "Cast " << id << std::endl;
|
|
||||||
|
|
||||||
// on success call the callback with SPELL_FAILED_SUCCESS
|
|
||||||
callback(SPELL_FAILED_SUCCESS);
|
|
||||||
|
|
||||||
|
|
||||||
void ProtoMoveTo(int point, Callback<bool> const& callback)
|
|
||||||
{
|
|
||||||
std::cout << "Move to point " << point << std::endl;
|
|
||||||
|
|
||||||
// on success call true
|
|
||||||
callback(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
Continuable<Callback<SpellCastResult>> CastSpell(int id)
|
|
||||||
{
|
|
||||||
return make_continuable([=](Callback<SpellCastResult>&& callback)
|
|
||||||
{
|
{
|
||||||
std::cout << "Cast " << id << std::endl;
|
std::cout << "Cast " << id << std::endl;
|
||||||
|
|
||||||
@ -67,122 +31,16 @@ Continuable<Callback<SpellCastResult>> CastSpell(int id)
|
|||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
/*
|
|
||||||
make_waterfall<Callback<SpellCastResult>>()
|
|
||||||
// .then(std::bind(&CastSpell, 71382, std::placeholders::_1))
|
|
||||||
.then([](SpellCastResult result, Callback<bool> const& callback)
|
|
||||||
{
|
|
||||||
ProtoMoveTo(1, callback);
|
|
||||||
})
|
|
||||||
.then([](bool success)
|
|
||||||
{
|
|
||||||
// Do something final
|
|
||||||
std::cout << "finish everything" << std::endl;
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
auto cabt = []()
|
|
||||||
{
|
|
||||||
// Do something
|
|
||||||
return ProtoMoveTo(2);
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef Callback<bool> cbd1;
|
|
||||||
typedef WeakCallback<int> cbd2;
|
|
||||||
typedef SharedCallback<std::string> cbd3;
|
|
||||||
|
|
||||||
cbd1 _cbd1;
|
|
||||||
cbd2 _cbd2;
|
|
||||||
cbd3 _cbd3;
|
|
||||||
|
|
||||||
auto _test_make_shared_callback = make_shared_callback([](bool)
|
|
||||||
{
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
Callback<> weak_cb_test;
|
|
||||||
|
|
||||||
{
|
|
||||||
WeakCallbackContainer callback;
|
|
||||||
|
|
||||||
// Some tests
|
|
||||||
ProtoCastSpell(22872)
|
|
||||||
.weak(callback)
|
|
||||||
.then([](bool success)
|
|
||||||
{
|
|
||||||
if (success)
|
|
||||||
{
|
|
||||||
// do something
|
|
||||||
}
|
|
||||||
|
|
||||||
// Do something
|
|
||||||
return ProtoMoveTo(2);
|
|
||||||
})
|
|
||||||
.then([]
|
|
||||||
{
|
|
||||||
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
std::shared_ptr<int> dealloc_test(new int{2}, [](int* me)
|
|
||||||
{
|
|
||||||
std::cout << "dealloc ok" << std::endl;
|
|
||||||
delete me;
|
|
||||||
});
|
|
||||||
|
|
||||||
auto const cb_void = callback([dealloc_test]
|
|
||||||
{
|
|
||||||
std::cout << "huhu i'm a..." << std::endl;
|
|
||||||
|
|
||||||
auto const cp = dealloc_test;
|
|
||||||
});
|
|
||||||
|
|
||||||
dealloc_test.reset();
|
|
||||||
|
|
||||||
auto const cb_bool = callback([](bool success)
|
|
||||||
{
|
|
||||||
std::cout << "...weak wrapped callback!" << std::endl;
|
|
||||||
});
|
|
||||||
|
|
||||||
weak_cb_test = callback([]
|
|
||||||
{
|
|
||||||
std::cout << "huhu i'm crashsafe (you wont see me)!" << std::endl;
|
|
||||||
std::logic_error("bad logic");
|
|
||||||
});
|
|
||||||
|
|
||||||
cb_void();
|
|
||||||
|
|
||||||
int i = 0;
|
|
||||||
++i;
|
|
||||||
|
|
||||||
cb_bool(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
// This will never be executed because the CallbackContainer was deallocated and its weak callback proxies are crash safe.
|
|
||||||
weak_cb_test();
|
|
||||||
|
|
||||||
auto weak_2 = make_shared_callback([]
|
|
||||||
{
|
|
||||||
std::cout << "huhu" << std::endl;
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
auto wrapped = make_weak_wrapped_callback(weak_2);
|
|
||||||
auto wrapped2 = make_weak_wrapped_callback(WeakCallback<>(weak_2));
|
|
||||||
wrapped();
|
|
||||||
wrapped2();
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
typedef Continuable<Callback<bool>> cont123;
|
typedef Continuable<Callback<bool>> cont123;
|
||||||
|
|
||||||
// typedef Continuable<Callback<bool>>::type myty1;
|
// typedef Continuable<Callback<bool>>::type myty1;
|
||||||
// typedef Continuable<Callback<bool>, float>::type myty2;
|
// typedef Continuable<Callback<bool>, float>::type myty2;
|
||||||
|
|
||||||
// Continuable<Callback<SpellCastResult>>
|
// Continuable<Callback<SpellCastResult>>
|
||||||
CastSpell(63362)
|
CastSpell(63362);
|
||||||
.then([](SpellCastResult result)
|
|
||||||
|
Continuable<Callback<SpellCastResult>> spell;
|
||||||
|
spell.then([](SpellCastResult result)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user