Bonjour,

Pas de question pour aujourd'hui. Tout du moins pas pour me débloquer.

Dans un projet sur lequel je suis en ce moment j'ai des objets que j'ai besoin de stocker au format XML et parfois CSV.
J'ai voulu essayer de réutiliser certaines choses que j'ai lu, mais que je n'avais pas encore utilisé, pour me faire la main.
J'aimerais savoir si ce que j'ai fait est correct, si vous avez de proposition d'amélioration et surtout si je suis pas partit dans la mauvaises direction.

J'ai tout regroupé dans le main.cpp pour poser, c'est un peut brouillon du coup...

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include <iostream>
#include <algorithm>
 
using namespace std;
 
#include "exceptions/XmlParsedException.h"
#include "exceptions/FileNotReadableException.h"
 
#include <iostream>
#include <fstream>
#include <sstream>
#include <list>
 
#include <QtXml/QDomDocument>
 
struct Foo
{
	Foo(const string& s_ = string()) : s(s_) {}
 
	string toString() const { return s; }
 
	string s;
};
 
struct Bar
{
	Bar(const string& a_ = string(), const string& b_ = string()) : a(a_), b(b_) {}
 
	string toString() const { return a + " - " + b; }
 
	string a;
	string b;
};
 
 
namespace reader
{
	//****************************************************************
	//							ObjectReader
	//****************************************************************
	template <typename T, typename DerivedReader>
	class ObjectReader
	{
		public :
			void readFrom(istream& is) { static_cast<DerivedReader*>(this)->readFrom(is); }
 
		protected :
			ObjectReader(T& object_) : object(object_) {}
 
			T& object;
	};
 
 
 
	//****************************************************************
	//							ObjectFileReader
	//****************************************************************
	template <typename T, typename DerivedFileReader>
	class ObjectFileReader : public ObjectReader<T, DerivedFileReader>
	{
		public :
			using ObjectReader<T, DerivedFileReader>::readFrom;
 
			void readFrom(const string& filePath)
			{
				ifstream ifs(filePath.c_str());
 
				if (!ifs)
				{
					throw exceptions::FileNotReadableException(filePath);
				}
 
				readFrom(ifs);
				ifs.close();
			}
 
		protected :
			ObjectFileReader(T& object_) : ObjectReader<T, DerivedFileReader>(object_) {}
	};
 
 
 
	//****************************************************************
	//							AbstractXmlReader
	//****************************************************************
	template <typename T, typename DerivedXmlReader>
	class AbstractXmlReader : public ObjectFileReader<T, DerivedXmlReader>
	{
		public :
			using ObjectFileReader<T, DerivedXmlReader>::readFrom;
 
			void readFrom(istream& is)
			{
				ostringstream oss;
					oss << is.rdbuf();
 
				QDomDocument domDoc;
				if (!domDoc.setContent(QByteArray(oss.str().c_str())))
				{
					throw exceptions::XmlParsedException(oss.str());
				}
 
				readFrom(domDoc.documentElement());
			}
 
			void readFrom(const QDomNode& node) { static_cast<DerivedXmlReader*>(this)->readFrom(node); }
 
		protected :
			AbstractXmlReader(T& object_) : ObjectFileReader<T, DerivedXmlReader>(object_) {}
	};
 
 
 
	//****************************************************************
	//							Global
	//****************************************************************
	template <typename T>
	class XmlReader : public AbstractXmlReader<T, XmlReader<T> >
	{
		protected :
			XmlReader(T& account) : AbstractXmlReader<T, XmlReader<T> >(account) {}
	};
 
 
 
	//****************************************************************
	//							Foo
	//****************************************************************
	template <>
	class XmlReader<Foo> : public AbstractXmlReader<Foo, XmlReader<Foo> >
	{
		public :
			static const string ROOT_TAG_NAME;
			static const string S_TAG_NAME;
 
			XmlReader(Foo& account) : AbstractXmlReader(account) {}
 
			using AbstractXmlReader::readFrom;
 
