Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ For more details with examples see the
[documentation](https://ripper37.github.io/libbase/).


### Building `libbase`
## Building `libbase`

#### Dependencies
### Dependencies

- [GLOG](https://github.com/google/glog)
- (Optional) [libcurl](https://curl.se/libcurl/) - for networking module
Expand All @@ -33,7 +33,7 @@ Dependencies either have to be already installed and CMake has to be able to
find them with `find_package()` or they can be resolved with `vcpkg`
(recommended).

#### Build with pre-existing `vcpkg` installation
### Build with pre-existing `vcpkg` installation

```bash
export VCPKG_ROOT=/path/to/vcpkg/
Expand All @@ -48,7 +48,7 @@ cmake -S . -B build -DCMAKE_TOOLCHAIN_FILE=/path/to/vcpkg/scripts/buildsystems/v
cmake --build build [-j <parallel_jobs>] [--config <Release|Debug>]
```

#### Build with internal `vcpkg` installation
### Build with internal `vcpkg` installation

If you don't have (or don't want to use) pre-existing `vcpkg` installation you
can ask `libbase` to set up its own internal `vcpkg` installation and use it
Expand All @@ -59,7 +59,7 @@ cmake -S . -B build -DLIBBASE_AUTO_VCPKG=1
cmake --build build [-j <parallel_jobs>] [--config <Release|Debug>]
```

#### Build with Conan
### Build with Conan

```bash
conan install . --build=missing -s build_type=Release [-o '&:option=value']
Expand All @@ -70,7 +70,7 @@ cmake --build --preset conan-release [-j <parallel_jobs>]
See `options` and `default_options` attributes in `conanfile.py` for available
build options.

#### Build manually
### Build manually

You can also manually build and install all required dependencies and then
simply build `libbase` with:
Expand All @@ -80,7 +80,7 @@ cmake -S . -B build
cmake --build build [-j <parallel_jobs>] [--config <Release|Debug>]
```

#### Running unit tests
### Running unit tests

Once you've built `libbase` you can run tests with:

Expand All @@ -92,13 +92,13 @@ For more details please refer to the
[building documentation](https://ripper37.github.io/libbase/master/getting_started/building.html).


### Using `libbase` in your project
## Using `libbase` in your project

You can install `libbase` in multiple ways - either by manually building it or
using a package manager like `vcpkg` (recommended) to do it for you. Once you've
installed it, you will need to find and link with it.

#### Install with `vcpkg`
### Install with `vcpkg`

To install `libbase` with `vcpkg` in the
[_manifest mode_](https://learn.microsoft.com/en-us/vcpkg/concepts/manifest-mode#manifest-files-in-projects)
Expand All @@ -124,7 +124,7 @@ in your terminal:
vcpkg install ripper37-libbase
```

#### Import into your CMake project with FetchContent
### Import into your CMake project with FetchContent

If you prefer to manually import your dependencies with CMake's `FetchContent`
you can import `libbase` in your project by adding this to your `CMakeFiles.txt`
Expand All @@ -147,7 +147,7 @@ FetchContent_MakeAvailable(libbase)
> `FetchContent` as well.


#### Manual
### Manual

Lastly you can simply build and install `libbase` manually. Please refer to the
[building section above](https://github.com/RippeR37/libbase?tab=readme-ov-file#building-libbase)
Expand All @@ -158,7 +158,7 @@ them all with:
cmake --install build_directory [--prefix <install_path_prefix>]
```

#### Add to your CMake project
### Add to your CMake project

Once you've installed `libbase` in your system, simply ask CMake to find it and
link all with:
Expand Down Expand Up @@ -202,6 +202,6 @@ or check out the example projects:
* Clang (13 through 18)
* MSVC (2022 19.43)

### License
## License

This project is licensed under the [MIT License](LICENSE).
3 changes: 2 additions & 1 deletion docs/features/networking.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ function to **asynchronously** cancel related request.

void PerformRequest(std::string resource_url) {
base::net::SimpleUrlLoader::DownloadUnbounded(
base::net::ResourceRequest{resource_url},
base::net::ResourceRequest{resource_url}.WithTimeout(base::Seconds(5)),
base::BindOnce(&OnResponse));
}

void OnResponse(base::net::ResourceResponse response) {
// Check response status and other metadata
}
Expand Down
2 changes: 1 addition & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ endif()

if(LIBBASE_BUILD_MODULE_WIN)
add_subdirectory(winapi_integration)
endif ()
endif()

if(LIBBASE_BUILD_MODULE_WX)
add_subdirectory(wxwidgets_integration)
Expand Down
32 changes: 10 additions & 22 deletions examples/networking/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,9 @@ void NetExampleGet() {

// Try to download and signal on finish
base::net::SimpleUrlLoader::DownloadUnbounded(
base::net::ResourceRequest{
"https://www.google.com/robots.txt",
base::net::kDefaultHeaders,
base::net::kNoPost,
true,
base::Seconds(5),
},
base::net::ResourceRequest{"https://www.google.com/robots.txt"}
.WithHeadersOnly()
.WithTimeout(base::Seconds(5)),
base::BindOnce([](base::net::ResourceResponse response) {
LogNetResponse(response);
}).Then(run_loop.QuitClosure()));
Expand All @@ -50,15 +46,11 @@ void NetExamplePost() {
base::RunLoop run_loop{};

// Try to download and signal on finish
const std::string data_str = "{\"key\": \"value\"}";
base::net::SimpleUrlLoader::DownloadUnbounded(
base::net::ResourceRequest{
"https://httpbin.org/post",
{"Content-Type: application/json"},
std::vector<uint8_t>{data_str.begin(), data_str.end()},
false,
base::Seconds(5),
},
base::net::ResourceRequest{"https://httpbin.org/post"}
.WithHeaders({"Content-Type: application/json"})
.WithPostData("{\"key\": \"value\"}")
.WithTimeout(base::Seconds(5)),
base::BindOnce([](base::net::ResourceResponse response) {
LogNetResponse(response);
}).Then(run_loop.QuitClosure()));
Expand Down Expand Up @@ -117,13 +109,9 @@ void NetExampleUrlRequest() {
base::RunLoop run_loop;

UrlRequestExampleUser url_request_user{run_loop.QuitClosure()};
url_request_user.Download(base::net::ResourceRequest{
"https://www.google.com/robots.txt",
base::net::kDefaultHeaders,
base::net::kNoPost,
false,
base::Seconds(5),
});
url_request_user.Download(
base::net::ResourceRequest{"https://www.google.com/robots.txt"}
.WithTimeout(base::Seconds(5)));

run_loop.Run();
}
Expand Down
4 changes: 1 addition & 3 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ if(LIBBASE_BUILD_MODULE_NET)
target_compile_definitions(libbase_net
PUBLIC
${LIBBASE_FEATURE_DEFINES}
INTERFACE
LIBBASE_MODULE_NET
)

Expand All @@ -163,6 +162,7 @@ if(LIBBASE_BUILD_MODULE_NET)
base/net/init.h
base/net/request_cancellation_token.cc
base/net/request_cancellation_token.h
base/net/resource_request.cc
base/net/resource_request.h
base/net/resource_response.h
base/net/result.h
Expand All @@ -188,7 +188,6 @@ if(LIBBASE_BUILD_MODULE_WIN)
target_compile_definitions(libbase_win
PUBLIC
${LIBBASE_FEATURE_DEFINES}
INTERFACE
LIBBASE_MODULE_WIN
)

Expand Down Expand Up @@ -228,7 +227,6 @@ if(LIBBASE_BUILD_MODULE_WX)
target_compile_definitions(libbase_wx
PUBLIC
${LIBBASE_FEATURE_DEFINES}
INTERFACE
LIBBASE_MODULE_WX
)

Expand Down
2 changes: 2 additions & 0 deletions src/base/message_loop/win/win_message_loop_attachment.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#if defined(LIBBASE_IS_WINDOWS)
#if defined(LIBBASE_MODULE_WIN)

#include <memory>

Expand Down Expand Up @@ -42,4 +43,5 @@ class WinMessageLoopAttachment {
} // namespace win
} // namespace base

#endif // defined(LIBBASE_MODULE_WIN)
#endif // defined(LIBBASE_IS_WINDOWS)
4 changes: 4 additions & 0 deletions src/base/message_loop/wx/wx_message_loop_attachment.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#if defined(LIBBASE_MODULE_WX)

#include <memory>

#include "base/message_loop/message_pump.h"
Expand Down Expand Up @@ -39,3 +41,5 @@ class WxMessageLoopAttachment {

} // namespace wx
} // namespace base

#endif // defined(LIBBASE_MODULE_WX)
4 changes: 4 additions & 0 deletions src/base/net/impl/net_thread.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#if defined(LIBBASE_MODULE_NET)

#include <map>
#include <memory>
#include <optional>
Expand Down Expand Up @@ -42,3 +44,5 @@ class NetThread {

} // namespace net
} // namespace base

#endif // defined(LIBBASE_MODULE_NET)
4 changes: 4 additions & 0 deletions src/base/net/impl/net_thread_impl.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#if defined(LIBBASE_MODULE_NET)

#include <atomic>
#include <map>
#include <mutex>
Expand Down Expand Up @@ -78,3 +80,5 @@ class NetThread::NetThreadImpl {

} // namespace net
} // namespace base

#endif // defined(LIBBASE_MODULE_NET)
4 changes: 4 additions & 0 deletions src/base/net/init.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#if defined(LIBBASE_MODULE_NET)

namespace base {
namespace net {

Expand All @@ -10,3 +12,5 @@ void Deinitialize();

} // namespace net
} // namespace base

#endif // defined(LIBBASE_MODULE_NET)
4 changes: 4 additions & 0 deletions src/base/net/request_cancellation_token.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#if defined(LIBBASE_MODULE_NET)

#include <cstddef>

namespace base {
Expand All @@ -16,3 +18,5 @@ struct RequestCancellationToken {

} // namespace net
} // namespace base

#endif // defined(LIBBASE_MODULE_NET)
83 changes: 83 additions & 0 deletions src/base/net/resource_request.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include "base/net/resource_request.h"

namespace base {
namespace net {

ResourceRequest ResourceRequest::WithHeaders(
std::vector<std::string> new_headers) const& {
return ResourceRequest{*this}.WithHeaders(std::move(new_headers));
}

ResourceRequest&& ResourceRequest::WithHeaders(
std::vector<std::string> new_headers) && {
headers = std::move(new_headers);
return std::move(*this);
}

ResourceRequest ResourceRequest::WithPostData(
std::vector<uint8_t> new_post_data) const& {
return ResourceRequest{*this}.WithPostData(std::move(new_post_data));
}

ResourceRequest&& ResourceRequest::WithPostData(
std::vector<uint8_t> new_post_data) && {
post_data = std::move(new_post_data);
return std::move(*this);
}

ResourceRequest ResourceRequest::WithPostData(
std::string_view new_post_data) const& {
return ResourceRequest{*this}.WithPostData(std::move(new_post_data));
}

ResourceRequest&& ResourceRequest::WithPostData(
std::string_view new_post_data) && {
post_data = std::vector<uint8_t>(new_post_data.begin(), new_post_data.end());
return std::move(*this);
}

ResourceRequest ResourceRequest::WithHeadersOnly(bool new_headers_only) const& {
return ResourceRequest{*this}.WithHeadersOnly(std::move(new_headers_only));
}

ResourceRequest&& ResourceRequest::WithHeadersOnly(bool new_headers_only) && {
headers_only = std::move(new_headers_only);
return std::move(*this);
}

ResourceRequest ResourceRequest::WithTimeout(
base::TimeDelta new_timeout) const& {
return ResourceRequest{*this}.WithTimeout(std::move(new_timeout));
}

ResourceRequest&& ResourceRequest::WithTimeout(base::TimeDelta new_timeout) && {
timeout = std::move(new_timeout);
return std::move(*this);
}

ResourceRequest ResourceRequest::WithConnectTimeout(
base::TimeDelta new_connect_timeout) const& {
return ResourceRequest{*this}.WithConnectTimeout(
std::move(new_connect_timeout));
}

ResourceRequest&& ResourceRequest::WithConnectTimeout(
base::TimeDelta new_connect_timeout) && {
connect_timeout = std::move(new_connect_timeout);
return std::move(*this);
}

ResourceRequest ResourceRequest::WithFollowRedirects(
bool new_follow_redirects) const& {
return ResourceRequest{*this}.WithFollowRedirects(
std::move(new_follow_redirects));
}

ResourceRequest&& ResourceRequest::WithFollowRedirects(
bool new_follow_redirects) && {
follow_redirects = std::move(new_follow_redirects);
return std::move(*this);
}

} // namespace net
} // namespace base
Loading
Loading