src/model.h

Thu, 09 Feb 2017 20:44:36 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Thu, 09 Feb 2017 20:44:36 +0200
changeset 1113
5f3139c802bf
parent 1091
4a754362f660
child 1147
a26568aa3cce
permissions
-rw-r--r--

Cleaned up GLCompiler. Among other changes, the compiler no longer has to be told what to compile.

1091
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
1 /*
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
2 * LDForge: LDraw parts authoring CAD
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
3 * Copyright (C) 2013 - 2017 Teemu Piippo
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
4 *
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
5 * This program is free software: you can redistribute it and/or modify
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
6 * it under the terms of the GNU General Public License as published by
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
8 * (at your option) any later version.
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
9 *
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
10 * This program is distributed in the hope that it will be useful,
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
13 * GNU General Public License for more details.
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
14 *
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
15 * You should have received a copy of the GNU General Public License
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
17 */
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
18
1073
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 #pragma once
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
20 #include "main.h"
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
21 #include "ldObject.h"
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22
1091
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
23 /*
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
24 * This class represents a LDraw model, consisting of a vector of objects. It manages LDObject ownership.
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
25 */
1113
5f3139c802bf Cleaned up GLCompiler. Among other changes, the compiler no longer has to be told what to compile.
Teemu Piippo <teemu@hecknology.net>
parents: 1091
diff changeset
26 class Model : public QObject
1073
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27 {
1113
5f3139c802bf Cleaned up GLCompiler. Among other changes, the compiler no longer has to be told what to compile.
Teemu Piippo <teemu@hecknology.net>
parents: 1091
diff changeset
28 Q_OBJECT
5f3139c802bf Cleaned up GLCompiler. Among other changes, the compiler no longer has to be told what to compile.
Teemu Piippo <teemu@hecknology.net>
parents: 1091
diff changeset
29
1073
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
30 public:
1079
67c6e5d32e68 More rework on model stuff, removals of LDSpawn calls. Most importantly, the LDraw code parsing function was moved to Model.
Teemu Piippo <teemu@hecknology.net>
parents: 1077
diff changeset
31 Model(DocumentManager* manager);
1073
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
32 Model(const Model& other) = delete;
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
33 ~Model();
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34
1091
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
35 void addObject(LDObject* object);
1073
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
36 virtual void insertObject(int position, LDObject* object);
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
37 virtual bool swapObjects(LDObject* one, LDObject* other);
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
38 virtual bool setObjectAt(int idx, LDObject* obj);
1091
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
39 template<typename T, typename... Args> T* emplace(Args&& ...args);
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
40 template<typename T, typename... Args> T* emplaceAt(int position, Args&& ...args);
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
41 template<typename T, typename... Args> T* emplaceReplacement(LDObject* object, Args&& ...args);
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
42 template<typename T, typename... Args> T* emplaceReplacementAt(int position, Args&& ...args);
1073
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
43 void removeAt(int position);
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
44 void remove(LDObject* object);
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
45 void replace(LDObject *object, Model& model);
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
46 void clear();
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
47 void merge(Model& other, int position = -1);
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
48 int size() const;
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
49 const QVector<LDObject*>& objects() const;
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
50 LDObject* getObject(int position) const;
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
51 void recountTriangles();
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
52 int triangleCount() const;
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
53 QVector<LDObject*>::iterator begin();
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
54 QVector<LDObject*>::iterator end();
1077
952d6b3e7d11 Replaced uses of LDSpawn with the Model class in edit modes
Teemu Piippo <teemu@hecknology.net>
parents: 1074
diff changeset
55 bool isEmpty() const;
1079
67c6e5d32e68 More rework on model stuff, removals of LDSpawn calls. Most importantly, the LDraw code parsing function was moved to Model.
Teemu Piippo <teemu@hecknology.net>
parents: 1077
diff changeset
56 DocumentManager* documentManager() const;
67c6e5d32e68 More rework on model stuff, removals of LDSpawn calls. Most importantly, the LDraw code parsing function was moved to Model.
Teemu Piippo <teemu@hecknology.net>
parents: 1077
diff changeset
57 LDObject* insertFromString(int position, QString line);
67c6e5d32e68 More rework on model stuff, removals of LDSpawn calls. Most importantly, the LDraw code parsing function was moved to Model.
Teemu Piippo <teemu@hecknology.net>
parents: 1077
diff changeset
58 LDObject* addFromString(QString line);
67c6e5d32e68 More rework on model stuff, removals of LDSpawn calls. Most importantly, the LDraw code parsing function was moved to Model.
Teemu Piippo <teemu@hecknology.net>
parents: 1077
diff changeset
59 LDObject* replaceWithFromString(LDObject* object, QString line);
1073
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
60
1113
5f3139c802bf Cleaned up GLCompiler. Among other changes, the compiler no longer has to be told what to compile.
Teemu Piippo <teemu@hecknology.net>
parents: 1091
diff changeset
61 signals:
5f3139c802bf Cleaned up GLCompiler. Among other changes, the compiler no longer has to be told what to compile.
Teemu Piippo <teemu@hecknology.net>
parents: 1091
diff changeset
62 void objectAdded(LDObject* object);
5f3139c802bf Cleaned up GLCompiler. Among other changes, the compiler no longer has to be told what to compile.
Teemu Piippo <teemu@hecknology.net>
parents: 1091
diff changeset
63 void aboutToRemoveObject(LDObject* object);
5f3139c802bf Cleaned up GLCompiler. Among other changes, the compiler no longer has to be told what to compile.
Teemu Piippo <teemu@hecknology.net>
parents: 1091
diff changeset
64 void objectModified(LDObject* object);
5f3139c802bf Cleaned up GLCompiler. Among other changes, the compiler no longer has to be told what to compile.
Teemu Piippo <teemu@hecknology.net>
parents: 1091
diff changeset
65
1073
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
66 protected:
1091
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
67 template<typename T, typename... Args> T* constructObject(Args&& ...args);
1073
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
68 void withdraw(LDObject* object);
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
69 virtual LDObject* withdrawAt(int position);
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
70
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
71 QVector<LDObject*> _objects;
1079
67c6e5d32e68 More rework on model stuff, removals of LDSpawn calls. Most importantly, the LDraw code parsing function was moved to Model.
Teemu Piippo <teemu@hecknology.net>
parents: 1077
diff changeset
72 DocumentManager* _manager;
1073
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
73 mutable int _triangleCount = 0;
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
74 mutable bool _needsTriangleRecount;
a0a0d581309b Major overhaul of object→document relationship: added the Model class which models the object buffer. Each object is to be included in a model (an invariant that currently does not hold). A document is a subclass of a model. The LDObject is also now agnostic about selection, and the selection is now a set. A lot of things are probably broken now but it's a major step forward.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
75 };
1087
80e25f6b0bb0 Some code cleanup in abstract editing modes.
Teemu Piippo <teemu@hecknology.net>
parents: 1082
diff changeset
76
80e25f6b0bb0 Some code cleanup in abstract editing modes.
Teemu Piippo <teemu@hecknology.net>
parents: 1082
diff changeset
77 int countof(Model& model);
1091
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
78
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
79 /*
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
80 * Given an LDObject type as the template parameter, and any number of variadic parameters, constructs an LDObject derivative
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
81 * and inserts it into this model. The variadic parameters and this model pointer are passed to the constructor. The constructed object
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
82 * is added to the end of the model.
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
83 *
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
84 * For instance, the LDLine contains a constructor as such:
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
85 *
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
86 * LDLine(Vertex v1, Vertex v2, Model* model);
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
87 *
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
88 * This constructor can be invoked as such:
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
89 *
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
90 * model->emplace<LDLine>(v1, v2);
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
91 */
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
92 template<typename T, typename... Args>
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
93 T* Model::emplace(Args&& ...args)
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
94 {
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
95 T* object = constructObject<T>(args...);
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
96 addObject(object);
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
97 return object;
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
98 }
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
99
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
100 /*
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
101 * Like emplace<>() but also takes a position as the first argument and emplaces the object at the given position instead of the
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
102 * end of the model.
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
103 */
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
104 template<typename T, typename... Args>
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
105 T* Model::emplaceAt(int position, Args&& ...args)
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
106 {
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
107 T* object = constructObject<T>(args...);
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
108 insertObject(position, object);
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
109 return object;
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
110 }
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
111
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
112 /*
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
113 * Like emplace<>() but instead of inserting the constructed object, the new object replaces the object given in the first parameter.
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
114 * If the old object cannot be replaced, the new object will not be constructed at all.
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
115 */
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
116 template<typename T, typename... Args>
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
117 T* Model::emplaceReplacement(LDObject* object, Args&& ...args)
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
118 {
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
119 if (object->model() == this)
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
120 {
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
121 int position = object->lineNumber();
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
122 T* replacement = constructObject<T>(args...);
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
123 setObjectAt(position, replacement);
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
124 return replacement;
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
125 }
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
126 else
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
127 return nullptr;
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
128 }
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
129
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
130 /*
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
131 * Like emplaceAt<>() but instead of inserting the constructed object, it replaces the document at the given spot instead.
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
132 * The replaced object is deleted in the process.
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
133 */
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
134 template<typename T, typename... Args>
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
135 T* Model::emplaceReplacementAt(int position, Args&& ...args)
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
136 {
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
137 T* replacement = constructObject<T>(args...);
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
138 setObjectAt(position, replacement);
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
139 return replacement;
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
140 }
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
141
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
142 /*
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
143 * Constructs an LDObject such that it gets this model as its model pointer.
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
144 */
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
145 template<typename T, typename... Args>
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
146 T* Model::constructObject(Args&& ...args)
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
147 {
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
148 static_assert (std::is_base_of<LDObject, T>::value, "Can only use this function with LDObject-derivatives");
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
149 T* object = new T {args..., this};
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
150
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
151 // Set default color. Relying on virtual functions, this cannot be done in the c-tor.
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
152 // TODO: store -1 as the default color
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
153 if (object->isColored())
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
154 object->setColor(object->defaultColor());
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
155
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
156 return object;
4a754362f660 Wrote documentation to the Model class.
Teemu Piippo <teemu@hecknology.net>
parents: 1087
diff changeset
157 }

mercurial