RobotTestingFramework  2.0.1
Robot Testing Framework
Vocab.h
Go to the documentation of this file.
1 /*
2  * Robot Testing Framework
3  *
4  * Copyright (C) 2015-2019 Istituto Italiano di Tecnologia (IIT)
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 
22 #ifndef ROBOTTESTINGFRAMEWORK_VOCAB_H
23 #define ROBOTTESTINGFRAMEWORK_VOCAB_H
24 
25 #include <string>
26 
27 namespace shlibpp {
28 class Vocab;
29 
30 // We need a macro for efficient switching.
31 constexpr int32_t VOCAB(char a, char b = 0, char c = 0, char d = 0)
32 {
33  return ((((int32_t)(d)) << 24) + (((int32_t)(c)) << 16) + (((int32_t)(b)) << 8) + ((int32_t)(a)));
34 }
35 }
36 
37 
39 {
40 public:
41  static int encode(const std::string& s)
42  {
43  char a = '\0';
44  char b = '\0';
45  char c = '\0';
46  char d = '\0';
47  if (s.length() >= 1) {
48  a = s[0];
49  if (s.length() >= 2) {
50  b = s[1];
51  if (s.length() >= 3) {
52  c = s[2];
53  if (s.length() >= 4) {
54  d = s[3];
55  }
56  }
57  }
58  }
59  return VOCAB(a, b, c, d);
60  }
61 
62 
63  static std::string decode(int code)
64  {
65  std::string s;
66  for (int i = 0; i < 4; i++) {
67  int ch = code % 256;
68  if (ch > 0) {
69  s += ((char)ch);
70  }
71  code /= 256;
72  }
73  return s;
74  }
75 };
76 
77 #endif // ROBOTTESTINGFRAMEWORK_VOCAB_H
static int encode(const std::string &s)
Definition: Vocab.h:41
constexpr int32_t VOCAB(char a, char b=0, char c=0, char d=0)
Definition: Vocab.h:31
static std::string decode(int code)
Definition: Vocab.h:63