"Fossies" - the Fresh Open Source Software Archive

Member "libsigc++-2.99.13/tests/test_bind.cc" (4 May 2019, 4143 Bytes) of package /linux/misc/libsigc++-2.99.13.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /* Copyright 2002 - 2016, The libsigc++ Development Team
    2  *  Assigned to public domain.  Use as you wish without restriction.
    3  */
    4 
    5 #include "testutilities.h"
    6 #include <sigc++/adaptors/bind.h>
    7 #include <sigc++/functors/slot.h>
    8 
    9 namespace
   10 {
   11 std::ostringstream result_stream;
   12 
   13 struct foo
   14 {
   15   // choose a type that can hold all return values
   16   using result_type = int;
   17 
   18   int operator()(int i)
   19   {
   20     result_stream << "foo(int " << i << ") ";
   21     return static_cast<int>(i > 0);
   22   }
   23 
   24   int operator()(int i, int j)
   25   {
   26     result_stream << "foo(int " << i << ", int " << j << ") ";
   27     return i + j;
   28   }
   29 
   30   int operator()(int i, int j, int k)
   31   {
   32     result_stream << "foo(int " << i << ", int " << j << ", int " << k << ") ";
   33     return 0;
   34   }
   35 };
   36 
   37 struct foo_void
   38 {
   39   using result_type = void;
   40 
   41   void operator()(int i) { result_stream << "foo_void(int " << i << ")"; }
   42 };
   43 
   44 int
   45 bar(int i, int j)
   46 {
   47   result_stream << "bar(int " << i << ", int " << j << ") ";
   48   return i + j;
   49 }
   50 
   51 bool
   52 simple(bool test)
   53 {
   54   result_stream << "simple(bool " << test << ") ";
   55   return test;
   56 }
   57 
   58 void
   59 egon(std::string& str)
   60 {
   61   result_stream << "egon(string '" << str << "')";
   62   str = "egon was here";
   63 }
   64 
   65 struct book : public sigc::trackable
   66 {
   67   explicit book(const std::string& name) : name_(name) {}
   68 
   69   // non-copyable:
   70   book(const book&) = delete;
   71   book& operator=(const book&) = delete;
   72 
   73   // non movable:
   74   book(book&&) = delete;
   75   book& operator=(book&&) = delete;
   76 
   77   std::string& get_name() { return name_; }
   78   operator std::string&() { return get_name(); }
   79 
   80 private:
   81   std::string name_;
   82 };
   83 
   84 } // end anonymous namespace
   85 
   86 int
   87 main(int argc, char* argv[])
   88 {
   89   auto util = TestUtilities::get_instance();
   90 
   91   if (!util->check_command_args(argc, argv))
   92     return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;
   93 
   94   // replacing bind1st, bind2nd
   95   result_stream << sigc::bind<0>(foo(), -12345)(5);
   96   util->check_result(result_stream, "foo(int -12345, int 5) -12340");
   97 
   98   result_stream << sigc::bind<1>(foo(), -12345)(5);
   99   util->check_result(result_stream, "foo(int 5, int -12345) -12340");
  100 
  101   // multiple
  102   sigc::bind(foo(), 1, 2)();
  103   util->check_result(result_stream, "foo(int 1, int 2) ");
  104 
  105   // bind from end
  106   sigc::bind<-1>(foo(), 4)(3);
  107   util->check_result(result_stream, "foo(int 3, int 4) ");
  108 
  109   sigc::bind(foo(), 4)(3);
  110   util->check_result(result_stream, "foo(int 3, int 4) ");
  111 
  112   // used together
  113   sigc::bind<0>(sigc::bind<0>(foo(), 7), 8)();
  114   util->check_result(result_stream, "foo(int 7, int 8) ");
  115 
  116   // void return
  117   sigc::bind(foo(), 9, 10)(11); // (only returned void if typeof() would be supported)
  118   util->check_result(result_stream, "foo(int 11, int 9, int 10) ");
  119 
  120   sigc::bind(foo_void(), 12)();
  121   util->check_result(result_stream, "foo_void(int 12)");
  122 
  123   // function pointer instead of functor
  124   sigc::bind (&bar, 13, 14)();
  125   util->check_result(result_stream, "bar(int 13, int 14) ");
  126 
  127   // method pointer instead of functor
  128   book test_book("otto");
  129   result_stream << sigc::bind<0>(&book::get_name, std::ref(test_book))();
  130   util->check_result(result_stream, "otto");
  131 
  132   // test return type of bind_functor::operator() overload with no arguments
  133   result_stream << sigc::bind(foo(), 15)();
  134   util->check_result(result_stream, "foo(int 15) 1");
  135 
  136   result_stream << sigc::bind(&simple, true)();
  137   util->check_result(result_stream, "simple(bool 1) 1");
  138 
  139   // test references
  140   std::string str("guest book");
  141   sigc::bind (&egon, std::ref(str))(); // Tell bind that it shall store a reference.
  142   result_stream
  143     << " "
  144     << str; // (This cannot be the default behaviour: just think about what happens if str dies!)
  145   util->check_result(result_stream, "egon(string 'guest book') egon was here");
  146 
  147   sigc::slot<void()> sl;
  148   {
  149     book guest_book("karl");
  150     sl = sigc::bind(&egon, std::ref(guest_book));
  151     sl();
  152     result_stream << " " << static_cast<std::string&>(guest_book);
  153     util->check_result(result_stream, "egon(string 'karl') egon was here");
  154   } // auto-disconnect
  155 
  156   sl();
  157   util->check_result(result_stream, "");
  158 
  159   return util->get_result_and_delete_instance() ? EXIT_SUCCESS : EXIT_FAILURE;
  160 }