Skip to content
This repository has been archived by the owner on Mar 22, 2021. It is now read-only.

Commit

Permalink
Introduced ConnectionOfType interface to support different implementa…
Browse files Browse the repository at this point in the history
…tion of a connection.
  • Loading branch information
m-spiessens committed Mar 19, 2020
1 parent c1c4da3 commit e96fae5
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 30 deletions.
10 changes: 5 additions & 5 deletions conan/Flow/conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class FlowCore(ConanFile):
name = "Flow"
version = "1.8"
version = "1.9"
description = """Flow is a pipes and filters implementation tailored for microcontrollers.
It provides 3 base concepts: component, port and connection."""
url = "https://github.com/CynaraKrewe/Flow"
Expand All @@ -14,10 +14,10 @@ class FlowCore(ConanFile):
build_policy = "missing"

def source(self):
download("https://github.com/CynaraKrewe/Flow/archive/v1.8.zip", "Flow-1.8.zip")
unzip("Flow-1.8.zip")
shutil.move("Flow-1.8", "Flow")
os.unlink("Flow-1.8.zip")
download("https://github.com/CynaraKrewe/Flow/archive/v1.9.zip", "Flow-1.9.zip")
unzip("Flow-1.9.zip")
shutil.move("Flow-1.9", "Flow")
os.unlink("Flow-1.9.zip")

def build(self):
self.output.info("Nothing to build, this package provides sources.")
Expand Down
78 changes: 58 additions & 20 deletions include/flow/flow.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,42 @@ class Connection
}
};

template<typename Type>
class ConnectionOfType :
public Connection
{
public:
/**
* \brief Send an element over the connection.
*
* If the buffering capacity of the connection is full the given element is not added.
*
* \param element The element to be sent.
* \return The element was successfully sent.
*/
virtual bool send(const Type& element) = 0;

/**
* \brief Receive an element from the connection.
*
* \param element [output] The received element.
* The return value indicates whether the element is valid.
* \return An element was successfully received.
* Thus the element output parameter has a valid value.
*/
virtual bool receive(Type& element) = 0;

/**
* \brief Is an element available for receiving?
*/
virtual bool peek() const = 0;

/**
* \brief Is the connection full?
*/
virtual bool full() const = 0;
};

