Skip to content

Commit 0a217b4

Browse files
Replace web::json with signalr::value on public API (#12)
1 parent a8653a8 commit 0a217b4

File tree

13 files changed

+734
-129
lines changed

13 files changed

+734
-129
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ PS> .\submodules\vcpkg\bootstrap-vcpkg.bat
1111
PS> .\submodules\vcpkg\vcpkg.exe install cpprestsdk:x64-windows
1212
PS> mkdir build.release
1313
PS> cd build.release
14-
PS> cmake .. -A x64 -DCMAKE_TOOLCHAIN_FILE="..\submodules\vcpkg\scripts\buildsystems\vcpkg.cmake" -DCMAKE_BUILD_TYPE=Release
14+
PS> cmake .. -A x64 -DCMAKE_TOOLCHAIN_FILE="..\submodules\vcpkg\scripts\buildsystems\vcpkg.cmake" -DCMAKE_BUILD_TYPE=Release -DUSE_CPPRESTSDK=true
1515
PS> cmake --build . --config Release
1616
```
1717
Output will be in `build.release\bin\Release\`
@@ -24,7 +24,7 @@ $ ./submodules/vcpkg/bootstrap-vcpkg.sh
2424
$ ./submodules/vcpkg/vcpkg install cpprestsdk
2525
$ mkdir build.release
2626
$ cd build.release
27-
$ cmake .. -DCMAKE_TOOLCHAIN_FILE=../submodules/vcpkg/scripts/buildsystems/vcpkg.cmake -DCMAKE_BUILD_TYPE=Release
27+
$ cmake .. -DCMAKE_TOOLCHAIN_FILE=../submodules/vcpkg/scripts/buildsystems/vcpkg.cmake -DCMAKE_BUILD_TYPE=Release -DUSE_CPPRESTSDK=true
2828
$ cmake --build . --config Release
2929
```
3030
Output will be in `build.release/bin/`
@@ -37,7 +37,7 @@ $ ./submodules/vcpkg/bootstrap-vcpkg.sh
3737
$ ./submodules/vcpkg/vcpkg install cpprestsdk boost-system boost-chrono boost-thread
3838
$ mkdir build.release
3939
$ cd build.release
40-
$ cmake .. -DCMAKE_TOOLCHAIN_FILE=../submodules/vcpkg/scripts/buildsystems/vcpkg.cmake -DCMAKE_BUILD_TYPE=Release
40+
$ cmake .. -DCMAKE_TOOLCHAIN_FILE=../submodules/vcpkg/scripts/buildsystems/vcpkg.cmake -DCMAKE_BUILD_TYPE=Release -DUSE_CPPRESTSDK=true
4141
$ cmake --build . --config Release
4242
```
4343
Output will be in `build.release/bin/`

include/signalrclient/hub_connection.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
#include "_exports.h"
77
#include <memory>
88
#include <functional>
9-
#include "cpprest/json.h"
109
#include "connection_state.h"
1110
#include "trace_level.h"
1211
#include "log_writer.h"
1312
#include "signalr_client_config.h"
13+
#include "signalr_value.h"
1414

1515
namespace signalr
1616
{
@@ -19,7 +19,7 @@ namespace signalr
1919
class hub_connection
2020
{
2121
public:
22-
typedef std::function<void __cdecl (const web::json::value&)> method_invoked_handler;
22+
typedef std::function<void __cdecl (const signalr::value&)> method_invoked_handler;
2323

2424
SIGNALRCLIENT_API explicit hub_connection(const std::string& url, trace_level trace_level = trace_level::all,
2525
std::shared_ptr<log_writer> log_writer = nullptr);
@@ -42,9 +42,9 @@ namespace signalr
4242

4343
SIGNALRCLIENT_API void __cdecl on(const std::string& event_name, const method_invoked_handler& handler);
4444

45-
SIGNALRCLIENT_API void invoke(const std::string& method_name, const web::json::value& arguments = web::json::value::array(), std::function<void(const web::json::value&, std::exception_ptr)> callback = [](const web::json::value&, std::exception_ptr) {}) noexcept;
45+
SIGNALRCLIENT_API void invoke(const std::string& method_name, const signalr::value& arguments = signalr::value(), std::function<void(const signalr::value&, std::exception_ptr)> callback = [](const signalr::value&, std::exception_ptr) {}) noexcept;
4646

47-
SIGNALRCLIENT_API void send(const std::string& method_name, const web::json::value& arguments = web::json::value::array(), std::function<void(std::exception_ptr)> callback = [](std::exception_ptr) {}) noexcept;
47+
SIGNALRCLIENT_API void send(const std::string& method_name, const signalr::value& arguments = signalr::value(), std::function<void(std::exception_ptr)> callback = [](std::exception_ptr) {}) noexcept;
4848

4949
private:
5050
std::shared_ptr<hub_connection_impl> m_pImpl;

include/signalrclient/signalr_value.h

Lines changed: 91 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace signalr
1212
/**
1313
* An enum defining the types a signalr::value may be.
1414
*/
15-
enum class type
15+
enum class value_type
1616
{
1717
map,
1818
array,
@@ -28,20 +28,88 @@ namespace signalr
2828
class value
2929
{
3030
public:
31+
/**
32+
* Create an object representing a value_type::null value.
33+
*/
3134
value();
32-
value(int val);
35+
36+
/**
37+
* Create an object representing a default value for the given value_type.
38+
*/
39+
value(value_type t);
40+
41+
/**
42+
* Create an object representing a value_type::boolean with the given bool value.
43+
*/
44+
value(bool val);
45+
46+
/**
47+
* Create an object representing a value_type::float64 with the given double value.
48+
*/
3349
value(double val);
50+
51+
/**
52+
* Create an object representing a value_type::string with the given string value.
53+
*/
3454
value(const std::string& val);
55+
56+
/**
57+
* Create an object representing a value_type::string with the given string value.
58+
*/
59+
value(std::string&& val);
60+
61+
/**
62+
* Create an object representing a value_type::array with the given vector of value's.
63+
*/
3564
value(const std::vector<value>& val);
65+
66+
/**
67+
* Create an object representing a value_type::array with the given vector of value's.
68+
*/
69+
value(std::vector<value>&& val);
70+
71+
/**
72+
* Create an object representing a value_type::map with the given map of string-value's.
73+
*/
3674
value(const std::map<std::string, value>& map);
3775

76+
/**
77+
* Create an object representing a value_type::map with the given map of string-value's.
78+
*/
79+
value(std::map<std::string, value>&& map);
80+
81+
/**
82+
* Copies an existing value.
83+
*/
84+
value(const value& rhs);
85+
86+
/**
87+
* Moves an existing value.
88+
*/
89+
value(value&& rhs) noexcept;
90+
91+
/**
92+
* Cleans up the resources associated with the value.
93+
*/
94+
~value();
95+
96+
/**
97+
* Copies an existing value.
98+
*/
99+
value& operator=(const value& rhs);
100+
101+
/**
102+
* Moves an existing value.
103+
*/
104+
value& operator=(value&& rhs) noexcept;
105+
38106
/**
39107
* True if the object stored is a Key-Value pair.
40108
*/
41109
bool is_map() const;
42110

43111
/**
44-
* True if the object stored is double.
112+
* True if the object stored is a double.
45113
*/
46114
bool is_double() const;
47115

@@ -83,16 +151,33 @@ namespace signalr
83151
/**
84152
* Returns the stored object as an array of signalr::value's. This will throw if the underlying object is not a signalr::type::array.
85153
*/
86-
std::vector<value> as_array() const;
154+
const std::vector<value>& as_array() const;
87155

88156
/**
89157
* Returns the stored object as a map of property name to signalr::value. This will throw if the underlying object is not a signalr::type::map.
90158
*/
91-
std::map<std::string, value> as_map() const;
159+
const std::map<std::string, value>& as_map() const;
92160

93161
/**
94162
* Returns the signalr::type that represents the stored object.
95163
*/
96-
type type() const;
164+
value_type type() const;
165+
166+
private:
167+
value_type mType;
168+
169+
union storage
170+
{
171+
bool boolean;
172+
std::string string;
173+
std::vector<value> array;
174+
double number;
175+
std::map<std::string, value> map;
176+
177+
storage() {}
178+
~storage() {}
179+
};
180+
181+
storage mStorage;
97182
};
98183
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
set (SOURCES
1+
set (SOURCES
22
HubConnectionSample.cpp
33
)
44

55
include_directories(
66
../../include/signalrclient)
7-
7+
88
add_executable (HubConnectionSample ${SOURCES})
99

1010
target_link_libraries(HubConnectionSample signalrclient)

src/signalrclient/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ set (SOURCES
99
logger.cpp
1010
negotiate.cpp
1111
signalr_client_config.cpp
12+
signalr_value.cpp
1213
stdafx.cpp
1314
trace_log_writer.cpp
1415
transport.cpp

src/signalrclient/callback_manager.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33

44
#include "stdafx.h"
55
#include "callback_manager.h"
6+
#include <sstream>
67

78
namespace signalr
89
{
910
// dtor_clear_arguments will be passed when closing any pending callbacks when the `callback_manager` is
1011
// destroyed (i.e. in the dtor)
11-
callback_manager::callback_manager(const web::json::value& dtor_clear_arguments)
12+
callback_manager::callback_manager(const signalr::value& dtor_clear_arguments)
1213
: m_dtor_clear_arguments(dtor_clear_arguments)
1314
{ }
1415

@@ -18,7 +19,7 @@ namespace signalr
1819
}
1920

2021
// note: callback must not throw except for the `on_progress` callback which will never be invoked from the dtor
21-
std::string callback_manager::register_callback(const std::function<void(const web::json::value&)>& callback)
22+
std::string callback_manager::register_callback(const std::function<void(const signalr::value&)>& callback)
2223
{
2324
auto callback_id = get_callback_id();
2425

@@ -33,9 +34,9 @@ namespace signalr
3334

3435

3536
// invokes a callback and stops tracking it if remove callback set to true
36-
bool callback_manager::invoke_callback(const std::string& callback_id, const web::json::value& arguments, bool remove_callback)
37+
bool callback_manager::invoke_callback(const std::string& callback_id, const signalr::value& arguments, bool remove_callback)
3738
{
38-
std::function<void(const web::json::value& arguments)> callback;
39+
std::function<void(const signalr::value& arguments)> callback;
3940

4041
{
4142
std::lock_guard<std::mutex> lock(m_map_lock);
@@ -67,7 +68,7 @@ namespace signalr
6768
}
6869
}
6970

70-
void callback_manager::clear(const web::json::value& arguments)
71+
void callback_manager::clear(const signalr::value& arguments)
7172
{
7273
{
7374
std::lock_guard<std::mutex> lock(m_map_lock);

src/signalrclient/callback_manager.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,29 @@
77
#include <unordered_map>
88
#include <functional>
99
#include <mutex>
10-
#include "cpprest/json.h"
10+
#include "signalrclient/signalr_value.h"
1111

1212
namespace signalr
1313
{
1414
class callback_manager
1515
{
1616
public:
17-
explicit callback_manager(const web::json::value& dtor_error);
17+
explicit callback_manager(const signalr::value& dtor_error);
1818
~callback_manager();
1919

2020
callback_manager(const callback_manager&) = delete;
2121
callback_manager& operator=(const callback_manager&) = delete;
2222

23-
std::string register_callback(const std::function<void(const web::json::value&)>& callback);
24-
bool invoke_callback(const std::string& callback_id, const web::json::value& arguments, bool remove_callback);
23+
std::string register_callback(const std::function<void(const signalr::value&)>& callback);
24+
bool invoke_callback(const std::string& callback_id, const signalr::value& arguments, bool remove_callback);
2525
bool remove_callback(const std::string& callback_id);
26-
void clear(const web::json::value& arguments);
26+
void clear(const signalr::value& arguments);
2727

2828
private:
2929
std::atomic<int> m_id { 0 };
30-
std::unordered_map<std::string, std::function<void(const web::json::value&)>> m_callbacks;
30+
std::unordered_map<std::string, std::function<void(const signalr::value&)>> m_callbacks;
3131
std::mutex m_map_lock;
32-
const web::json::value m_dtor_clear_arguments;
32+
const signalr::value m_dtor_clear_arguments;
3333

3434
std::string get_callback_id();
3535
};

src/signalrclient/hub_connection.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,18 @@ namespace signalr
4747
return m_pImpl->on(event_name, handler);
4848
}
4949

50-
void hub_connection::invoke(const std::string& method_name, const web::json::value& arguments, std::function<void(const web::json::value&, std::exception_ptr)> callback) noexcept
50+
void hub_connection::invoke(const std::string& method_name, const signalr::value& arguments, std::function<void(const signalr::value&, std::exception_ptr)> callback) noexcept
5151
{
5252
if (!m_pImpl)
5353
{
54-
callback(web::json::value(), std::make_exception_ptr(signalr_exception("invoke() cannot be called on destructed hub_connection instance")));
54+
callback(signalr::value(), std::make_exception_ptr(signalr_exception("invoke() cannot be called on destructed hub_connection instance")));
5555
return;
5656
}
5757

5858
return m_pImpl->invoke(method_name, arguments, callback);
5959
}
6060

61-
void hub_connection::send(const std::string& method_name, const web::json::value& arguments, std::function<void(std::exception_ptr)> callback) noexcept
61+
void hub_connection::send(const std::string& method_name, const signalr::value& arguments, std::function<void(std::exception_ptr)> callback) noexcept
6262
{
6363
if (!m_pImpl)
6464
{

0 commit comments

Comments
 (0)