value.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*! ========================================================================
  2. ** Extended Template and Library Test Suite
  3. ** Generic Value Test
  4. **
  5. ** Copyright (c) 2002 Adrian Bentley
  6. ** Copyright (c) 2010 Nikita Kitaev
  7. **
  8. ** This package is free software; you can redistribute it and/or
  9. ** modify it under the terms of the GNU General Public License as
  10. ** published by the Free Software Foundation; either version 2 of
  11. ** the License, or (at your option) any later version.
  12. **
  13. ** This package is distributed in the hope that it will be useful,
  14. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. ** General Public License for more details.
  17. **
  18. ** === N O T E S ===========================================================
  19. **
  20. ** ========================================================================= */
  21. #include <ETL/value>
  22. #include <stdio.h>
  23. using namespace etl;
  24. struct stupidv {
  25. float x, y;
  26. stupidv(float xin = 0, float yin = 0) : x(xin), y(yin) {}
  27. void print() const
  28. {
  29. printf("(x=%f,y=%f)\n", x, y);
  30. }
  31. };
  32. struct stupidp {
  33. float z, w;
  34. stupidp(float zin = 0, float win = 0) : z(zin), w(win) {}
  35. void print() const
  36. {
  37. printf("(z=%f,w=%f)\n", z, w);
  38. }
  39. };
  40. template <>
  41. class value_store_type<stupidp>
  42. {
  43. public:
  44. typedef stupidv value_type;
  45. };
  46. int main()
  47. {
  48. try {
  49. value v(10.0); // construction
  50. value v2; // default construct...
  51. // get type...
  52. printf("type of 10.0: %s\n", v.type().name());
  53. v2 = 1; // assignment
  54. printf("type of 1: %s\n", v2.type().name());
  55. // extract int test
  56. int *pi = value_cast<int>(&v2);
  57. printf("v2 is an int(%p)\n", pi);
  58. printf(" %d\n", value_cast<int>(v2));
  59. printf(" const version: %d\n", value_cast<int>(value(5)));
  60. v = 'c'; // assignment again...
  61. printf("type of c: %s\n", v.type().name());
  62. v2 = v; // value assignment
  63. printf("type of v2 , v: %s , %s\n", v2.type().name(), v.type().name());
  64. // random type test
  65. v = stupidv(0, 1);
  66. printf("type of vec: %s\n", v.type().name());
  67. // type cast with binary change test
  68. value_cast<stupidp>(&v)->print();
  69. value_cast<stupidv>(stupidp(5, 10)).print(); // copy test
  70. printf("type of v: %s\n", v.type().name());
  71. printf("type of v2: %s\n", v2.type().name());
  72. v.swap(v2);
  73. printf("type of v: %s\n", v.type().name());
  74. printf("type of v2: %s\n", v2.type().name());
  75. // test the exception throwing...
  76. value_cast<int>(stupidp(6, 66));
  77. } catch (const etl::bad_value_cast &e) {
  78. printf(" Exploded: %s\n", e.what());
  79. } catch (...) {
  80. printf(" Exploded\n");
  81. }
  82. return 0;
  83. }