1 #ifndef ENTITY 2 #define ENTITY 3 4 #include <map> 5 #include <string> 6 7 enum class Type { 8 INT, 9 ULONG, 10 BOOL, 11 DECIMAL, 12 COLOR, 13 STRING, 14 REGEX, 15 NAMES, // a enum type containing names 16 FONT, 17 RECTANGLE, 18 }; 19 20 static const std::map<Type, std::pair<std::string, char>> type_strings = { 21 {Type::INT, {"Integer", 'i'}}, 22 {Type::ULONG, {"Unsigned", 'u'}}, 23 {Type::BOOL, {"Boolean", 'b'}}, 24 {Type::DECIMAL, {"Decimal", 'd'}}, 25 {Type::COLOR, {"Color", 'c'}}, 26 {Type::STRING, {"String", 's'}}, 27 {Type::REGEX, {"Regex", 'r'}}, 28 {Type::NAMES, {"Names", 'n'}}, 29 {Type::FONT, {"Font", 'f'}}, 30 {Type::RECTANGLE, {"Rectangle", 'R'}}, 31 }; 32 33 bool operator<(Type t1, Type t2); 34 35 class HasDocumentation { 36 public: setDoc(const char text[])37 void setDoc(const char text[]) { doc_ = text; } doc()38 std::string doc() const { return doc_ ? doc_ : ""; }; 39 private: 40 /** we avoid the duplication of the doc string 41 * with every instance of the owning class. 42 */ 43 const char* doc_ = nullptr; 44 }; 45 46 class Entity { 47 public: 48 Entity() = default; Entity(const std::string & name)49 Entity(const std::string &name) : name_(name) {} 50 virtual ~Entity() = default; 51 name()52 std::string name() const { return name_; } 53 virtual Type type() = 0; typestr(Type type)54 static std::string typestr(Type type) { 55 return type_strings.at(type).first; 56 } typestr()57 std::string typestr() { return typestr(type()); } 58 typechar(Type type)59 static char typechar(Type type) { 60 return type_strings.at(type).second; 61 } typechar()62 char typechar() { return typechar(type()); } 63 64 protected: 65 std::string name_; 66 }; 67 68 /* // not yet supported 69 class Symlink : public Entity { 70 public: 71 Symlink(const std::string name, std::shared_ptr<Entity> target) 72 : Entity(name), target_(target) {} 73 74 Type type() { return Type::SYMLINK; } 75 std::shared_ptr<Entity> follow() { return target_; } 76 77 private: 78 std::shared_ptr<Entity> target_; 79 }; 80 */ 81 82 83 #endif // ENTITY 84 85