| 1 |
////////////////////////////////////////////////////////////////////////////// |
| 2 |
////////////////////////////////////////////////////////////////////////////// |
| 3 |
// |
| 4 |
// copyright : (C) 2008 by Eran Ifrah |
| 5 |
// file name : cpptoken.h |
| 6 |
// |
| 7 |
// ------------------------------------------------------------------------- |
| 8 |
// A |
| 9 |
// _____ _ _ _ _ |
| 10 |
// / __ \ | | | | (_) | |
| 11 |
// | / \/ ___ __| | ___| | _| |_ ___ |
| 12 |
// | | / _ \ / _ |/ _ \ | | | __/ _ ) |
| 13 |
// | \__/\ (_) | (_| | __/ |___| | || __/ |
| 14 |
// \____/\___/ \__,_|\___\_____/_|\__\___| |
| 15 |
// |
| 16 |
// F i l e |
| 17 |
// |
| 18 |
// This program is free software; you can redistribute it and/or modify |
| 19 |
// it under the terms of the GNU General Public License as published by |
| 20 |
// the Free Software Foundation; either version 2 of the License, or |
| 21 |
// (at your option) any later version. |
| 22 |
// |
| 23 |
////////////////////////////////////////////////////////////////////////////// |
| 24 |
////////////////////////////////////////////////////////////////////////////// |
| 25 |
#ifndef __cpptoken__ |
| 26 |
#define __cpptoken__ |
| 27 |
|
| 28 |
#include <string> |
| 29 |
#include <list> |
| 30 |
#include <map> |
| 31 |
#include "codelite_exports.h" |
| 32 |
#include <wx/string.h> |
| 33 |
#include <wx/wxsqlite3.h> |
| 34 |
#include <vector> |
| 35 |
#include <wx/filename.h> |
| 36 |
#include <set> |
| 37 |
#include <algorithm> |
| 38 |
#include "wxStringHash.h" |
| 39 |
|
| 40 |
typedef std::vector<wxFileName> wxFileList_t; |
| 41 |
class WXDLLIMPEXP_CL CppToken |
| 42 |
{ |
| 43 |
int m_id; |
| 44 |
wxString name; // the name of the token |
| 45 |
size_t offset; // file offset |
| 46 |
wxString filename; |
| 47 |
size_t lineNumber; |
| 48 |
|
| 49 |
public: |
| 50 |
typedef std::vector<CppToken> Vec_t; |
| 51 |
|
| 52 |
class wxFileNameToString |
| 53 |
{ |
| 54 |
public: |
| 55 |
wxString operator()(const wxFileName& fn) { return fn.GetFullPath(); } |
| 56 |
}; |
| 57 |
|
| 58 |
// Required by later versions of clang compiler |
| 59 |
bool operator<(const CppToken& rhs) const; |
| 60 |
|
| 61 |
/** |
| 62 |
* @class Predicate |
| 63 |
* a helper class that can be used to filter tokens using std:remove_if |
| 64 |
* this Predicate filters files that do not exist in m_files member |
| 65 |
*/ |
| 66 |
class RemoveIfNotIn |
| 67 |
{ |
| 68 |
std::unordered_set<wxString> m_files; |
| 69 |
|
| 70 |
public: |
| 71 |
RemoveIfNotIn(const wxFileList_t& fileList) |
| 72 |
{ |
| 73 |
|
| 74 |
// first convert vector<wxFileName> to vector<wxString> |
| 75 |
std::vector<wxString> v; |
| 76 |
v.resize(fileList.size()); |
| 77 |
std::transform(fileList.begin(), fileList.end(), v.begin(), wxFileNameToString()); |
| 78 |
|
| 79 |
// construct a set based on the vector content |
| 80 |
m_files = std::unordered_set<wxString>(v.begin(), v.end()); |
| 81 |
} |
| 82 |
|
| 83 |
RemoveIfNotIn(const std::unordered_set<wxString>& filesSet) |
| 84 |
: m_files(filesSet) |
| 85 |
{ |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* @brief return true if the token's file does _not_ exists in the |
| 90 |
* m_files member (which was initialized while constructing the Predicate) |
| 91 |
*/ |
| 92 |
bool operator()(const CppToken& token) { return m_files.count(token.getFilename()) == 0; } |
| 93 |
}; |
| 94 |
|
| 95 |
public: |
| 96 |
CppToken(); |
| 97 |
CppToken(wxSQLite3ResultSet& res); |
| 98 |
~CppToken(); |
| 99 |
|
| 100 |
void reset(); |
| 101 |
void append(wxChar ch); |
| 102 |
|
| 103 |
/** |
| 104 |
* @brief store the token into the datbase. |
| 105 |
* @param db database pointer |
| 106 |
* @param fileId the file ID for this token |
| 107 |
*/ |
| 108 |
int store(wxSQLite3Database* db, wxLongLong fileId) const; |
| 109 |
|
| 110 |
/** |
| 111 |
* @brief load tokens from the cache by file/name |
| 112 |
* @param db |
| 113 |
* @param name |
| 114 |
* @param file_name |
| 115 |
* @return |
| 116 |
*/ |
| 117 |
static CppToken::Vec_t loadByNameAndFile(wxSQLite3Database* db, const wxString& name, wxLongLong fileID); |
| 118 |
/** |
| 119 |
* @brief |
| 120 |
* @param db |
| 121 |
* @param name |
| 122 |
* @return |
| 123 |
*/ |
| 124 |
static CppToken::Vec_t loadByName(wxSQLite3Database* db, const wxString& name); |
| 125 |
|
| 126 |
void setName(const wxString& name) { this->name = name; } |
| 127 |
|
| 128 |
void setOffset(const size_t& offset) { this->offset = offset; } |
| 129 |
|
| 130 |
const wxString& getName() const { return name; } |
| 131 |
const size_t& getOffset() const { return offset; } |
| 132 |
|
| 133 |
void setFilename(const wxString& filename) { this->filename = filename; } |
| 134 |
|
| 135 |
const wxString& getFilename() const { return filename; } |
| 136 |
|
| 137 |
void setId(const int& id) { this->m_id = id; } |
| 138 |
|
| 139 |
const int& getId() const { return m_id; } |
| 140 |
|
| 141 |
size_t getLineNumber() const { return lineNumber; } |
| 142 |
|
| 143 |
void setLineNumber(size_t lineNo) { lineNumber = lineNo; } |
| 144 |
|
| 145 |
void print(); |
| 146 |
|
| 147 |
// Provide lower-than operator so we can use std::vector::sort method |
| 148 |
bool operator<(const CppToken& rhs) { return filename < rhs.filename; } |
| 149 |
}; |
| 150 |
|
| 151 |
class WXDLLIMPEXP_CL CppTokensMap |
| 152 |
{ |
| 153 |
std::unordered_map<wxString, std::vector<CppToken>*> m_tokens; |
| 154 |
|
| 155 |
public: |
| 156 |
CppTokensMap(); |
| 157 |
~CppTokensMap(); |
| 158 |
|
| 159 |
/** |
| 160 |
* @brief return true if any token with given name exists in the map |
| 161 |
* @param name token's name to search |
| 162 |
*/ |
| 163 |
bool contains(const wxString& name); |
| 164 |
/** |
| 165 |
* @brief return list of tokens with given name |
| 166 |
* @param name token name |
| 167 |
* @param tokens [output] |
| 168 |
*/ |
| 169 |
void findTokens(const wxString& name, std::vector<CppToken>& tokens); |
| 170 |
/** |
| 171 |
* @brief add token to the map. if token with same name already exists, it will be appended so multiple tokens with |
| 172 |
* same name is allowed |
| 173 |
* @param token token to add |
| 174 |
*/ |
| 175 |
void addToken(const CppToken& token); |
| 176 |
|
| 177 |
/** |
| 178 |
* @brief add list of tokens for a given name to the map |
| 179 |
*/ |
| 180 |
void addToken(const wxString& name, const CppToken::Vec_t& list); |
| 181 |
|
| 182 |
/** |
| 183 |
* @brief clear all token |
| 184 |
*/ |
| 185 |
void clear(); |
| 186 |
|
| 187 |
/** |
| 188 |
* @brief return true if no tokens were found |
| 189 |
*/ |
| 190 |
bool is_empty(); |
| 191 |
}; |
| 192 |
|
| 193 |
#endif // __cpptoken__ |