token_cache.hpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*=============================================================================
  2. Boost.Wave: A Standard compliant C++ preprocessor library
  3. http://www.boost.org/
  4. Copyright (c) 2001-2012 Hartmut Kaiser. Distributed under the Boost
  5. Software License, Version 1.0. (See accompanying file
  6. LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. =============================================================================*/
  8. #if !defined(BOOST_TOKEN_CACHE_HPP_4D2320B7_1D56_4113_A114_397E70FA438C_INCLUDED)
  9. #define BOOST_TOKEN_CACHE_HPP_4D2320B7_1D56_4113_A114_397E70FA438C_INCLUDED
  10. #include <vector>
  11. #include <boost/wave/wave_config.hpp>
  12. #include <boost/wave/token_ids.hpp>
  13. // this must occur after all of the includes and before any code appears
  14. #ifdef BOOST_HAS_ABI_HEADERS
  15. #include BOOST_ABI_PREFIX
  16. #endif
  17. ///////////////////////////////////////////////////////////////////////////////
  18. namespace boost {
  19. namespace wave {
  20. namespace cpplexer {
  21. ///////////////////////////////////////////////////////////////////////////////
  22. //
  23. // The token_cache template is used to cache the tokens corresponding to the
  24. // keywords, operators and other constant language elements.
  25. //
  26. // This avoids repeated construction of these tokens, which is especially
  27. // effective when used in conjunction with a copy on write string
  28. // implementation (COW string).
  29. //
  30. ///////////////////////////////////////////////////////////////////////////////
  31. template <typename StringT>
  32. class token_cache
  33. {
  34. public:
  35. token_cache()
  36. : cache(T_LAST_TOKEN - T_FIRST_TOKEN)
  37. {
  38. typename std::vector<StringT>::iterator it = cache.begin();
  39. for (unsigned int i = T_FIRST_TOKEN; i < T_LAST_TOKEN; ++i, ++it)
  40. {
  41. *it = StringT(boost::wave::get_token_value(token_id(i)));
  42. }
  43. }
  44. StringT const &get_token_value(token_id id) const
  45. {
  46. return cache[BASEID_FROM_TOKEN(id) - T_FIRST_TOKEN];
  47. }
  48. private:
  49. std::vector<StringT> cache;
  50. };
  51. ///////////////////////////////////////////////////////////////////////////////
  52. } // namespace cpplexer
  53. } // namespace wave
  54. } // namespace boost
  55. // the suffix header occurs after all of the code
  56. #ifdef BOOST_HAS_ABI_HEADERS
  57. #include BOOST_ABI_SUFFIX
  58. #endif
  59. #endif // !defined(BOOST_TOKEN_CACHE_HPP_4D2320B7_1D56_4113_A114_397E70FA438C_INCLUDED)