| 1 | // ========================================================= // |
|---|
| 2 | // // |
|---|
| 3 | // File : saiop.h // |
|---|
| 4 | // Purpose : operations on SAI // |
|---|
| 5 | // // |
|---|
| 6 | // Coded by Ralf Westram (coder@reallysoft.de) in Oct 19 // |
|---|
| 7 | // http://www.arb-home.de/ // |
|---|
| 8 | // // |
|---|
| 9 | // ========================================================= // |
|---|
| 10 | |
|---|
| 11 | #ifndef SAIOP_H |
|---|
| 12 | #define SAIOP_H |
|---|
| 13 | |
|---|
| 14 | #ifndef ARB_STRARRAY_H |
|---|
| 15 | #include <arb_strarray.h> |
|---|
| 16 | #endif |
|---|
| 17 | #ifndef ERRORORTYPE_H |
|---|
| 18 | #include <ErrorOrType.h> |
|---|
| 19 | #endif |
|---|
| 20 | #ifndef _GLIBCXX_STRING |
|---|
| 21 | #include <string> |
|---|
| 22 | #endif |
|---|
| 23 | #ifndef ARBDB_BASE_H |
|---|
| 24 | #include <arbdb_base.h> |
|---|
| 25 | #endif |
|---|
| 26 | #ifndef _GLIBCXX_VECTOR |
|---|
| 27 | #include <vector> |
|---|
| 28 | #endif |
|---|
| 29 | |
|---|
| 30 | #define sai_assert(cond) arb_assert(cond) |
|---|
| 31 | |
|---|
| 32 | class SaiOperator; |
|---|
| 33 | |
|---|
| 34 | typedef SmartPtr<SaiOperator> SaiOperatorPtr; |
|---|
| 35 | |
|---|
| 36 | typedef ErrorOr<std::string> ErrorOrString; |
|---|
| 37 | typedef ErrorOr<SaiOperatorPtr> ErrorOrSaiOperatorPtr; |
|---|
| 38 | |
|---|
| 39 | enum SaiOperatorType { |
|---|
| 40 | SOP_ACI, // N->1 |
|---|
| 41 | SOP_TRANSLATE, // 1->1 |
|---|
| 42 | SOP_MATRIX, // 2->1 |
|---|
| 43 | SOP_BOOLCHAIN, // N->1 |
|---|
| 44 | // (avoid changing order! int is saved to config) |
|---|
| 45 | }; |
|---|
| 46 | |
|---|
| 47 | #define SAI_OPERATOR_TYPES 4 |
|---|
| 48 | |
|---|
| 49 | class SaiCalcEnv : virtual Noncopyable { |
|---|
| 50 | const CharPtrArray& input; // reference to input data (not owned; has to persist while 'this' is used) |
|---|
| 51 | GBDATA *gb_main; // needed by SaiAciApplicator |
|---|
| 52 | |
|---|
| 53 | public: |
|---|
| 54 | SaiCalcEnv(const CharPtrArray& input_, GBDATA *gb_main_) : |
|---|
| 55 | input(input_), |
|---|
| 56 | gb_main(gb_main_) |
|---|
| 57 | {} |
|---|
| 58 | |
|---|
| 59 | const CharPtrArray& get_input() const { return input; } |
|---|
| 60 | GBDATA *get_gbmain() const { return gb_main; } |
|---|
| 61 | |
|---|
| 62 | GB_ERROR check_lengths_equal(size_t& len) const; |
|---|
| 63 | }; |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | class SaiOperator { |
|---|
| 67 | SaiOperatorType type; |
|---|
| 68 | |
|---|
| 69 | static const char *typeName[]; |
|---|
| 70 | |
|---|
| 71 | public: |
|---|
| 72 | SaiOperator(SaiOperatorType type_) : type(type_) {} |
|---|
| 73 | virtual ~SaiOperator() {} |
|---|
| 74 | |
|---|
| 75 | static ErrorOrSaiOperatorPtr make(SaiOperatorType type, const char *config); // factory |
|---|
| 76 | static const char *type_name(SaiOperatorType type) { return typeName[type]; } |
|---|
| 77 | |
|---|
| 78 | SaiOperatorType get_type() const { return type; } |
|---|
| 79 | virtual std::string get_config() const = 0; // generate config string |
|---|
| 80 | std::string get_description() const; |
|---|
| 81 | |
|---|
| 82 | virtual ErrorOrString apply(const SaiCalcEnv& calcEnv) const = 0; |
|---|
| 83 | }; |
|---|
| 84 | |
|---|
| 85 | // ----------------------------------- |
|---|
| 86 | // different operator types: |
|---|
| 87 | |
|---|
| 88 | class SaiTranslator FINAL_TYPE : public SaiOperator { |
|---|
| 89 | char transtab[256]; |
|---|
| 90 | |
|---|
| 91 | public: |
|---|
| 92 | SaiTranslator(char default_translation) : |
|---|
| 93 | SaiOperator(SOP_TRANSLATE) |
|---|
| 94 | { |
|---|
| 95 | transtab[0] = 0; |
|---|
| 96 | memset(transtab+1, default_translation, 255); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | static ErrorOrSaiOperatorPtr make(const char *config); |
|---|
| 100 | |
|---|
| 101 | std::string get_config() const OVERRIDE; |
|---|
| 102 | ErrorOrString apply(const SaiCalcEnv& calcEnv) const OVERRIDE; |
|---|
| 103 | |
|---|
| 104 | void addTranslation(const char *from, char to); |
|---|
| 105 | void deduceTranslations(class ConfigMapping& mapping) const; // order may differ from that defined via addTranslation() |
|---|
| 106 | }; |
|---|
| 107 | |
|---|
| 108 | class SaiMatrixTranslator FINAL_TYPE : public SaiOperator { |
|---|
| 109 | static const char DEFAULT_INDEX_CHAR = 'A'; // the index char of the default translator table |
|---|
| 110 | SaiOperatorPtr firstToIndexChar; // translate 1st SAI content to index character (index into vector 'secondToResult') |
|---|
| 111 | std::vector<SaiOperatorPtr> secondToResult; // translate 2nd SAI to result character |
|---|
| 112 | |
|---|
| 113 | SaiMatrixTranslator() : SaiOperator(SOP_MATRIX) {} // used by factory (make) |
|---|
| 114 | |
|---|
| 115 | public: |
|---|
| 116 | SaiMatrixTranslator(SaiOperatorPtr Default) : |
|---|
| 117 | SaiOperator(SOP_MATRIX), |
|---|
| 118 | firstToIndexChar(new SaiTranslator(DEFAULT_INDEX_CHAR)) |
|---|
| 119 | { |
|---|
| 120 | secondToResult.push_back(Default); |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | static ErrorOrSaiOperatorPtr make(const char *config); |
|---|
| 124 | |
|---|
| 125 | std::string get_config() const OVERRIDE; |
|---|
| 126 | ErrorOrString apply(const SaiCalcEnv& calcEnv) const OVERRIDE; |
|---|
| 127 | |
|---|
| 128 | void addOperator(const char *from, SaiOperatorPtr to); |
|---|
| 129 | }; |
|---|
| 130 | |
|---|
| 131 | enum SaiBoolOp { |
|---|
| 132 | SBO_FIRST, |
|---|
| 133 | SBO_AND, |
|---|
| 134 | SBO_OR, |
|---|
| 135 | SBO_XOR, |
|---|
| 136 | SBO_NAND, |
|---|
| 137 | SBO_NOR, |
|---|
| 138 | SBO_XNOR, |
|---|
| 139 | }; |
|---|
| 140 | |
|---|
| 141 | typedef SmartPtr<class SaiBoolRule> SaiBoolRulePtr; |
|---|
| 142 | typedef ErrorOr<SaiBoolRulePtr> ErrorOrSaiBoolRulePtr; |
|---|
| 143 | |
|---|
| 144 | class SaiBoolRule { |
|---|
| 145 | SaiBoolOp op; |
|---|
| 146 | bool specifyTrueChars; |
|---|
| 147 | std::string chars; |
|---|
| 148 | |
|---|
| 149 | bool charSpecified(char c) const { return chars.find_first_of(c) != std::string::npos; } |
|---|
| 150 | |
|---|
| 151 | public: |
|---|
| 152 | SaiBoolRule(SaiBoolOp op_, bool specifyTrueChars_, const char *chars_) : |
|---|
| 153 | op(op_), |
|---|
| 154 | specifyTrueChars(specifyTrueChars_), |
|---|
| 155 | chars(chars_) |
|---|
| 156 | {} |
|---|
| 157 | |
|---|
| 158 | SaiBoolOp get_op() const { return op; } |
|---|
| 159 | bool specifiesTrueChars() const { return specifyTrueChars; } |
|---|
| 160 | const char *get_chars() const { return chars.c_str(); } |
|---|
| 161 | |
|---|
| 162 | void prepare_input_data(const char *input, size_t len, char *output) const { |
|---|
| 163 | for (size_t p = 0; p<len; ++p) { |
|---|
| 164 | output[p] = charSpecified(input[p]) == specifyTrueChars ? '1' : '0'; |
|---|
| 165 | } |
|---|
| 166 | sai_assert(input[len] == 0); |
|---|
| 167 | output[len] = 0; |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | void apply(char *inout, const char *in, size_t len) const; |
|---|
| 171 | |
|---|
| 172 | static ErrorOrSaiBoolRulePtr make(const char *fromString); |
|---|
| 173 | std::string to_string() const; |
|---|
| 174 | }; |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | class SaiBoolchainOperator: public SaiOperator { |
|---|
| 178 | std::vector<SaiBoolRule> rule; |
|---|
| 179 | char outTrans[2]; |
|---|
| 180 | |
|---|
| 181 | public: |
|---|
| 182 | SaiBoolchainOperator(char c_0, char c_1) : |
|---|
| 183 | SaiOperator(SOP_BOOLCHAIN) |
|---|
| 184 | { |
|---|
| 185 | outTrans[0] = c_0; |
|---|
| 186 | outTrans[1] = c_1; |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | static ErrorOrSaiOperatorPtr make(const char *config); |
|---|
| 190 | |
|---|
| 191 | std::string get_config() const OVERRIDE; |
|---|
| 192 | ErrorOrString apply(const SaiCalcEnv& calcEnv) const OVERRIDE; |
|---|
| 193 | |
|---|
| 194 | void addRule(const SaiBoolRule& r) { |
|---|
| 195 | sai_assert(correlated(rule.empty(), r.get_op() == SBO_FIRST)); // 1st rule needs special operator type |
|---|
| 196 | rule.push_back(r); |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | #if defined(UNIT_TESTS) |
|---|
| 200 | void dropRule() { rule.pop_back(); } // shall only be used in unit-tests |
|---|
| 201 | #endif |
|---|
| 202 | |
|---|
| 203 | size_t count_rules() const { return rule.size(); } |
|---|
| 204 | const SaiBoolRule& getRule(size_t idx) const { |
|---|
| 205 | sai_assert(idx<rule.size()); |
|---|
| 206 | return rule[idx]; |
|---|
| 207 | } |
|---|
| 208 | char getOutTrans(bool i) const { return outTrans[i]; } |
|---|
| 209 | }; |
|---|
| 210 | |
|---|
| 211 | |
|---|
| 212 | class SaiAciApplicator : public SaiOperator { |
|---|
| 213 | std::string aci; |
|---|
| 214 | |
|---|
| 215 | public: |
|---|
| 216 | SaiAciApplicator(const char *aci_) : |
|---|
| 217 | SaiOperator(SOP_ACI), |
|---|
| 218 | aci(aci_) |
|---|
| 219 | {} |
|---|
| 220 | |
|---|
| 221 | static ErrorOrSaiOperatorPtr make(const char *config); |
|---|
| 222 | |
|---|
| 223 | std::string get_config() const OVERRIDE; |
|---|
| 224 | ErrorOrString apply(const SaiCalcEnv& calcEnv) const OVERRIDE; |
|---|
| 225 | |
|---|
| 226 | const std::string get_aci() const { return aci; } |
|---|
| 227 | }; |
|---|
| 228 | |
|---|
| 229 | |
|---|
| 230 | #else |
|---|
| 231 | #error saiop.h included twice |
|---|
| 232 | #endif // SAIOP_H |
|---|