utree_detail1.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  3. Copyright (c) 2001-2011 Hartmut Kaiser
  4. Copyright (c) 2011 Bryce Lelbach
  5. Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. =============================================================================*/
  8. #if !defined(BOOST_SPIRIT_UTREE_DETAIL1)
  9. #define BOOST_SPIRIT_UTREE_DETAIL1
  10. #include <boost/type_traits/alignment_of.hpp>
  11. namespace boost { namespace spirit { namespace detail
  12. {
  13. template <typename UTreeX, typename UTreeY>
  14. struct visit_impl;
  15. struct index_impl;
  16. ///////////////////////////////////////////////////////////////////////////
  17. // Our POD double linked list. Straightforward implementation.
  18. // This implementation is very primitive and is not meant to be
  19. // used stand-alone. This is the internal data representation
  20. // of lists in our utree.
  21. ///////////////////////////////////////////////////////////////////////////
  22. struct list // keep this a POD!
  23. {
  24. struct node;
  25. template <typename Value>
  26. class node_iterator;
  27. void free();
  28. void copy(list const& other);
  29. void default_construct();
  30. template <typename T, typename Iterator>
  31. void insert(T const& val, Iterator pos);
  32. template <typename T>
  33. void push_front(T const& val);
  34. template <typename T>
  35. void push_back(T const& val);
  36. void pop_front();
  37. void pop_back();
  38. node* erase(node* pos);
  39. node* first;
  40. node* last;
  41. std::size_t size;
  42. };
  43. ///////////////////////////////////////////////////////////////////////////
  44. // A range of utree(s) using an iterator range (begin/end) of node(s)
  45. ///////////////////////////////////////////////////////////////////////////
  46. struct range
  47. {
  48. list::node* first;
  49. list::node* last;
  50. };
  51. ///////////////////////////////////////////////////////////////////////////
  52. // A range of char*s
  53. ///////////////////////////////////////////////////////////////////////////
  54. struct string_range
  55. {
  56. char const* first;
  57. char const* last;
  58. };
  59. ///////////////////////////////////////////////////////////////////////////
  60. // A void* plus type_info
  61. ///////////////////////////////////////////////////////////////////////////
  62. struct void_ptr
  63. {
  64. void* p;
  65. std::type_info const* i;
  66. };
  67. ///////////////////////////////////////////////////////////////////////////
  68. // Our POD fast string. This implementation is very primitive and is not
  69. // meant to be used stand-alone. This is the internal data representation
  70. // of strings in our utree. This is deliberately a POD to allow it to be
  71. // placed in a union. This POD fast string specifically utilizes
  72. // (sizeof(list) * alignment_of(list)) - (2 * sizeof(char)). In a 32 bit
  73. // system, this is 14 bytes. The two extra bytes are used by utree to store
  74. // management info.
  75. //
  76. // It is a const string (i.e. immutable). It stores the characters directly
  77. // if possible and only uses the heap if the string does not fit. Null
  78. // characters are allowed, making it suitable to encode raw binary. The
  79. // string length is encoded in the first byte if the string is placed in-situ,
  80. // else, the length plus a pointer to the string in the heap are stored.
  81. ///////////////////////////////////////////////////////////////////////////
  82. struct fast_string // Keep this a POD!
  83. {
  84. static std::size_t const
  85. buff_size = (sizeof(list) + boost::alignment_of<list>::value)
  86. / sizeof(char);
  87. static std::size_t const
  88. small_string_size = buff_size-sizeof(char);
  89. static std::size_t const
  90. max_string_len = small_string_size - 3;
  91. struct heap_store
  92. {
  93. char* str;
  94. std::size_t size;
  95. };
  96. union
  97. {
  98. char buff[buff_size];
  99. long lbuff[buff_size / (sizeof(long)/sizeof(char))]; // for initialize
  100. heap_store heap;
  101. };
  102. int get_type() const;
  103. void set_type(int t);
  104. bool is_heap_allocated() const;
  105. std::size_t size() const;
  106. char const* str() const;
  107. template <typename Iterator>
  108. void construct(Iterator f, Iterator l);
  109. void swap(fast_string& other);
  110. void free();
  111. void copy(fast_string const& other);
  112. void initialize();
  113. char& info();
  114. char info() const;
  115. short tag() const;
  116. void tag(short tag);
  117. };
  118. }}}
  119. #endif