Comments (8)
When I submitted this issue I wasn't talking about changing the signatures of the various overloads or whether to add or remove any, I was alluding to code duplication.
Currently this is the code:
bool write(const char* buf, int len, std::function<void(error)> callback)
{
uv_buf_t bufs[] = { uv_buf_t { const_cast<char*>(buf), static_cast<size_t>(len) } };
callbacks::store(handle<HANDLE_T>::get()->data, uvpp::internal::uv_cid_write, callback);
return uv_write(new uv_write_t, handle<HANDLE_T>::template get<uv_stream_t>(), bufs, 1, [](uv_write_t* req, int status) {
callbacks::invoke<decltype(callback)>(req->handle->data, uvpp::internal::uv_cid_write, error(status));
delete req;
}) == 0;
}
bool write(const std::string& buf, std::function<void(error)> callback)
{
uv_buf_t bufs[] = { uv_buf_t { const_cast<char*>(buf.c_str()), buf.length()} };
callbacks::store(handle<HANDLE_T>::get()->data, uvpp::internal::uv_cid_write, callback);
return uv_write(new uv_write_t, handle<HANDLE_T>::template get<uv_stream_t>(), bufs, 1, [](uv_write_t* req, int status) {
callbacks::invoke<decltype(callback)>(req->handle->data, uvpp::internal::uv_cid_write, error(status));
delete req;
}) == 0;
}
bool write(const std::vector<char>& buf, std::function<void(error)> callback)
{
uv_buf_t bufs[] = { uv_buf_t { const_cast<char*>(&buf[0]), buf.size() } };
callbacks::store(handle<HANDLE_T>::get()->data, uvpp::internal::uv_cid_write, callback);
return uv_write(new uv_write_t, handle<HANDLE_T>::template get<uv_stream_t>(), bufs, 1, [](uv_write_t* req, int status) {
callbacks::invoke<decltype(callback)>(req->handle->data, uvpp::internal::uv_cid_write, error(status));
delete req;
}) == 0;
}
and I think it would be better written like this:
bool write(const char* buf, int len, std::function<void(error)> callback)
{
uv_buf_t bufs[] = { uv_buf_t { const_cast<char*>(buf), static_cast<size_t>(len) } };
callbacks::store(handle<HANDLE_T>::get()->data, uvpp::internal::uv_cid_write, callback);
return uv_write(new uv_write_t, handle<HANDLE_T>::template get<uv_stream_t>(), bufs, 1, [](uv_write_t* req, int status) {
callbacks::invoke<decltype(callback)>(req->handle->data, uvpp::internal::uv_cid_write, error(status));
delete req;
}) == 0;
}
bool write(const std::string& buf, std::function<void(error)> callback)
{
return write(buf.data(), buf.size(), callback);
}
bool write(const std::vector<char>& buf, std::function<void(error)> callback)
{
return write(&buf[0], buf.size(), callback);
}
Note also that you're passing the callback by value, so it would probably be better to use perfect forwarding too:
bool write(const char* buf, int len, std::function<void(error)>&& callback)
{
uv_buf_t bufs[] = { uv_buf_t { const_cast<char*>(buf), static_cast<size_t>(len) } };
callbacks::store(handle<HANDLE_T>::get()->data, uvpp::internal::uv_cid_write, callback);
return uv_write(new uv_write_t, handle<HANDLE_T>::template get<uv_stream_t>(), bufs, 1, [](uv_write_t* req, int status) {
callbacks::invoke<decltype(callback)>(req->handle->data, uvpp::internal::uv_cid_write, error(status));
delete req;
}) == 0;
}
bool write(const std::string& buf, std::function<void(error)>&& callback)
{
return write(buf.data(), buf.size(), std::forward<std::function<void(error)>>(callback));
}
bool write(const std::vector<char>& buf, std::function<void(error)>&& callback)
{
return write(&buf[0], buf.size(), std::forward<std::function<void(error)>>(callback));
}
from uvpp.
Question: What's the point of having a std::vector
if we can just use a std::basic_string
?
from uvpp.
Makes no difference, we could have both methods, or just one with a generic pointer and size, but it's less secure
from uvpp.
Actually having a vector of widechars can be beneficial for using UTF-8/16 strings for CJK languages.
from uvpp.
Right but wouldn't you just use a std::wstring
in that case?
from uvpp.
I think that's problematic, the best way in my opinion is to always have utf-8 encoded strings, so either plain std::string or vector wchar_t and thus wstring shouldn't be used in public interfaces, the size of wchar_t is implementation defined.
http://en.wikipedia.org/wiki/Wide_character
from uvpp.
@stevelorimer heh, should have caught that smell. That'd make a useful PR.
from uvpp.
👍
from uvpp.
Related Issues (5)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from uvpp.