Updated sequence diagram example in readme

This commit is contained in:
Bartek Kryza
2022-12-15 01:15:37 +01:00
parent 9a20aae030
commit 4f95f426f1

View File

@@ -244,73 +244,78 @@ generates the following diagram (via PlantUML):
The following C++ code: The following C++ code:
```cpp ```cpp
#include <algorithm> #include <atomic>
#include <numeric> #include <functional>
#include <vector> #include <iostream>
#include <memory>
#include <string>
namespace clanguml { namespace clanguml {
namespace t20001 { namespace t20029 {
using encoder_function_t = std::function<std::string(std::string &&)>;
namespace detail { std::string encode_b64(std::string &&content) { return std::move(content); }
struct C {
auto add(int x, int y) { return x + y; }
};
}
class A { template <typename T> class Encoder : public T {
public: public:
A() {} bool send(std::string &&msg)
int add(int x, int y) { return m_c.add(x, y); }
int add3(int x, int y, int z)
{ {
std::vector<int> v; auto encoded = encode(std::move(msg));
v.push_back(x); return T::send(std::move(encoded));
v.push_back(y);
v.push_back(z);
auto res = add(v[0], v[1]) + v[2];
log_result(res);
return res;
} }
void log_result(int r) {} protected:
std::string encode(std::string &&msg) { return encode_b64(std::move(msg)); }
private: private:
detail::C m_c{}; encoder_function_t f_;
}; };
class B { template <typename T> class Retrier : public T {
public: public:
B(A &a) bool send(std::string &&msg)
: m_a{a}
{ {
std::string buffer{std::move(msg)};
int retryCount = 5;
while (retryCount--) {
if (T::send(buffer))
return true;
} }
int wrap_add(int x, int y) return false;
}
};
class ConnectionPool {
public:
void connect()
{ {
auto res = m_a.add(x, y); if (!is_connected_.load())
m_a.log_result(res); connect_impl();
return res;
} }
int wrap_add3(int x, int y, int z) bool send(const std::string &msg) { return true; }
{
auto res = m_a.add3(x, y, z);
m_a.log_result(res);
return res;
}
private: private:
A &m_a; void connect_impl() { is_connected_ = true; }
std::atomic<bool> is_connected_;
}; };
int tmain() int tmain()
{ {
A a; auto pool = std::make_shared<Encoder<Retrier<ConnectionPool>>>();
B b(a);
return b.wrap_add3(1, 2, 3); pool->connect();
for (std::string line; std::getline(std::cin, line);) {
if (!pool->send(std::move(line)))
break;
}
return 0;
} }
} }
} }
@@ -318,7 +323,7 @@ int tmain()
generates the following diagram (via PlantUML): generates the following diagram (via PlantUML):
![sequence_diagram_example](docs/test_cases/t20001_sequence.svg) ![sequence_diagram_example](docs/test_cases/t20029_sequence.svg)
### Package diagrams ### Package diagrams