1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
2 | // Copyright (C) 1999-2015 Maciej Komosinski and Szymon Ulatowski. |
---|
3 | // See LICENSE.txt for details. |
---|
4 | |
---|
5 | #ifndef _GENO_H_ |
---|
6 | #define _GENO_H_ |
---|
7 | |
---|
8 | #include <frams/util/sstring.h> |
---|
9 | #include <frams/util/extvalue.h> |
---|
10 | |
---|
11 | class MultiMap; |
---|
12 | class Geno; |
---|
13 | class GenoConvManager; |
---|
14 | |
---|
15 | class GenoValidator |
---|
16 | { |
---|
17 | public: |
---|
18 | virtual int testGenoValidity(Geno& g) = 0;/// -1=no information 0=invalid 1=valid |
---|
19 | virtual ~GenoValidator() { }; |
---|
20 | }; |
---|
21 | |
---|
22 | /// Basic GenoValidator that works by building a Model from any Geno (by converting to f0). |
---|
23 | /// Validation fails when the model can't be built or the genotype can't be converted. |
---|
24 | class ModelGenoValidator : public GenoValidator |
---|
25 | { |
---|
26 | public: |
---|
27 | int testGenoValidity(Geno& g); |
---|
28 | }; |
---|
29 | |
---|
30 | /// basic information about a single genotype. |
---|
31 | class Geno : public DestrBase |
---|
32 | { |
---|
33 | friend class Simulator;//needs to access validators directly |
---|
34 | SString gen; |
---|
35 | SString name; |
---|
36 | char format; |
---|
37 | SString txt; |
---|
38 | int isvalid; ///< <0 -> unknown >=0 -> value for "isValid" |
---|
39 | |
---|
40 | Geno *f0gen; |
---|
41 | |
---|
42 | int mapinshift; /// number of characters in the initial comment |
---|
43 | int mapoutshift; /// number of characters in the output comment |
---|
44 | int multiline; |
---|
45 | |
---|
46 | void init(const SString& genstring, char genformat, const SString& genname, const SString& comment); |
---|
47 | void validate(void); |
---|
48 | |
---|
49 | void freeF0(); |
---|
50 | |
---|
51 | bool isInvalid() { return isvalid == 0; } |
---|
52 | |
---|
53 | friend class Model; |
---|
54 | friend class GenoConvManager; |
---|
55 | |
---|
56 | public: |
---|
57 | typedef SListTempl<GenoValidator*> Validators; |
---|
58 | |
---|
59 | /// create a genotype object from primitives |
---|
60 | /// @param genstring pure genotype, without any comments |
---|
61 | /// @param genformat genotype format |
---|
62 | /// @param comment information about genotype (for genetic operators and "history") |
---|
63 | Geno(const char *genstring = 0, char genformat = -1, const char *genname = 0, const char *comment = 0); |
---|
64 | |
---|
65 | /// create a genotype object from primitives |
---|
66 | /// @param genstring pure genotype, wihtout any comments |
---|
67 | /// @param genformat genotype format |
---|
68 | /// @param name genotype name, new name will generated if needed |
---|
69 | /// @param comment information about genotype (for genetic operators and "history") |
---|
70 | Geno(const SString& genstring, char genformat, const SString& genname, const SString& comment); |
---|
71 | |
---|
72 | /// create object from full string, containing optional format and comment information |
---|
73 | Geno(const SString & fullstring); |
---|
74 | |
---|
75 | /// clone |
---|
76 | Geno(const Geno& src); |
---|
77 | |
---|
78 | void operator=(const Geno& src); |
---|
79 | |
---|
80 | ~Geno(); |
---|
81 | |
---|
82 | void setValid(int v) { isvalid = v; } |
---|
83 | int getValid() { return isvalid; } |
---|
84 | |
---|
85 | /// return string representation, with format comment at the beginning |
---|
86 | SString toString(void) const; |
---|
87 | SString shortString(void) const; |
---|
88 | |
---|
89 | void setString(const SString& genewithcomments); |
---|
90 | |
---|
91 | /** @param newformat=-1 -> don't change */ |
---|
92 | void setGene(const SString& g, char newformat = -1); |
---|
93 | SString getGene(void) const; |
---|
94 | |
---|
95 | SString getName(void) const; |
---|
96 | void setName(const SString&); |
---|
97 | char getFormat(void) const; |
---|
98 | |
---|
99 | SString getComment(void) const; |
---|
100 | void setComment(const SString&); |
---|
101 | |
---|
102 | /// invalid genotype cannot be used to build a creature |
---|
103 | bool isValid(void); |
---|
104 | |
---|
105 | /// make converted version of the genotype. |
---|
106 | /// @param converter_missing optional output parameter (ignored when NULL). Receives true if the conversion fails because of the lack of appropriate converter(s) (the returned Geno is always invalid in this case). Receives false if the genotype was converted by a converter or a converter chain (the returned Geno can be valid or invalid, depending on the converter's decision). |
---|
107 | Geno getConverted(char otherformat, MultiMap *m = 0, bool *converter_missing = NULL); |
---|
108 | |
---|
109 | /// @return -1 = before first char in the string |
---|
110 | /// @return -2 = after last char in the string |
---|
111 | int mapGenToString(int genpos) const; |
---|
112 | /// @return -1 = before first char in the genotype |
---|
113 | /// @return -2 = after last char in the genotype |
---|
114 | int mapStringToGen(int stringpos) const; |
---|
115 | |
---|
116 | int operator==(const Geno &g) { return (format == g.format) && (gen == g.gen); } |
---|
117 | |
---|
118 | void* owner; |
---|
119 | |
---|
120 | // managing global Geno-related objects (used for validation and conversion) |
---|
121 | static Validators* useValidators(Validators* val); |
---|
122 | static Validators* getValidators(); |
---|
123 | |
---|
124 | static GenoConvManager* useConverters(GenoConvManager* gcm); |
---|
125 | static GenoConvManager* getConverters(); |
---|
126 | }; |
---|
127 | |
---|
128 | #ifndef NO_GENOCONVMANAGER |
---|
129 | #include "genoconv.h" |
---|
130 | #endif |
---|
131 | |
---|
132 | #endif |
---|