// Bug 167714 - Large performance leak in signal_impl::notify and sweep

// This program can be used for measuring execution time for many disconnections
// from a signal with many connected slots.

#include <sigc++/trackable.h>
#include <sigc++/signal.h>
#include <sigc++/connection.h>
#include <sigc++/functors/ptr_fun.h>
#include <iostream>
#include <stdlib.h>

int foo(int i)   {std::cout << "foo(int "<<i<<")" << std::endl; return 1;}
int bar(float i) {std::cout << "bar(float "<<i<<")" << std::endl; return 1;}

int main(int argc, char* argv[])
{
  if (argc != 3)
  {
    std::cout << "Usage: time " << argv[0]
              << " length-of-slot-list number-of-disconnections" << std::endl;
    return 1;
  }

  const int slot_list_len = atoi(argv[1]);
  const int n_disconnections = atoi(argv[2]);
  std::cout << slot_list_len << " slots in slot list, " << n_disconnections
            << " connections and disconnections" << std::endl;

  // signal
  sigc::signal<int,int> sig;

  // Create a slot list of the requested length.
  for (int i = 0; i < slot_list_len; i++)
  {
    sig.connect(sigc::ptr_fun1(&foo));
  }

  // Connect and disconnect a slot multiple times.
  sigc::connection con;
  for (int i = 0; i < n_disconnections; i++)
  {
    con = sig.connect(sigc::ptr_fun1(&bar));
    con.disconnect();
  }
  return 0;
}
