libUTL++
Translator.h
1 #pragma once
2 
4 
5 #include <libutl/AutoPtr.h>
6 #include <libutl/Encoder.h>
7 #include <libutl/Hashtable.h>
8 #include <libutl/Mapping.h>
9 #include <libutl/MemStream.h>
10 #include <libutl/Pair.h>
11 
13 
14 UTL_NS_BEGIN;
15 
17 // Translator //////////////////////////////////////////////////////////////////////////////////////
19 
30 
32 class Translator : public Encoder
33 {
35 
36 public:
47  Translator(Stream* stream,
48  bool owner = true,
49  Mapping* mapping = nullptr,
50  bool mappingOwner = true,
51  bool rememberMappings = false,
52  char prefix = '$')
53  {
54  set(stream, owner, mapping, mappingOwner, rememberMappings, prefix);
55  }
56 
62  void
63  add(const String& lhs, const String& rhs)
64  {
65  _xlats += new Pair(lhs.clone(), rhs.clone());
66  }
67 
68  void clear();
69 
70  virtual void
71  dump(Stream& os, uint_t level = uint_t_max) const
72  {
73  _xlats.dump(os, level);
74  }
75 
76  virtual size_t encode(const byte_t* block, size_t num);
77 
78  virtual size_t
79  decode(byte_t* block, size_t num)
80  {
81  ABORT();
82  return 0;
83  }
84 
86  const Mapping*
87  getMapping() const
88  {
89  return _mapping;
90  }
91 
93  bool
95  {
96  return getFlag(flg_rememberMappings);
97  }
98 
100  bool
102  {
103  return getFlag(flg_mappingOwner);
104  }
105 
116  void set(Stream* stream,
117  bool owner = true,
118  Mapping* mapping = nullptr,
119  bool mappingOwner = true,
120  bool rememberMappings = false,
121  char prefix = '$');
122 
130  void
131  setMapping(Mapping* mapping, bool mappingOwner = true, bool rememberMappings = false)
132  {
133  if (isMappingOwner()) delete _mapping;
134  _mapping = mapping;
135  setFlag(flg_mappingOwner, mappingOwner);
136  setFlag(flg_rememberMappings, rememberMappings);
137  }
138 
139 private:
140  void
141  init()
142  {
143  _mapping = nullptr;
144  }
145 
146  void
147  deInit()
148  {
149  setMapping(nullptr);
150  }
151 
152  const String* translate(const String& token);
153 
154 private:
155  enum flg_t
156  {
157  flg_mappingOwner = 12,
158  flg_rememberMappings
159  };
160 
161 private:
162  String _prefix;
163  Hashtable _xlats;
164  Mapping* _mapping;
165  AutoPtr<> _mappedObject;
166 };
167 
169 // StringTranslator ////////////////////////////////////////////////////////////////////////////////
171 
182 
184 class StringTranslator : public Object
185 {
187 
188 public:
189  StringTranslator(Mapping* mapping,
190  bool mappingOwner = true,
191  bool rememberMappings = true,
192  char prefix = '$')
193  {
194  set(mapping, mappingOwner, rememberMappings, prefix);
195  }
196 
205  void set(Mapping* mapping,
206  bool mappingOwner = true,
207  bool rememberMappings = true,
208  char prefix = '$');
209 
215  void
216  add(const String& lhs, const String& rhs)
217  {
218  _translator.add(lhs, rhs);
219  }
220 
221  virtual void
222  dump(Stream& os, uint_t level = uint_t_max) const
223  {
224  _translator.dump(os, level);
225  }
226 
232  String translate(const String& str);
233 
234 private:
235  void
236  init()
237  {
238  set(nullptr);
239  }
240 
241  void
242  deInit()
243  {
244  _translator.set(nullptr);
245  }
246 
247 private:
248  Translator _translator;
249  MemStream _ms;
250 };
251 
253 
254 UTL_NS_END;
void deInit()
De-initialize UTL++.
Encoder/decoder abstraction.
Definition: Encoder.h:39
#define UTL_CLASS_DECL(DC, BC)
Declaration of standard UTL++ functionality for a non-template class.
Definition: macros.h:688
unsigned char byte_t
Unsigned character.
Definition: types.h:31
void add(const String &lhs, const String &rhs)
Add a mapping.
Definition: Translator.h:63
Character string.
Definition: String.h:31
void setMapping(Mapping *mapping, bool mappingOwner=true, bool rememberMappings=false)
Set the mapping object.
Definition: Translator.h:131
virtual size_t decode(byte_t *block, size_t num)
Decode data from the associated stream.
Definition: Translator.h:79
Simple translator.
Definition: Translator.h:32
void add(const String &lhs, const String &rhs)
Add a mapping.
Definition: Translator.h:216
virtual void dump(Stream &os, uint_t level=uint_t_max) const
Dump a human-readable representation of self to the given output stream.
Definition: Translator.h:222
Memory stream.
Definition: MemStream.h:40
unsigned int uint_t
Unsigned integer.
Definition: types.h:59
bool isMappingOwner() const
Get the mappingOwner flag.
Definition: Translator.h:101
String translator.
Definition: Translator.h:184
#define ABORT()
Immediately terminates the program.
Definition: macros.h:59
const uint_t uint_t_max
Maximum uint_t value.
Stream I/O abstraction.
Definition: Stream.h:68
Abstraction for object->object mapping.
Definition: Mapping.h:28
Chained hashing collection.
Definition: Hashtable.h:92
Translator(Stream *stream, bool owner=true, Mapping *mapping=nullptr, bool mappingOwner=true, bool rememberMappings=false, char prefix='$')
Constructor.
Definition: Translator.h:47
virtual void dump(Stream &os, uint_t level=uint_t_max) const
Dump a human-readable representation of self to the given output stream.
Definition: Translator.h:71
Simple container for two objects.
Definition: Pair.h:25
const Mapping * getMapping() const
Get the mapping.
Definition: Translator.h:87
bool getRememberMappings() const
Get the rememberMappings flag.
Definition: Translator.h:94
Root of UTL++ class hierarchy.
Definition: Object.h:52
A "smart" pointer that can also be dumb.
Definition: AutoPtr.h:32
void init()
Initialize UTL++.