src/model.h

changeset 1073
a0a0d581309b
child 1074
a62f810ca26f
equal deleted inserted replaced
1072:9ce9496427f2 1073:a0a0d581309b
1 #pragma once
2 #include "main.h"
3 #include "ldObject.h"
4
5 class Model
6 {
7 public:
8 Model();
9 Model(const Model& other) = delete;
10 ~Model();
11
12 virtual void addObject(LDObject* object);
13 virtual void insertObject(int position, LDObject* object);
14 virtual bool swapObjects(LDObject* one, LDObject* other);
15 virtual bool setObjectAt(int idx, LDObject* obj);
16 void removeAt(int position);
17 void remove(LDObject* object);
18 void replace(LDObject *object, Model& model);
19 void clear();
20 void merge(Model& other, int position = -1);
21 void replace(LDObject* object, LDObject* newObject);
22 int size() const;
23 const QVector<LDObject*>& objects() const;
24 LDObject* getObject(int position) const;
25 void recountTriangles();
26 int triangleCount() const;
27 QVector<LDObject*>::iterator begin();
28 QVector<LDObject*>::iterator end();
29
30 template<typename T, typename... Args>
31 T* emplace(Args&& ...args)
32 {
33 T* object = constructObject<T>(args...);
34 addObject(object);
35 return object;
36 }
37
38 template<typename T, typename... Args>
39 T* emplaceAt(int position, Args&& ...args)
40 {
41 T* object = constructObject<T>(args...);
42 insertObject(position, object);
43 return object;
44 }
45
46 template<typename T, typename... Args>
47 T* emplaceReplacement(LDObject* object, Args&& ...args)
48 {
49 if (object->model() == this)
50 {
51 int position = object->lineNumber();
52 T* replacement = constructObject<T>(args...);
53 setObjectAt(position, replacement);
54 return replacement;
55 }
56 else
57 return nullptr;
58 }
59
60 protected:
61 template<typename T, typename... Args>
62 T* constructObject(Args&& ...args)
63 {
64 static_assert (std::is_base_of<LDObject, T>::value, "Can only use this function with LDObject-derivatives");
65 T* object = new T {args..., this};
66
67 // Set default color. Relying on virtual functions, this cannot be done in the c-tor.
68 // TODO: store -1 as the default color
69 if (object->isColored())
70 object->setColor(object->defaultColor());
71
72 return object;
73 }
74
75 void withdraw(LDObject* object);
76 virtual LDObject* withdrawAt(int position);
77
78 QVector<LDObject*> _objects;
79 mutable int _triangleCount = 0;
80 mutable bool _needsTriangleRecount;
81 };

mercurial