io definitions and attach node added

This commit is contained in:
keldu.magnus 2020-08-23 15:47:14 +02:00
parent 681235d788
commit 8235be9cb3
9 changed files with 126 additions and 8 deletions

View File

@ -77,3 +77,7 @@ def format_iter(env,files):
format_iter(env,env.sources + env.headers)
env.Alias('format', env.format_actions)
env.Install('/usr/local/lib/', [env.library_shared, env.library_static])
env.Install('/usr/local/include/kelgin/', [env.headers])
env.Alias('install', '/usr/local/')

View File

@ -1 +0,0 @@
#include "driver/async-unix.h"

5
driver/io-unix.cpp Normal file
View File

@ -0,0 +1,5 @@
#include "driver/io-unix.h"
namespace gin {
}

9
driver/io-unix.h Normal file
View File

@ -0,0 +1,9 @@
#pragma once
#ifdef GIN_UNIX
#error "Don't include this directly"
#endif
namespace gin {
}

View File

@ -223,4 +223,10 @@ ConvertConveyorNodeBase::ConvertConveyorNodeBase(Own<ConveyorNode> &&dep)
void ConvertConveyorNodeBase::getResult(ErrorOrValue &err_or_val) {
getImpl(err_or_val);
}
void AttachConveyorNodeBase::getResult(ErrorOrValue& err_or_val){
if(child){
child->getResult(err_or_val);
}
}
} // namespace gin

View File

@ -40,7 +40,6 @@ protected:
Own<ConveyorNode> node;
ConveyorStorage *storage;
public:
ConveyorBase(Own<ConveyorNode> &&node_p,
ConveyorStorage *storage_p = nullptr);
@ -94,6 +93,12 @@ public:
*/
Conveyor<T> buffer(size_t limit = std::numeric_limits<size_t>::max());
/*
* This method just takes ownership of any supplied types
*/
template<typename... Args>
Conveyor<T> attach(Args&&... args);
// Waiting and resolving
ErrorOr<T> take();
@ -293,6 +298,26 @@ public:
}
};
class AttachConveyorNodeBase : public ConveyorNode {
public:
AttachConveyorNodeBase(Own<ConveyorNode>&& dep):
ConveyorNode(std::move(dep))
{}
void getResult(ErrorOrValue& err_or_val) override;
};
template<typename... Args>
class AttachConveyorNode : public AttachConveyorNodeBase {
private:
std::tuple<Args...> attached_data;
public:
AttachConveyorNode(Own<ConveyorNode>&& dep, Args&&... args):
AttachConveyorNodeBase(std::move(dep)),
attached_data{std::move(args...)}
{}
};
class ConvertConveyorNodeBase : public ConveyorNode {
public:
ConvertConveyorNodeBase(Own<ConveyorNode> &&dep);
@ -360,6 +385,13 @@ template <typename T> Conveyor<T> Conveyor<T>::buffer(size_t size) {
return Conveyor<T>{std::move(storage_node), storage_ptr};
}
template<typename T>
template<typename... Args>
Conveyor<T> Conveyor<T>::attach(Args&&... args){
Own<AttachConveyorNode<Args...>> attach_node = heap<AttachConveyorNode<Args...>>(std::move(node), std::move(args...));
return Conveyor<T>{std::move(attach_node), storage};
}
template <typename T>
Conveyor<T> Conveyor<T>::toConveyor(Own<ConveyorNode> &&node,
ConveyorStorage *storage) {

View File

@ -1,4 +1,4 @@
#pragma once
#include "io.h"
namespace gin {

55
source/io.h Normal file
View File

@ -0,0 +1,55 @@
#pragma once
#include "common.h"
#include "async.h"
namespace gin {
#ifdef GIN_UNIX
#define Fd int;
#endif
class InputStream {
public:
virtual ~InputStream() = default;
};
class OutputStream {
public:
virtual ~OutputStream() = default;
};
class IoStream : public InputStream, public OutputStream {
public:
virtual ~IoStream() = default;
};
class Server {
public:
virtual ~Server() = default;
virtual Own<IoStream> accept() = 0;
};
class NetworkAddress {
public:
virtual ~NetworkAddress() = default;
virtual Own<Server> listen() = 0;
virtual Own<IoStream> connect() = 0;
};
class AsyncIoProvider {
public:
virtual ~AsyncIoProvider() = default;
virtual Own<NetworkAddress> parse(const std::string&) = 0;
};
struct AsyncIoContext {
Own<AsyncIoProvider> io;
EventLoop& loop;
WaitScope& wait_scope;
};
AsyncIoContext setupAsyncIo();
}

View File

@ -114,10 +114,18 @@ GIN_TEST("Async Scheduling"){
auto feeder_conveyor = newConveyorAndFeeder<size_t>();
/*
* Attach node test data
*/
Own<size_t> counter = heap<size_t>();
size_t* ctr_ptr = counter.get();
*ctr_ptr = 0;
Conveyor<std::string> string_conveyor = feeder_conveyor.conveyor
.then([](size_t foo){
return std::to_string(foo);
.then([ctr_ptr](size_t foo){
return std::to_string(foo + ++(*ctr_ptr));
})
.attach(std::move(counter))
.buffer(10)
.then([](const std::string& value){
return value + std::string{"post"};
@ -138,17 +146,17 @@ GIN_TEST("Async Scheduling"){
GIN_EXPECT(!foo.isError(), "Return is an error: " + foo.error().message());
GIN_EXPECT(foo.isValue(), "Return is not a value");
GIN_EXPECT(foo.value() == (std::string{"pre"} + std::to_string(10) + std::string{"post"}), "Values is not pre10post, but " + foo.value());
GIN_EXPECT(foo.value() == (std::string{"pre"} + std::to_string(11) + std::string{"post"}), "Values is not pre11post, but " + foo.value());
ErrorOr<std::string> foo_20 = string_conveyor.take();
GIN_EXPECT(!foo_20.isError(), "Return is an error: " + foo_20.error().message());
GIN_EXPECT(foo_20.isValue(), "Return is not a value");
GIN_EXPECT(foo_20.value() == (std::string{"pre"} + std::to_string(20) + std::string{"post"}), "Values is not pre20post, but " + foo_20.value());
GIN_EXPECT(foo_20.value() == (std::string{"pre"} + std::to_string(22) + std::string{"post"}), "Values is not pre22post, but " + foo_20.value());
ErrorOr<std::string> foo_30 = string_conveyor.take();
GIN_EXPECT(!foo_30.isError(), "Return is an error: " + foo_30.error().message());
GIN_EXPECT(foo_30.isValue(), "Return is not a value");
GIN_EXPECT(foo_30.value() == (std::string{"pre"} + std::to_string(30) + std::string{"post"}), "Values is not pre30post, but " + foo_30.value());
GIN_EXPECT(foo_30.value() == (std::string{"pre"} + std::to_string(33) + std::string{"post"}), "Values is not pre33post, but " + foo_30.value());
}