/**
* \brief A representation of a component.
*/
Expand Down Expand Up @@ -135,7 +171,8 @@ class Component
* \note Recommendation: use Flow::connect() instead.
*/
template<typename Type>
class ConnectionOfType: public Connection,
class ConnectionFIFO :
public ConnectionOfType<Type>,
protected Queue<Type>
{
public:
Expand All @@ -146,7 +183,7 @@ class ConnectionOfType: public Connection,
* \param receiver The input port to be connected.
* \param size The amount of elements the connection can buffer.
*/
ConnectionOfType(OutPort<Type>& sender, InPort<Type>& receiver,
ConnectionFIFO(OutPort<Type>& sender, InPort<Type>& receiver,
uint16_t size) :
Queue<Type>(size), receiver(receiver), sender(sender)
{
Expand All @@ -157,7 +194,7 @@ class ConnectionOfType: public Connection,
/**
* \brief Destructor.
*/
virtual ~ConnectionOfType()
virtual ~ConnectionFIFO()
{
sender.disconnect();
receiver.disconnect();
Expand All @@ -172,7 +209,7 @@ class ConnectionOfType: public Connection,
* \param element The element to be sent.
* \return The element was successfully sent.
*/
bool send(const Type& element)
bool send(const Type& element) final override
{
receiver.request();
return this->enqueue(element);
Expand All @@ -188,23 +225,23 @@ class ConnectionOfType: public Connection,
* \return An element was successfully received.
* Thus the element output parameter has a valid value.
*/
bool receive(Type& element)
bool receive(Type& element) final override
{
return this->dequeue(element);
}

/**
* \brief Is an element available for receiving?
*/
bool peek()
bool peek() const final override
{
return !this->isEmpty();
}

/**
* \brief Is the connection full?
*/
bool full()
bool full() const final override
{
return this->isFull();
}
Expand All @@ -220,17 +257,18 @@ class ConnectionOfType: public Connection,
* \note Recommendation: use Flow::connect() instead.
*/
template<typename Type>
class BiDirectionalConnectionOfType: public Connection
class BiDirectionalConnectionFIFO :
public Connection
{
public:
BiDirectionalConnectionOfType(InOutPort<Type>& portA, InOutPort<Type>& portB,
BiDirectionalConnectionFIFO(InOutPort<Type>& portA, InOutPort<Type>& portB,
uint16_t size) :
connectionA(ConnectionOfType<Type>(portA, portB, size)),
connectionB(ConnectionOfType<Type>(portB, portA, size))
connectionA(ConnectionFIFO<Type>(portA, portB, size)),
connectionB(ConnectionFIFO<Type>(portB, portA, size))
{}

private:
ConnectionOfType<Type> connectionA, connectionB;
ConnectionFIFO<Type> connectionA, connectionB;
};

/**
Expand Down Expand Up @@ -445,7 +483,7 @@ template<typename Type>
Connection* connect(OutPort<Type>& sender, InPort<Type>& receiver,
uint16_t size = 1)
{
return new ConnectionOfType<Type>(sender, receiver, size);
return new ConnectionFIFO<Type>(sender, receiver, size);
}

/**
Expand All @@ -461,7 +499,7 @@ Connection* connect(OutPort<Type>* sender, InPort<Type>& receiver,
{
assert(sender != nullptr);

return new ConnectionOfType<Type>(*sender, receiver, size);
return new ConnectionFIFO<Type>(*sender, receiver, size);
}

/**
Expand All @@ -477,7 +515,7 @@ Connection* connect(OutPort<Type>& sender, InPort<Type>* receiver,
{
assert(receiver != nullptr);

return new ConnectionOfType<Type>(sender, *receiver, size);
return new ConnectionFIFO<Type>(sender, *receiver, size);
}

/**
Expand All @@ -494,7 +532,7 @@ Connection* connect(OutPort<Type>* sender, InPort<Type>* receiver,
assert(sender != nullptr);
assert(receiver != nullptr);

return new ConnectionOfType<Type>(*sender, *receiver, size);
return new ConnectionFIFO<Type>(*sender, *receiver, size);
}

/**
Expand All @@ -508,7 +546,7 @@ template<typename Type>
Connection* connect(InOutPort<Type>& portA, InOutPort<Type>& portB,
uint16_t size = 1)
{
return new BiDirectionalConnectionOfType<Type>(portA, portB, size);
return new BiDirectionalConnectionFIFO<Type>(portA, portB, size);
}

/**
Expand All @@ -524,7 +562,7 @@ Connection* connect(InOutPort<Type>* portA, InOutPort<Type>& portB,
{
assert(portA != nullptr);

return new BiDirectionalConnectionOfType<Type>(*portA, portB, size);
return new BiDirectionalConnectionFIFO<Type>(*portA, portB, size);
}

/**
Expand All @@ -540,7 +578,7 @@ Connection* connect(InOutPort<Type>& portA, InOutPort<Type>* portB,
{
assert(portB != nullptr);

return new BiDirectionalConnectionOfType<Type>(portA, *portB, size);
return new BiDirectionalConnectionFIFO<Type>(portA, *portB, size);
}

/**
Expand All @@ -557,7 +595,7 @@ Connection* connect(InOutPort<Type>* portA, InOutPort<Type>* portB,
assert(portA != nullptr);
assert(portB != nullptr);

return new BiDirectionalConnectionOfType<Type>(*portA, *portB, size);
return new BiDirectionalConnectionFIFO<Type>(*portA, *portB, size);
}

} //namespace Flow
Expand Down
10 changes: 5 additions & 5 deletions source/flow_test/connection_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,21 @@

#include "flow_test/data.h"

using Flow::ConnectionOfType;
using Flow::ConnectionFIFO;
using Flow::OutPort;
using Flow::InPort;

#define CONNECTION_FIFO_SIZE 1000

TEST_GROUP(ConnectionOfType_TestBench)
{
ConnectionOfType<Data>* unitUnderTest;
ConnectionFIFO<Data>* unitUnderTest;
OutPort<Data> sender;
InPort<Data> receiver{&dummyComponent};

void setup()
{
unitUnderTest = new Flow::ConnectionOfType<Data>(sender,
unitUnderTest = new Flow::ConnectionFIFO<Data>(sender,
receiver, CONNECTION_FIFO_SIZE);
}

Expand Down Expand Up @@ -127,7 +127,7 @@ TEST(ConnectionOfType_TestBench, FullConnection)
CHECK(!unitUnderTest->receive(response));
}

static void producer(ConnectionOfType<Data>* _unitUnderTest,
static void producer(ConnectionFIFO<Data>* _unitUnderTest,
const unsigned long long count)
{
for (unsigned long long c = 0; c <= count; c++)
Expand All @@ -137,7 +137,7 @@ static void producer(ConnectionOfType<Data>* _unitUnderTest,
}
}

static void consumer(ConnectionOfType<Data>* _unitUnderTest,
static void consumer(ConnectionFIFO<Data>* _unitUnderTest,
const unsigned long long count, bool* success)
{
unsigned long long c = 0;
Expand Down

0 comments on commit e96fae5

Please sign in to comment.