			void readFrom(const QDomNode& node)
			{
				object.s = node.firstChild().nodeValue().toStdString();
			}
	};
	const string XmlReader<Foo>::ROOT_TAG_NAME = "foo";
	const string XmlReader<Foo>::S_TAG_NAME = "s";
 
 
 
	//****************************************************************
	//							Bar
	//****************************************************************
	template <>
	class XmlReader<Bar> : public AbstractXmlReader<Bar, XmlReader<Bar> >
	{
		public :
			static const string ROOT_TAG_NAME;
			static const string A_TAG_NAME;
			static const string B_TAG_NAME;
 
			XmlReader(Bar& account) : AbstractXmlReader(account) {}
 
			using AbstractXmlReader::readFrom;
 
			void readFrom(const QDomNode& node)
			{
				object.a = node.firstChildElement(A_TAG_NAME.c_str()).firstChild().nodeValue().toStdString();
				object.b = node.firstChildElement(B_TAG_NAME.c_str()).firstChild().nodeValue().toStdString();
			}
	};
	const string XmlReader<Bar>::ROOT_TAG_NAME = "bar";
	const string XmlReader<Bar>::A_TAG_NAME = "a";
	const string XmlReader<Bar>::B_TAG_NAME = "b";
 
 
 
	//****************************************************************
	//							Listes
	//****************************************************************
	template <typename T>
	class XmlReader<list<T> > : public AbstractXmlReader<list<T>, XmlReader<list<T> > >
	{
		public :
			XmlReader(list<T>& myList) : AbstractXmlReader<list<T>, XmlReader<list<T> > >(myList) {}
 
			using AbstractXmlReader<list<T>, XmlReader<list<T> > >::readFrom;
 
			void readFrom(const QDomNode& node)
			{
				QDomNode child = node.firstChildElement();
 
				while (!child.isNull())
				{
					T element;
 
					XmlReader<T>(element).readFrom(child);
 
					this->object.push_back(element);
 
					child = child.nextSibling();
				}
			}
	};
}
 
using namespace reader;
 
int main()
{
	try
	{
		list<Foo> foosList;
		XmlReader<list<Foo> >(foosList).readFrom("./foos.xml");
		list<Bar> accountsList;
		XmlReader<list<Bar> >(accountsList).readFrom("./bars.xml");
 
		for_each(foosList.begin(), foosList.end(), [](const Foo& item) { cout << item.toString() << endl; } );
		for_each(accountsList.begin(), accountsList.end(), [](const Bar& item) { cout << item.toString() << endl; } );
	}
	catch (const exceptions::MyException& myException)
	{
		cout << myException.toString() << endl;
	}
 
	return 0;
}
L'idée de départ était de pouvoir ecrire/lire mes objets en XML tout en permettant d'ajouter facilement un autre format.
C'était aussi l'occasion pour moi de me mettre à la STL, J'avais la, mauvaise, habitude d'utiliser les librairies de Qt en lieu et place de la STL donc je n'y suis pas très familiarisé.

Très vite j'ai eu beaucoup de fichier dans mon projet, 8 objets différents à stocker, une classe pour chaque fichier... C'était lourd, j'ai donc voulu essayer de regrouper le tout en utilisant les template.
Autant pour m'entraîner à les manipuler que pour mettre une fois en place un autre système que les méthode virtuelles ou la redéfinition de méthode.

J'avais donc quelque chose dans ce genre là :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
ObjectReader
    => FileReader
        => XmlReader
            => FooXmlReader
            => BarXmlReader
        => CsvReader
            => FooCsvReader
            => BarCsvReader
On voit très bien qu'avec quelques objets en plus, plus la gestion de l'écriture le nombre de fichier commençait vraiment à devenir encombrant.
J'ai donc voulu simplifier en :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
ObjectReader
    => FileReader
        => XmlReader<T>
        => CsvReader<T>
Je me suis aussi dit que de pouvoir utiliser toujours la même classe avec seulement le paramètre template qui permet de différencier le type d'objet lut/écrit était plus confortable.

Cette solution est-elle viable ?
Y a-t-il un moyen plus pratique, plus évolutif ?
L'utilisation des templates est-elle justifié dans ce cas ?

Merci d'avance pour vos commentaires.