Last active
October 21, 2017 19:59
-
-
Save senapk/cfd93eb69f28c34eaa1981476e7a7bcf to your computer and use it in GitHub Desktop.
Classes Repositório e Auxiliar para POO EC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef POO_H | |
#define POO_H | |
#include <iostream> | |
#include <sstream> | |
#include <vector> | |
#include <map> | |
namespace poo { | |
template <class T> | |
T cast(std::string palavra){ | |
T value; | |
std::stringstream(palavra) >> value; | |
return value; | |
} | |
inline int Int(std::string word){ | |
return cast<int>(word); | |
} | |
inline int Float(std::string word){ | |
return cast<float>(word); | |
} | |
//quebra o texto em um vetor de pedaços, utilizando o separador sep | |
std::vector<std::string> split(const std::string& text, char sep = ' '){ | |
std::vector<std::string> saida; | |
std::istringstream ss(text); | |
std::string token; | |
while(getline(ss, token, sep)) | |
saida.push_back(token); | |
return saida; | |
} | |
//dá um tab de dois espacos em branco no texto | |
//retorna o texto tabeado | |
std::string tab2(std::string data, std::string prefix = " "){ | |
auto linhas = split(data, '\n'); | |
std::string saida = ""; | |
for(auto& line : linhas) | |
saida += prefix + line + "\n"; | |
if(saida.size() > 0) | |
saida.pop_back();//remove a ultima \n | |
return saida; | |
} | |
template<class T> | |
std::string vet2str(std::vector<T> vet, std::string separ){ | |
std::stringstream ss; | |
for(auto elem : vet) | |
ss << elem.toString() << separ; | |
return ss.str(); | |
} | |
template<class T> | |
std::vector<T> map_values(std::map<std::string, T> &mapa){ | |
std::vector<T> vet; | |
for(auto& par: mapa) | |
vet.push_back(par.second); | |
return vet; | |
} | |
template<class T> | |
std::vector<T> map_keys(std::map<std::string, T> &mapa){ | |
std::vector<T> vet; | |
for(auto& par: mapa) | |
vet.push_back(par.first); | |
return vet; | |
} | |
std::string getCmd(){ | |
std::string line = ""; | |
while((line == "" ) || (line[0] == '#') || (line.substr(0, 2) == " ")) | |
std::getline(std::cin, line); | |
return line; | |
} | |
}//namespace poo | |
#endif // POO_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef REPOSITORY_H | |
#define REPOSITORY_H | |
#include <iostream> | |
#include <map> | |
template <class T> | |
class Repository { | |
std::map<std::string, T> _mapa; | |
std::string nomeTipo; | |
public: | |
Repository(std::string nomeTipo = ""){ | |
this->nomeTipo = nomeTipo; | |
} | |
//adiciona se a chave ainda nao existir | |
void add(std::string key, T t){ | |
if(_mapa.count(key) == 1) | |
throw nomeTipo + " " + key + " ja existe"; | |
_mapa[key] = t; | |
} | |
//retorna se o objeto está no repositório | |
bool has(std::string key){ | |
return _mapa.count(key); | |
} | |
//retorna se conseguiu remover | |
void rm(std::string key){ | |
if(!_mapa.erase(key)) | |
throw nomeTipo + " " + key + " nao existe"; | |
} | |
//retorna o endereço do objeto | |
T * get(std::string key){ | |
if(_mapa.count(key) == 0) | |
throw nomeTipo + " " + key + " nao existe"; | |
return &_mapa[key]; | |
} | |
//retonar um vetor com a cópia dos elementos | |
std::vector<T> values(){ | |
std::vector<T> vet; | |
for(auto& par : _mapa) | |
vet.push_back(par.second); | |
return vet; | |
} | |
//retorna um vetor com a cópia das chaves | |
std::vector<std::string> keys(){ | |
std::vector<std::string> vet; | |
for(auto& par : _mapa) | |
vet.push_back(par.first); | |
return vet; | |
} | |
}; | |
#endif // REPOSITORY_H | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment