Bonjour

Je souhaiterai mettre à disposition une vue d'un container qui interdise l'ajout et la suppression d'objet, mais qui permet l'accès et la modification des objets contenus dans le container. C'est du QVector (Qt) mais c'est comme des container stl.

Pour le moment, j'ai fait un truc comme ça :

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
template<class C, class T = typename C::Data>
class const_view
{
public:
    const_view(const C& container) : m_cnt(&container) { }
 
    typedef typename C::const_iterator const_iterator;
 
    int size() const { return m_cnt->size(); }
 
    const_iterator begin() const { return m_cnt->begin(); }
    const_iterator end() const { return m_cnt->end(); }
 
    const T& at(int index) const { return (*m_cnt)->at(index); }
 
private:
    const C* m_cnt;
};
 
template<class C, class T = typename C::Data>
class view
{
public:
    view(C& container) : m_cnt(&container) { }
 
    typedef typename C::const_iterator const_iterator;
    typedef typename C::iterator iterator;
 
    int size() const { return m_cnt->size(); }
 
    const_iterator begin() const { return m_cnt->begin(); }
    const_iterator end() const { return m_cnt->end(); }
 
    iterator begin() { return m_cnt->begin(); }
    iterator end() { return m_cnt->end(); }
 
    const T& at(int index) const { return m_cnt->at(index); }
    T& at(int index) { return (*m_cnt)[index]; }
 
private:
    C* m_cnt;
};
 
class DataModel : public QObject
{
    Q_OBJECT
 
public:
    typedef const_view<QVector<Project*>, Project*> ProjectsConstView;
    typedef view<QVector<Project*>, Project*> ProjectsView;
 
    Project* addProject();
 
    // pour retourner un vue "read-only"
    ProjectsConstView projects() const;
 
    // pour retourner une vue qui permette de modifier les objets du container
    ProjectsView projects();
 
private:
    QVector<Project*> m_projects;
}
Ca me semble compliqué. Avez vous des idées plus simples ?
Merci