Serial-TCP/IPbridge  1.0
config.h
Go to the documentation of this file.
1 
12 #ifndef CONFIG_H
13 #define CONFIG_H
14 
15 #include <map>
16 #include <string>
17 #include <tr1/cstdint>
18 
19 #include "base_exception.h"
20 #include "conversions.h"
21 
28 class Config {
29  public:
34  public:
35  explicit InvalidValueError(const std::string &errorMessage):
36  BaseException(errorMessage) {}
37  };
38 
40  typedef std::map<std::string, std::string> RawConfigValues;
41 
43  typedef size_t BufferSize;
44 
46  typedef unsigned LengthField;
47 
49  typedef std::tr1::uint32_t LengthFieldMessageLength;
50 
51  public:
65  static Config fromRawConfigValues(RawConfigValues rawConfigValues);
66 
72  ~Config();
73 
81  Config(const Config &other);
82 
92  Config & operator=(const Config &other);
93 
101  bool operator==(const Config &other) const;
102 
110  bool operator!=(const Config &other) const;
111 
119  void swap(Config &other);
120 
122  size_t getBufferSize() const { return _bufferSize; }
123 
126 
138  static LengthFieldMessageLength binaryFieldLengthToNumber(const std::string &binaryNumber);
139 
148  static LengthFieldMessageLength computeMaximalMessageLength(unsigned lengthField);
149 
165  static std::string messageLengthToBinaryLengthField(LengthFieldMessageLength messageLength,
166  unsigned lengthField);
167 
168  protected:
174  Config();
175 
183  Config(RawConfigValues rawConfigValues);
184 
185  protected:
186 
199  template <typename T>
200  static T parseNumber(const std::string &optionName,
201  const std::string &rawValue, T minValue, T maxValue) {
202  T value = 0;
203 
204  // Check whether the rawValue is a valid unsigned value
205  // (I can use the trick toString(value) != rawValues because of
206  // the precondition of the fromRawConfigValues() function)
207  if (!strToNum(rawValue, value) || toString(value) != rawValue) {
208  throw InvalidValueError(std::string("TCP/IP configuration parsing error: invalid ") +
209  optionName + " (" + rawValue + ")");
210  }
211 
212  // Check bounds
213  if (value < minValue || value > maxValue) {
214  throw InvalidValueError(std::string("TCP/IP configuration parsing error: invalid ") +
215  optionName + " (" + rawValue + ") - " + optionName + " must be between " +
216  toString(minValue) + " and " + toString(maxValue));
217  }
218 
219  return value;
220  }
221 
222  private:
223  // All parsig functions have the same behaviour - they parses the
224  // selected value and returns it. They throw InvalidValueError if the
225  // value is invalid.
226  static BufferSize parseBufferSize(const std::string &rawBufferSize);
227  static LengthField parseLengthField(const std::string &rawLengthField);
228 
229  private:
234 
239 };
240 
241 #endif // #ifndef CONFIG_H
242 
243 // End of file config.h
size_t BufferSize
Buffer size type.
Definition: config.h:43
static LengthField parseLengthField(const std::string &rawLengthField)
static const LengthField DEFAULT_LENGTH_FIELD
Default length field (in bytes).
Definition: config.h:238
std::map< std::string, std::string > RawConfigValues
Raw configuration option -> values mapping.
Definition: config.h:40
Config & operator=(const Config &other)
Assignment operator.
static Config fromRawConfigValues(RawConfigValues rawConfigValues)
Creates a Config object from raw configuration values.
void swap(Config &other)
Swap the contents of this object with the other object.
bool operator!=(const Config &other) const
Non-equality comparison.
bool operator==(const Config &other) const
Equality comparison.
static LengthFieldMessageLength computeMaximalMessageLength(unsigned lengthField)
Returns the maximal message size that can be stored in lengthField bytes.
BaseException class - declarations.
Base class for all configuration classes.
Definition: config.h:28
LengthField getLengthField() const
Returns the size of a field containing the length of a message (in bytes).
Definition: config.h:125
InvalidValueError(const std::string &errorMessage)
Definition: config.h:35
Base exception class for all project exceptions.
Definition: base_exception.h:23
std::string toString(const T &value)
Converts the selected value into a string.
Definition: conversions.h:26
Various conversion functions for common usage - declarations.
static LengthFieldMessageLength binaryFieldLengthToNumber(const std::string &binaryNumber)
Transforms the selected binary string into a number.
size_t getBufferSize() const
Returns the buffer size (in bytes).
Definition: config.h:122
bool strToNum(const std::string &str, N &number, std::ios_base &(*format)(std::ios_base &)=std::dec)
Converts the selected string into a number.
Definition: conversions.h:95
~Config()
Destructor.
BufferSize _bufferSize
Buffer size (in bytes).
Definition: config.h:231
static BufferSize parseBufferSize(const std::string &rawBufferSize)
This exception should be thrown if some configuration value is invalid.
Definition: config.h:33
static std::string messageLengthToBinaryLengthField(LengthFieldMessageLength messageLength, unsigned lengthField)
Transforms the selected message length into a binary length field string.
unsigned LengthField
Length field type.
Definition: config.h:46
static const BufferSize DEFAULT_BUFFER_SIZE
Default buffer size (in bytes).
Definition: config.h:236
std::tr1::uint32_t LengthFieldMessageLength
Message length type when using length field.
Definition: config.h:49
static T parseNumber(const std::string &optionName, const std::string &rawValue, T minValue, T maxValue)
Parses the selected number from the selected string.
Definition: config.h:200
LengthField _lengthField
Size of a field containing the length of a message (in bytes).
Definition: config.h:233
Config()
Default constructor.