Coder Social home page Coder Social logo

miso's Introduction

Hi there

Some older, but still fun projects:

Some articles I wrote over the time

When time permits I publish in ACCU's Overload magazine. Here are some of my articles:

Help with development

Keeping all the servers above online has its recurring monthly cost, all help is much appreciated.

miso's People

Contributors

fritzone avatar vainamon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

miso's Issues

Connect with unique objects

Hello!
Thanks for the useful nice little tool.
Consider the following example:

class other_class
{
public:
    other_class() = default;
    other_class(int v) : nr(v) {}

    void clicked()
    {
    	xil_printf("clicked:");
    }

    void clicked_again(int v)
    {
    	xil_printf("clicked_again:");
    }


private:

    int nr = 23;
};

class my_class
{
public:

    my_class(int f) : mf(f) {}

    void some_method()
    {
        emit click();
    }

    miso::signal<> click;
    int mf = 0;

};

int main() {
    my_class src(56);
    my_class src2(5656);

    other_class dst(4);
    other_class dst2(100500);

    miso::connect(src.click, std::bind(&other_class::clicked, dst));
    miso::connect(src2.click, std::bind(&other_class::clicked, dst2));

    src.some_method();

    return 0;
}

Slot clicked() will be called twice, which is unexpected, but is not surprise - because of static nature of SHT in connect_i function. For solve this I replace static SHT sh to

static std::unordered_map<std::string, SHT> sh_hash;    

auto sh_key = typeid(T).name() + std::string(typeid(FT).name()) + static_cast<std::ostringstream&>(
                        std::ostringstream().flush() << reinterpret_cast<void*>(&sholders)
                      ).str();

SHT &sh = sh_hash[sh_key];

But there is another problem if objects dynamically and repeatedly created, connected, emitted and destroyed: sh_hash would not be cleared and would be overfull sometime. That is why I changed connect_i function to:

        template<class T, class FT, class SHT>
        void connect_i(T &&f, std::vector<common_slot_base *> &sholders, bool active = true) {
            static std::unordered_map<std::string, SHT> sh_hash;
            
            auto sh_key = typeid(T).name() + std::string(typeid(FT).name()) + static_cast<std::ostringstream&>(
                        std::ostringstream().flush() << reinterpret_cast<void*>(&sholders)
                      ).str();

            SHT &sh = sh_hash[sh_key];

            func_and_bool<FT> fb{std::make_shared<FT>(std::forward<T>(f)), active, reinterpret_cast<void *>(&f)};
            bool already_in = false;
            bool active_status = active;
            
            std::for_each(sh.slots.begin(), sh.slots.end(),
                          [&](func_and_bool<FT> &s) { if (s.addr == fb.addr){s.active = active; already_in = true;} active_status |= s.active; });
            
            if (active_status) {
                if (!already_in) sh.slots.emplace_back(fb);
                if (std::find(sholders.begin(), sholders.end(), static_cast<common_slot_base *>(&sh)) == sholders.end()) {
                    sholders.push_back(&sh);
                }
            } else {
                remove(sholders.begin(), sholders.end(), static_cast<common_slot_base *>(&sh));
                sh_hash.erase(sh_key);
            }
        }

That's all.

Avoidable copies

Due to the "synchronous" nature of your library, the object could be copied less than currently. In the unmodified version a "larger" object gets copied up to 5 times.

With the patch applied the object gets copied only once (for the internal storage into call_args member. Maybe even this could be avoided if no "delayed" (or in Qt words "QueuedConnection") is implemented.

miso.h.patch.txt

Crash on disconnect when only one connect is called

Thanks you for your project for signal/slot handling! I am trying to make an app without Qt but with an event loop and your project respond very well to my need.

I found a crash when I am calling disconnect with only one connect previously called. In your example, if I uncomment the last main and if I change that lines:

auto lambdv = []() {std::cout << "lambvoid" << std::endl; };
miso::connect(src.click, lambdv);
//miso::connect(src.click, std::bind(&other_class::clicked, dst));
//miso::connect(src.click, global_void_method);
//miso::connect(src.click, f);

Then the output is:

mc_addre0x7ffdb71c77f0
lambvoid
Erreur de segmentation (core dumped)

Here the stack:

#0 0x000055555559e0c9 in miso::signal<>::emit_signal() (this=0x7fffffffd6b0) at /home/theo/Dev/miso/miso.h:126
#1 0x000055555559b2e6 in miso::signal<>::delayed_call<>(miso::internal::sequence<> const&) (this=0x7fffffffd6b0) at /home/theo/Dev/miso/miso.h:132
#2 0x000055555559a8ce in miso::signal<>::delayed_dispatch() (this=0x7fffffffd6b0) at /home/theo/Dev/miso/miso.h:136
#3 0x000055555559a0c3 in miso::internal::operator<< <my_class>(miso::internal::emitter<my_class>&&, miso::signal<>&) (e=..., s=...) at /home/theo/Dev/miso/miso.h:96
#4 0x0000555555599787 in my_class::some_method (this=0x7fffffffd6b0) at /home/theo/Dev/miso/miso.cpp:141
#5 0x0000555555593a75 in main (argc=1, argv=0x7fffffffd8a8) at /home/theo/Dev/miso/miso.cpp:200

I will trying to fix that but I need to look more closer your code :).

Async signal/slot

With QT you can put a QObject on a QThread and emit a signal to a slot for that QObject. This allows asynchronicity, since an object on Thread-A can emit a signal to a slot of an object on Thread-B.

Is there any way to replicate that functionality with Miso?

Currently I do the connection like this:
connect(objB->signal, std::bind(&ObjA::slot, objA, std::placeholders::_1));

But I don't know how to put the objB onto a separate thread nicely with Miso.

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.