Coder Social home page Coder Social logo

Comments (8)

steve-lorimer avatar steve-lorimer commented on September 1, 2024 1

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.

jalcine avatar jalcine commented on September 1, 2024

Question: What's the point of having a std::vector if we can just use a std::basic_string?

from uvpp.

larroy avatar larroy commented on September 1, 2024

Makes no difference, we could have both methods, or just one with a generic pointer and size, but it's less secure

from uvpp.

aggsol avatar aggsol commented on September 1, 2024

Actually having a vector of widechars can be beneficial for using UTF-8/16 strings for CJK languages.

from uvpp.

jalcine avatar jalcine commented on September 1, 2024

Right but wouldn't you just use a std::wstring in that case?

from uvpp.

larroy avatar larroy commented on September 1, 2024

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.

jalcine avatar jalcine commented on September 1, 2024

@stevelorimer heh, should have caught that smell. That'd make a useful PR.

from uvpp.

larroy avatar larroy commented on September 1, 2024

👍

from uvpp.

Related Issues (5)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.