2
* To change this license header, choose License Headers in Project Properties.
3
* To change this template file, choose Tools | Templates
4
* and open the template in the editor.
11
* Created on February 19, 2018, 8:22 PM
23
#include <unordered_map>
25
namespace PetriEngine {
31
friend std::ostream& operator<< (std::ostream& stream, const Color& color);
34
const std::vector<const Color*> _tuple;
35
ColorType* _colorType;
36
std::string _colorName;
40
Color(ColorType* colorType, uint32_t id, std::vector<const Color*>& colors);
41
Color(ColorType* colorType, uint32_t id, const char* color);
44
bool isTuple() const {
45
return _tuple.size() > 1;
48
const std::string& getColorName() const {
49
if (this->isTuple()) {
50
throw "Cannot get color from a tuple color.";
55
ColorType* getColorType() const {
59
uint32_t getId() const {
63
const Color* operator[] (size_t index) const;
64
bool operator< (const Color& other) const;
65
bool operator> (const Color& other) const;
66
bool operator<= (const Color& other) const;
67
bool operator>= (const Color& other) const;
69
bool operator== (const Color& other) const {
70
return _colorType == other._colorType && _id == other._id;
72
bool operator!= (const Color& other) const {
73
return !((*this) == other);
76
const Color& operator++ () const;
77
const Color& operator-- () const;
79
std::string toString() const;
80
static std::string toString(const Color* color);
81
static std::string toString(const std::vector<const Color*>& colors);
85
* Singleton pattern from:
86
* https://stackoverflow.com/questions/1008019/c-singleton-design-pattern
88
class DotConstant : public Color {
93
static const Color* dotConstant() {
94
static DotConstant _instance;
99
DotConstant(DotConstant const&) = delete;
100
void operator=(DotConstant const&) = delete;
102
bool operator== (const DotConstant& other) {
115
iterator(ColorType& type, size_t index) : type(type), index(index) {}
117
const Color& operator++() {
118
return type[++index];
121
const Color& operator++(int) {
122
return type[index++];
125
const Color& operator--() {
126
return type[--index];
128
const Color& operator--(int) {
129
return type[index--];
132
const Color& operator*() {
136
const Color* operator->() {
140
bool operator==(iterator& other) {
141
return type == other.type && index == other.index;
144
bool operator!=(iterator& other) {
145
return !(type == other.type) || index != other.index;
150
std::vector<Color> _colors;
155
ColorType(std::vector<ColorType*> elements);
156
ColorType(std::string name = "Undefined") : _colors(), _name(std::move(name)) {
157
_id = (uintptr_t)this;
160
virtual void addColor(const char* colorName);
161
virtual void addColor(std::vector<const Color*>& colors);
162
virtual void addDot() {
163
_colors.push_back(*DotConstant::dotConstant());
166
virtual size_t size() const {
167
return _colors.size();
170
virtual const Color& operator[] (size_t index) {
171
return _colors[index];
174
virtual const Color& operator[] (int index) {
175
return _colors[index];
178
virtual const Color& operator[] (uint32_t index) {
179
return _colors[index];
182
virtual const Color* operator[] (const char* index);
184
virtual const Color* operator[] (const std::string& index) {
185
return (*this)[index.c_str()];
188
bool operator== (const ColorType& other) const {
189
return _id == other._id;
196
const std::string& getName() const {
205
return {*this, size()};
209
class ProductType : public ColorType {
211
std::vector<ColorType*> constituents;
212
std::unordered_map<size_t,Color> cache;
215
ProductType(const std::string& name = "Undefined") : ColorType(name) {}
220
void addType(ColorType* type) {
221
constituents.push_back(type);
224
void addColor(const char* colorName) override {}
225
void addColor(std::vector<const Color*>& colors) override {}
226
void addDot() override {}
228
size_t size() const override {
230
for (auto ct : constituents) {
231
product *= ct->size();
236
bool containsTypes(const std::vector<const ColorType*>& types) const {
237
if (constituents.size() != types.size()) return false;
239
for (size_t i = 0; i < constituents.size(); ++i) {
240
if (!(*constituents[i] == *types[i])) {
248
const Color* getColor(const std::vector<const Color*>& colors);
250
const Color& operator[](size_t index) override;
251
const Color& operator[](int index) override {
252
return operator[]((size_t)index);
254
const Color& operator[](uint32_t index) override {
255
return operator[]((size_t)index);
258
const Color* operator[](const char* index) override;
259
const Color* operator[](const std::string& index) override;
264
ColorType* colorType;
271
bool operator==(Binding& other) {
272
return var->name.compare(other.var->name);
278
#endif /* COLORS_H */