Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 8th, 2012  |  syntax: None  |  size: 1.34 KB  |  hits: 12  |  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. Is it possible and resonable to use Boost ICL instead of for_each in case of selecting a service for request?
  2. #include <boost/range/numeric.hpp>
  3. #include <boost/range/algorithm.hpp>
  4. #include <boost/range/adaptors.hpp>
  5.  
  6. typedef std::vector<std::string> args_t;
  7. typedef std::map<std::string, args_t> service_map;
  8.  
  9. struct applicable
  10. {
  11.     template <typename T>
  12.         applicable(const T& args) : _args(args) { }
  13.  
  14.     bool operator()(const service_map::value_type& svc) const
  15.     {
  16.         for (auto& a:_args)
  17.             if (svc.second.end() == boost::find(svc.second, a))
  18.                 return false;
  19.         return true;
  20.     }
  21.   private: args_t _args;
  22. };
  23.  
  24. int main(int argc, const char *argv[])
  25. {
  26.     using namespace boost::adaptors;
  27.     static const service_map services {
  28.         { "A", { "1","2","3" } },
  29.         { "B", { "2","3","4" } },
  30.         { "C", { "3","4","5" } },
  31.         { "D", { "4","5","6" } }
  32.     };
  33.  
  34.     const std::string label = "request: ";
  35.  
  36.     for (const auto& req : std::list<args_t> { {"5"}, {"4","3"}, {"1","3"} })
  37.     {
  38.         std::cout << boost::accumulate(req, label) << std::endl;
  39.  
  40.         for (auto& svc : services | filtered(applicable(req)))
  41.             std::cout << "applicable: " << svc.first << std::endl;
  42.     }
  43. }
  44.        
  45. request: 5
  46. applicable: C
  47. applicable: D
  48. request: 43
  49. applicable: B
  50. applicable: C
  51. request: 13
  52. applicable: A