• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*=============================================================================
2     Boost.Wave: A Standard compliant C++ preprocessor library
3 
4     Sample: Collect token statistics from the analysed files
5 
6     http://www.boost.org/
7 
8     Copyright (c) 2001-2010 Hartmut Kaiser. Distributed under the Boost
9     Software License, Version 1.0. (See accompanying file
10     LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
11 =============================================================================*/
12 
13 #if !defined(BOOST_COLLECT_TOKEN_STATISTICS_VERSION_HPP)
14 #define BOOST_COLLECT_TOKEN_STATISTICS_VERSION_HPP
15 
16 #include <algorithm>
17 #include <map>
18 #include <iostream>
19 
20 #include <boost/assert.hpp>
21 #include <boost/wave/token_ids.hpp>
22 
23 ///////////////////////////////////////////////////////////////////////////////
24 class collect_token_statistics
25 {
26     enum {
27         count = boost::wave::T_LAST_TOKEN - boost::wave::T_FIRST_TOKEN
28     };
29 
30 public:
collect_token_statistics()31     collect_token_statistics()
32     {
33         std::fill(&token_count[0], &token_count[count], 0);
34     }
35 
36     // account for the given token type
37     template <typename Token>
operator ()(Token const & token)38     void operator() (Token const& token)
39     {
40         using boost::wave::token_id;
41 
42         int id = ID_FROM_TOKEN(token) - boost::wave::T_FIRST_TOKEN;
43         BOOST_ASSERT(id < count);
44         ++token_count[id];
45     }
46 
47     // print out the token statistics in descending order
print() const48     void print() const
49     {
50         using namespace boost::wave;
51         typedef std::multimap<int, token_id> ids_type;
52 
53         ids_type ids;
54         for (unsigned int i = 0; i < count; ++i)
55         {
56             ids.insert(ids_type::value_type(
57                 token_count[i], token_id(i + T_FIRST_TOKEN)));
58         }
59 
60         ids_type::reverse_iterator rend = ids.rend();
61         for(ids_type::reverse_iterator rit = ids.rbegin(); rit != rend; ++rit)
62         {
63             std::cout << boost::wave::get_token_name((*rit).second) << ": "
64                       << (*rit).first << std::endl;
65         }
66     }
67 
68 private:
69     int token_count[count];
70 };
71 
72 #endif // !defined(BOOST_COLLECT_TOKEN_STATISTICS_VERSION_HPP)
73