enum_maker.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright (c) 2003 Daniel Wallin and Arvid Norberg
  2. // Permission is hereby granted, free of charge, to any person obtaining a
  3. // copy of this software and associated documentation files (the "Software"),
  4. // to deal in the Software without restriction, including without limitation
  5. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  6. // and/or sell copies of the Software, and to permit persons to whom the
  7. // Software is furnished to do so, subject to the following conditions:
  8. // The above copyright notice and this permission notice shall be included
  9. // in all copies or substantial portions of the Software.
  10. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
  11. // ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12. // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13. // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
  14. // SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  15. // ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  16. // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
  18. // OR OTHER DEALINGS IN THE SOFTWARE.
  19. #ifndef LUABIND_ENUM_MAKER_HPP_INCLUDED
  20. #define LUABIND_ENUM_MAKER_HPP_INCLUDED
  21. #include <vector>
  22. #include <string>
  23. #include <luabind/config.hpp>
  24. #include <luabind/detail/class_rep.hpp>
  25. namespace luabind
  26. {
  27. struct value;
  28. struct value_vector : public std::vector<value>
  29. {
  30. // a bug in intel's compiler forces us to declare these constructors explicitly.
  31. value_vector();
  32. virtual ~value_vector();
  33. value_vector(const value_vector& v);
  34. value_vector& operator,(const value& rhs);
  35. };
  36. struct value
  37. {
  38. friend class std::vector<value>;
  39. template<class T>
  40. value(const char* name, T v)
  41. : name_(name)
  42. , val_(v)
  43. {}
  44. const char* name_;
  45. int val_;
  46. value_vector operator,(const value& rhs) const
  47. {
  48. value_vector v;
  49. v.push_back(*this);
  50. v.push_back(rhs);
  51. return v;
  52. }
  53. private:
  54. value() {}
  55. };
  56. inline value_vector::value_vector()
  57. : std::vector<value>()
  58. {
  59. }
  60. inline value_vector::~value_vector() {}
  61. inline value_vector::value_vector(const value_vector& rhs)
  62. : std::vector<value>(rhs)
  63. {
  64. }
  65. inline value_vector& value_vector::operator,(const value& rhs)
  66. {
  67. push_back(rhs);
  68. return *this;
  69. }
  70. namespace detail
  71. {
  72. template<class From>
  73. struct enum_maker
  74. {
  75. explicit enum_maker(From& from): from_(from) {}
  76. From& operator[](const value& val)
  77. {
  78. from_.add_static_constant(val.name_, val.val_);
  79. return from_;
  80. }
  81. From& operator[](const value_vector& values)
  82. {
  83. for (value_vector::const_iterator i = values.begin(); i != values.end(); ++i)
  84. {
  85. from_.add_static_constant(i->name_, i->val_);
  86. }
  87. return from_;
  88. }
  89. From& from_;
  90. private:
  91. void operator=(enum_maker const&); // C4512, assignment operator could not be generated
  92. template<class T> void operator,(T const&) const;
  93. };
  94. }
  95. }
  96. #endif // LUABIND_ENUM_MAKER_HPP_INCLUDED