ARB
saiop.h
Go to the documentation of this file.
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 
33 
35 
38 
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
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) :
94  {
95  transtab[0] = 0;
96  memset(transtab+1, default_translation, 255);
97  }
98 
99  static ErrorOrSaiOperatorPtr make(const char *config);
100 
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:
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 {
139 };
140 
143 
144 class SaiBoolRule {
145  SaiBoolOp op;
146  bool specifyTrueChars;
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
static const char * type_name(SaiOperatorType type)
Definition: saiop.h:76
const char * GB_ERROR
Definition: arb_core.h:25
virtual ErrorOrString apply(const SaiCalcEnv &calcEnv) const =0
return string(buffer, length)
SaiMatrixTranslator(SaiOperatorPtr Default)
Definition: saiop.h:116
std::string get_description() const
Definition: saiop.cxx:56
ErrorOr< std::string > ErrorOrString
Definition: saiop.h:36
virtual std::string get_config() const =0
STL namespace.
char getOutTrans(bool i) const
Definition: saiop.h:208
GB_ERROR check_lengths_equal(size_t &len) const
Definition: saiop.cxx:22
SaiCalcEnv(const CharPtrArray &input_, GBDATA *gb_main_)
Definition: saiop.h:54
static ErrorOrSaiOperatorPtr make(SaiOperatorType type, const char *config)
Definition: saiop.cxx:46
int chars
Definition: seq_search.cxx:38
Definition: saiop.h:137
Definition: saiop.h:134
const SaiBoolRule & getRule(size_t idx) const
Definition: saiop.h:204
const CharPtrArray & get_input() const
Definition: saiop.h:59
size_t count_rules() const
Definition: saiop.h:203
void prepare_input_data(const char *input, size_t len, char *output) const
Definition: saiop.h:162
SaiBoolOp get_op() const
Definition: saiop.h:158
SaiOperatorType
Definition: saiop.h:39
SaiOperatorType get_type() const
Definition: saiop.h:78
GBDATA * get_gbmain() const
Definition: saiop.h:60
SaiAciApplicator(const char *aci_)
Definition: saiop.h:216
SaiBoolchainOperator(char c_0, char c_1)
Definition: saiop.h:182
const char * get_chars() const
Definition: saiop.h:160
Definition: saiop.h:40
xml element
#define sai_assert(cond)
Definition: saiop.h:30
#define OVERRIDE
Definition: cxxforward.h:93
SmartPtr< SaiOperator > SaiOperatorPtr
Definition: saiop.h:32
virtual ~SaiOperator()
Definition: saiop.h:73
SmartPtr< class SaiBoolRule > SaiBoolRulePtr
Definition: saiop.h:141
SaiBoolOp
Definition: saiop.h:131
Definition: saiop.h:135
SaiBoolRule(SaiBoolOp op_, bool specifyTrueChars_, const char *chars_)
Definition: saiop.h:152
Definition: saiop.h:133
bool specifiesTrueChars() const
Definition: saiop.h:159
SaiOperator(SaiOperatorType type_)
Definition: saiop.h:72
ErrorOr< SaiOperatorPtr > ErrorOrSaiOperatorPtr
Definition: saiop.h:37
ErrorOr< SaiBoolRulePtr > ErrorOrSaiBoolRulePtr
Definition: saiop.h:142
Definition: output.h:28
SaiTranslator(char default_translation)
Definition: saiop.h:92