basic_function.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* Copyright 2016-2017 Joaquin M Lopez Munoz.
  2. * Distributed under the Boost Software License, Version 1.0.
  3. * (See accompanying file LICENSE_1_0.txt or copy at
  4. * http://www.boost.org/LICENSE_1_0.txt)
  5. *
  6. * See http://www.boost.org/libs/poly_collection for library home page.
  7. */
  8. /* basic usage of boost::function_collection */
  9. #include <boost/poly_collection/function_collection.hpp>
  10. #include <functional>
  11. #include <memory>
  12. #include <random>
  13. #include <vector>
  14. #include "rolegame.hpp"
  15. int main()
  16. {
  17. //[basic_function_1
  18. std::vector<std::unique_ptr<sprite>> sprs;
  19. std::vector<std::string> msgs;
  20. std::vector<window> wnds;
  21. //]
  22. // populate sprs
  23. std::mt19937 gen{92748}; // some arbitrary random seed
  24. std::discrete_distribution<> rnd{{1,1,1}};
  25. for(int i=0;i<4;++i){ // assign each type with 1/3 probability
  26. switch(rnd(gen)){
  27. case 0: sprs.push_back(std::make_unique<warrior>(i));;break;
  28. case 1: sprs.push_back(std::make_unique<juggernaut>(i));break;
  29. case 2: sprs.push_back(std::make_unique<goblin>(i));break;
  30. }
  31. }
  32. // populate msgs
  33. msgs.push_back("\"stamina: 10,000\"");
  34. msgs.push_back("\"game over\"");
  35. // populate wnds
  36. wnds.emplace_back("pop-up 1");
  37. wnds.emplace_back("pop-up 2");
  38. //[basic_function_2
  39. //= #include <boost/poly_collection/function_collection.hpp>
  40. //= ...
  41. //=
  42. // function signature accepting std::ostream& and returning nothing
  43. using render_callback=void(std::ostream&);
  44. boost::function_collection<render_callback> c;
  45. //]
  46. //[basic_function_3
  47. //<-
  48. auto render_sprite=[](const sprite& s){
  49. //->
  50. //= auto render_sprite(const sprite& s){
  51. return [&](std::ostream& os){s.render(os);};
  52. }/*<-*/;/*->*/
  53. //<-
  54. auto render_message=[](const std::string& m){
  55. //->
  56. //= auto render_message(const std::string& m){
  57. return [&](std::ostream& os){os<<m;};
  58. }/*<-*/;/*->*/
  59. //<-
  60. auto render_window=[](const window& w){
  61. //->
  62. //= auto render_window(const window& w){
  63. return [&](std::ostream& os){w.display(os);};
  64. }/*<-*/;/*->*/
  65. //= ...
  66. //=
  67. for(const auto& ps:sprs)c.insert(render_sprite(*ps));
  68. for(const auto& m:msgs)c.insert(render_message(m));
  69. for(const auto& w:wnds)c.insert(render_window(w));
  70. //]
  71. //[basic_function_4
  72. const char* comma="";
  73. for(const auto& cbk:c){
  74. std::cout<<comma;
  75. cbk(std::cout);
  76. comma=",";
  77. }
  78. std::cout<<"\n";
  79. //]
  80. //[basic_function_5
  81. auto cbk=*c.begin();
  82. cbk(std::cout); // renders first element to std::cout
  83. std::function<render_callback> f=cbk;
  84. f(std::cout); // exactly the same
  85. //]
  86. //[basic_function_6
  87. //= *c.begin()=render_message("last minute message"); // compile-time error
  88. //]
  89. }