source: NonGTP/Boost/boost/wave/cpplexer/token_cache.hpp @ 857

Revision 857, 2.0 KB checked in by igarcia, 19 years ago (diff)
Line 
1/*=============================================================================
2    Boost.Wave: A Standard compliant C++ preprocessor library
3
4    http://www.boost.org/
5
6    Copyright (c) 2001-2005 Hartmut Kaiser. Distributed under the Boost
7    Software License, Version 1.0. (See accompanying file
8    LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9=============================================================================*/
10
11#if !defined(TOKEN_CACHE_HPP_4D2320B7_1D56_4113_A114_397E70FA438C_INCLUDED)
12#define TOKEN_CACHE_HPP_4D2320B7_1D56_4113_A114_397E70FA438C_INCLUDED
13
14#include <boost/wave/token_ids.hpp>
15
16///////////////////////////////////////////////////////////////////////////////
17namespace boost {
18namespace wave {
19namespace cpplexer {
20
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///////////////////////////////////////////////////////////////////////////////
31template <typename StringT>
32class token_cache
33{
34public:
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
45    StringT const &get_token_value(token_id id) const
46    {
47        return cache[BASEID_FROM_TOKEN(id) - T_FIRST_TOKEN];
48    }
49   
50private:
51    std::vector<StringT> cache;
52};
53
54///////////////////////////////////////////////////////////////////////////////
55}   // namespace cpplexer
56}   // namespace wave
57}   // namespace boost
58
59#endif // !defined(TOKEN_CACHE_HPP_4D2320B7_1D56_4113_A114_397E70FA438C_INCLUDED)
Note: See TracBrowser for help on using the repository browser.