factory.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. Copyright 2007 Tobias Schwinger
  3. Copyright 2019 Glen Joseph Fernandes
  4. (glenjofe@gmail.com)
  5. Distributed under the Boost Software License, Version 1.0.
  6. (http://www.boost.org/LICENSE_1_0.txt)
  7. */
  8. #include <boost/functional/factory.hpp>
  9. #include <boost/core/lightweight_test.hpp>
  10. #include <boost/smart_ptr/scoped_ptr.hpp>
  11. class sum {
  12. public:
  13. explicit sum(int i0 = 0, int i1 = 0, int i2 = 0, int i3 = 0,
  14. int i4 = 0, int i5 = 0, int i6 = 0, int i7 = 0,
  15. int i8 = 0, int i9 = 0)
  16. : value_(i0 + i1 + i2 + i3 + i4 + i5 + i6 + i7 + i8 + i9) { }
  17. int get() const {
  18. return value_;
  19. }
  20. private:
  21. int value_;
  22. };
  23. int main()
  24. {
  25. boost::factory<sum*> x;
  26. int a = 1;
  27. int b = 2;
  28. int c = 3;
  29. int d = 4;
  30. int e = 5;
  31. int f = 6;
  32. int g = 7;
  33. int h = 8;
  34. int i = 9;
  35. int j = 10;
  36. {
  37. boost::scoped_ptr<sum> s(x());
  38. BOOST_TEST(s->get() == 0);
  39. }
  40. {
  41. boost::scoped_ptr<sum> s(x(a));
  42. BOOST_TEST(s->get() == 1);
  43. }
  44. {
  45. boost::scoped_ptr<sum> s(x(a, b));
  46. BOOST_TEST(s->get() == 3);
  47. }
  48. {
  49. boost::scoped_ptr<sum> s(x(a, b, c));
  50. BOOST_TEST(s->get() == 6);
  51. }
  52. {
  53. boost::scoped_ptr<sum> s(x(a, b, c, d));
  54. BOOST_TEST(s->get() == 10);
  55. }
  56. {
  57. boost::scoped_ptr<sum> s(x(a, b, c, d, e));
  58. BOOST_TEST(s->get() == 15);
  59. }
  60. {
  61. boost::scoped_ptr<sum> s(x(a, b, c, d, e, f));
  62. BOOST_TEST(s->get() == 21);
  63. }
  64. {
  65. boost::scoped_ptr<sum> s(x(a, b, c, d, e, f, g));
  66. BOOST_TEST(s->get() == 28);
  67. }
  68. {
  69. boost::scoped_ptr<sum> s(x(a, b, c, d, e, f, g, h));
  70. BOOST_TEST(s->get() == 36);
  71. }
  72. {
  73. boost::scoped_ptr<sum> s(x(a, b, c, d, e, f, g, h, i));
  74. BOOST_TEST(s->get() == 45);
  75. }
  76. {
  77. boost::scoped_ptr<sum> s(x(a, b, c, d, e, f, g, h, i, j));
  78. BOOST_TEST(s->get() == 55);
  79. }
  80. return boost::report_errors();
  81. }