@spartanPAGE
Ok przetestowałem te klasę i tak:
void loadLinesFrom(CStrRef path)
gdzie :
using Str = std::string;
using StrRef = Str &;
using CStrRef = const StrRef;
Czemu nie można:
void loadLinesFrom(std::string path)
Sprawdziłem działa
Dzięki tej zmianie mozna od tego momentu używać funkcji nie tak:
using m_CStrRef = std::string & ;
std::string path = "./abc.txt";
m_CStrRef pathToFile = path;
FileHeldInMemory<> file;
file.loadLinesFrom(pathToFile);
Tylko tak:
std::string path = "./abc.txt";
FileHeldInMemory<> file;
file.loadLinesFrom(path);
lub:
FileHeldInMemory<> file;
file.loadLinesFrom("./abc.txt");
Odczytywanie lini pliku:
inline const LineType &lineAt(size_t id) const{
return lines[id];
}
inline LineType &lineAt(size_t id){
return const_cast<LineType&>(static_cast<const FileHeldInMemory*>(this)->lineAt(id));
}
gdzie:
using LineType = Str;
using Str = std::string;
Czemu są dwie funkcje nie wystarczy jedna moim zdaniem.
Nie mówiąc o tym czy takie cuda nie pojęte są potrzebne.
return const_cast<LineType&>(static_cast<const FileHeldInMemory*>(this)->lineAt(id));
Brak możliwości zapisania (już nie mówię że dowolnej) linii do pliku tekstowego - chyba ze czegoś nie widzę bynajmniej nie dostrzegam.
Przy okazji znalazłem potencjalnego buga u ciebie i u mnie :
Wczytujemy plik abc.txt
Jego zawartośc to:
tekst
testowy
Uzywamy funkcji
inline const LineType &lineAt(size_t id) const{
return lines[id];
}
inline LineType &lineAt(size_t id){
return const_cast<LineType&>(static_cast<const FileHeldInMemory*>(this)->lineAt(id));
}
I teraz w jakiejś funkcji:
FileHeldInMemory<> file;
std::string wynik = file.lineAt(0);//wynik tekst
std::string wynik = file.lineAt(1);//wynik testowy
std::string wynik = file.lineAt(2lubwiecej);// **wywali porgram**
Idziemy dalej
void saveTo(CStrRef path);
Po co funkcja zapisująca plik skoro nie zauważyłem żeby istniała funkcja modyfikująca plik czy linie tego pliku.
Ogólne odczucia i opinia i w ogóle w szczególe.(subiektywne)
Przez wszelkie udziwienia klase użyć cięej niż moją
Takie cuda zaciemniają "przekaz"
using Str = std::string;
using StrRef = Str &;
using CStrRef = const StrRef;
template<
template<class, class> class ContainerT = std::vector,
template<class> class AllocatorT = std::allocator
>
class FileHeldInMemory{
public:
using LineType = Str;
using Allocator = AllocatorT<LineType>;
using Container = ContainerT<LineType, Allocator>;
....
return const_cast<LineType&>(static_cast<const FileHeldInMemory*>(this)->lineAt(id));
.....
if(auto file = std::ostream(path))
for(const auto &element : lines)
....
if(auto&& file = std::ifstream(path))