This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Oct 30th, 2012  |  syntax: C++  |  size: 1.57 KB  |  views: 26  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <iostream>
  2. #include <functional>
  3. #include <limits>
  4. #include <string>
  5. #include <cassert>
  6. #include <numeric>
  7. #include <vector>
  8.  
  9. template<typename T, typename Func, typename LogFunc>
  10. class InstanceClassifier
  11. {
  12.     std::vector<std::function<bool(const T&)>> functions;
  13.     LogFunc log_func;
  14. public:
  15.    
  16.     InstanceClassifier()
  17.     {}
  18.     InstanceClassifier(std::vector<std::function<bool(const T&)>> functions_):
  19.     functions(functions_)
  20.     {}
  21.     InstanceClassifier& operator = (const InstanceClassifier& other)
  22.     {
  23.         functions = other.functions;
  24.         return *this;
  25.     }
  26.     bool isSatisfied(const T& t) const
  27.     {
  28.         if (log_func)
  29.         {
  30.                         std::cout << ":)\n";
  31.             log_func(t);
  32.         }
  33.                 else
  34.                 {
  35.                      std::cout << ":(\n";
  36.                 }
  37.         assert(functions.size());// pointless to call without functions
  38.         if (!functions.size())
  39.         {
  40.             return true; //logic: all (all == 0 in this case :))  satisfy
  41.         }
  42.         std::vector<bool> results;// if editing remember remember vector<bool> is not a container
  43.         for (auto it = functions.cbegin(); it!=functions.cend(); ++it)
  44.             results.push_back( (*it) (t) );
  45.         return std::accumulate(results.cbegin(), results.cend(), true, std::multiplies<bool>());
  46.     }
  47. };
  48.  
  49. int main()
  50. {
  51.     std::function<void (int)> lambdada= [](int i){std::cout << i <<std::endl;};
  52.         InstanceClassifier<int, std::plus<bool>, decltype (lambdada) > even_positive( {[](int i){return i%2==0;}, [](int i){return i>0;} });
  53.         even_positive.isSatisfied(42);
  54. }
clone this paste RAW Paste Data