Skip to content

Commit

Permalink
Add example async client
Browse files Browse the repository at this point in the history
  • Loading branch information
mezzode committed Nov 11, 2019
1 parent 05c9569 commit 72c2780
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 27 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
cmake_minimum_required (VERSION 3.8)

# Add source to this project's executable.
add_executable (exampleClient "exampleClient.cpp" "exampleClient.h" "client.cpp" "client.h")
add_executable (server "server.cpp" "server.h")
add_executable (exampleClient "exampleClient.cpp" "CustomClass.cpp" "CustomClass.h" "client.cpp" "client.h")
add_executable (exampleAsyncClient "exampleAsyncClient.cpp" "CustomClass.cpp" "CustomClass.h" "client.cpp" "client.h")
19 changes: 19 additions & 0 deletions CustomClass.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "CustomClass.h"

CustomClass::CustomClass(int a, int b) : counterA{ a }, counterB{ b } {}

void CustomClass::incrementA() {
++counterA;
}

void CustomClass::incrementB() {
++counterB;
}

int CustomClass::getA() {
return counterA;
}

int CustomClass::getB() {
return counterB;
}
9 changes: 7 additions & 2 deletions exampleClient.h → CustomClass.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class CustomClass {
public:
int counterA;

CustomClass(int a, int b);
CustomClass(int a = 0, int b = 0);
void incrementA();
void incrementB();
int getA();
Expand All @@ -15,4 +15,9 @@ class CustomClass {
template<class Archive>
friend void serialize(Archive& archive, CustomClass& m);
// external serialize function must be friend to serialize private attribute
};
};

template<class Archive>
void serialize(Archive& archive, CustomClass& m) {
archive(m.counterA, m.counterB);
}
52 changes: 52 additions & 0 deletions exampleAsyncClient.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "client.h"
#include "CustomClass.h"

using namespace std;

#include <future>

int main()
{
cout << "I am the async client." << endl;

const auto policy{ std::launch::async };

// sends

auto intSend{ std::async(policy, []() {
send("mykeyasync", 101);
}) };

auto strSend{ std::async(policy, []() {
send("she", string{ "ra" });
}) };

auto customSend{ std::async(policy, []() {
CustomClass custom{ 42, 81 };
custom.incrementA();
custom.incrementB();
send("mycustomclassasync", custom);
}) };

// gets

auto intGet{ std::async(policy, [&intSend]() {
intSend.get(); // await send success
cout << get<int>("mykeyasync") << endl;
}) };

auto strGet{ std::async(policy, [&strSend]() {
strSend.get(); // await send success
cout << get<string>("she") << endl;
}) };

auto customGet{ std::async(policy, [&customSend]() {
customSend.get(); // await send success
auto savedCustom{ get<CustomClass>("mycustomclassasync") };
savedCustom.incrementA();
savedCustom.incrementB();
cout << savedCustom.getA() << endl << savedCustom.getB() << endl;
}) };

getchar(); // wait before closing
}
25 changes: 1 addition & 24 deletions exampleClient.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "client.h"
#include "exampleClient.h"
#include "CustomClass.h"

using namespace std;

Expand All @@ -26,26 +26,3 @@ int main()

getchar(); // wait before closing
}

CustomClass::CustomClass(int a = 0, int b = 0) : counterA{ a }, counterB{ b } {}

void CustomClass::incrementA() {
++counterA;
}

void CustomClass::incrementB() {
++counterB;
}

int CustomClass::getA() {
return counterA;
}

int CustomClass::getB() {
return counterB;
}

template<class Archive>
void serialize(Archive& archive, CustomClass& m) {
archive(m.counterA, m.counterB);
}

0 comments on commit 72c2780

Please sign in to comment.