/usr/include/ola/messaging/Descriptor.h is in libola-dev 0.9.8-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 | /*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Descriptor.h
* Holds the metadata (schema) for a Message.
* Copyright (C) 2011 Simon Newton
*/
#ifndef INCLUDE_OLA_MESSAGING_DESCRIPTOR_H_
#define INCLUDE_OLA_MESSAGING_DESCRIPTOR_H_
#include <ola/messaging/DescriptorVisitor.h>
#include <ola/network/IPV4Address.h>
#include <ola/network/MACAddress.h>
#include <ola/rdm/UID.h>
#include <map>
#include <string>
#include <vector>
#include <utility>
namespace ola {
namespace messaging {
class FieldDescriptorVisitor;
/**
* Describes a field, which may be a group of sub fields.
*/
class FieldDescriptorInterface {
public:
virtual ~FieldDescriptorInterface() {}
// Returns the name of this field
virtual const std::string& Name() const = 0;
// Call back into a FieldDescriptorVisitor
virtual void Accept(FieldDescriptorVisitor *visitor) const = 0;
// Returns true if the size of this field is constant
virtual bool FixedSize() const = 0;
// True if there is some bound on the field's size.
virtual bool LimitedSize() const = 0;
// This is the max size in bytes of the field. This is only valid if
// LimitedSize() is true, otherwise it returns 0.
virtual unsigned int MaxSize() const = 0;
};
/**
* The base implementation of a field.
*/
class FieldDescriptor: public FieldDescriptorInterface {
public:
explicit FieldDescriptor(const std::string &name)
: m_name(name) {
}
virtual ~FieldDescriptor() {}
// Returns the name of this field
const std::string& Name() const { return m_name; }
private:
std::string m_name;
};
/**
* A FieldDescriptor that represents a bool
*/
class BoolFieldDescriptor: public FieldDescriptor {
public:
explicit BoolFieldDescriptor(const std::string &name)
: FieldDescriptor(name) {
}
bool FixedSize() const { return true; }
bool LimitedSize() const { return true; }
unsigned int MaxSize() const { return 1; }
void Accept(FieldDescriptorVisitor *visitor) const {
visitor->Visit(this);
}
};
/**
* A FieldDescriptor that represents a IPv4 Address
*/
class IPV4FieldDescriptor: public FieldDescriptor {
public:
explicit IPV4FieldDescriptor(const std::string &name)
: FieldDescriptor(name) {
}
bool FixedSize() const { return true; }
bool LimitedSize() const { return true; }
unsigned int MaxSize() const { return ola::network::IPV4Address::LENGTH; }
void Accept(FieldDescriptorVisitor *visitor) const {
visitor->Visit(this);
}
};
/**
* A FieldDescriptor that represents a MAC Address
*/
class MACFieldDescriptor: public FieldDescriptor {
public:
explicit MACFieldDescriptor(const std::string &name)
: FieldDescriptor(name) {
}
bool FixedSize() const { return true; }
bool LimitedSize() const { return true; }
unsigned int MaxSize() const { return ola::network::MACAddress::LENGTH; }
void Accept(FieldDescriptorVisitor *visitor) const {
visitor->Visit(this);
}
};
/**
* A FieldDescriptor that represents a UID
*/
class UIDFieldDescriptor: public FieldDescriptor {
public:
explicit UIDFieldDescriptor(const std::string &name)
: FieldDescriptor(name) {
}
bool FixedSize() const { return true; }
bool LimitedSize() const { return true; }
unsigned int MaxSize() const { return ola::rdm::UID::LENGTH; }
void Accept(FieldDescriptorVisitor *visitor) const {
visitor->Visit(this);
}
};
/**
* A FieldDescriptor that represents a string
*/
class StringFieldDescriptor: public FieldDescriptor {
public:
StringFieldDescriptor(const std::string &name,
uint8_t min_size,
uint8_t max_size)
: FieldDescriptor(name),
m_min_size(min_size),
m_max_size(max_size) {
}
bool FixedSize() const { return m_min_size == m_max_size; }
bool LimitedSize() const { return true; }
unsigned int MinSize() const { return m_min_size; }
unsigned int MaxSize() const { return m_max_size; }
void Accept(FieldDescriptorVisitor *visitor) const {
visitor->Visit(this);
}
private:
uint8_t m_min_size, m_max_size;
};
/**
* A FieldDescriptor that represents an integer type.
*
* Intervals are closed (include the endpoints).
*/
template <typename type>
class IntegerFieldDescriptor: public FieldDescriptor {
public:
typedef std::pair<type, type> Interval;
typedef std::vector<std::pair<type, type> > IntervalVector;
typedef std::map<std::string, type> LabeledValues;
IntegerFieldDescriptor(const std::string &name,
bool little_endian = false,
int8_t multiplier = 0)
: FieldDescriptor(name),
m_little_endian(little_endian),
m_multipler(multiplier) {
}
IntegerFieldDescriptor(const std::string &name,
const IntervalVector &intervals,
const LabeledValues &labels,
bool little_endian = false,
int8_t multiplier = 0)
: FieldDescriptor(name),
m_little_endian(little_endian),
m_multipler(multiplier),
m_intervals(intervals),
m_labels(labels) {
}
bool FixedSize() const { return true; }
bool LimitedSize() const { return true; }
unsigned int MaxSize() const { return sizeof(type); }
int8_t Multiplier() const { return m_multipler; }
bool IsLittleEndian() const { return m_little_endian; }
const IntervalVector &Intervals() const { return m_intervals; }
bool IsValid(type value) const {
if (m_intervals.empty())
return true;
typename IntervalVector::const_iterator iter = m_intervals.begin();
for (; iter != m_intervals.end(); ++iter) {
if (value >= iter->first && value <= iter->second)
return true;
}
return false;
}
const LabeledValues &Labels() const { return m_labels; }
bool LookupLabel(const std::string &label, type *value) const {
typename LabeledValues::const_iterator iter = m_labels.find(label);
if (iter == m_labels.end())
return false;
*value = iter->second;
return true;
}
const std::string LookupValue(type value) const {
typename LabeledValues::const_iterator iter = m_labels.begin();
for (; iter != m_labels.end(); ++iter) {
if (iter->second == value)
return iter->first;
}
return "";
}
void Accept(FieldDescriptorVisitor *visitor) const {
visitor->Visit(this);
}
private:
bool m_little_endian;
int8_t m_multipler;
IntervalVector m_intervals;
LabeledValues m_labels;
};
typedef IntegerFieldDescriptor<uint8_t> UInt8FieldDescriptor;
typedef IntegerFieldDescriptor<uint16_t> UInt16FieldDescriptor;
typedef IntegerFieldDescriptor<uint32_t> UInt32FieldDescriptor;
typedef IntegerFieldDescriptor<int8_t> Int8FieldDescriptor;
typedef IntegerFieldDescriptor<int16_t> Int16FieldDescriptor;
typedef IntegerFieldDescriptor<int32_t> Int32FieldDescriptor;
/**
* A FieldDescriptor that consists of a group of FieldDescriptors. Groups can
* vary in size two ways. First, the group may contain a field which itself is
* of variable size (i.e. a string or another group). This type of message
* structure requires some other data in the message itself to indicate the
* field/group length and as such isn't supported.
*
* An example of this type of group would be:
*
* +----------------+
* | bool (1) |
* +----------------+
* | string (0, 32) |
* +----------------+
*
* This could hold data like:
* (true, "foo"),
* (false, "bar)
*
* The second (and simpler) type is where the group size is fixed (i.e.
* contains only fixed length fields) and the number of times the group appears
* in the message varies. By knowing the length of the message we can work out
* the number of times a group occurs (see VariableFieldSizeCalculator.h which
* does this).
*
* An example of this type of group would be:
*
* +----------------+
* | bool (1) |
* +----------------+
* | uint16 (2) |
* +----------------+
*
* This could hold data like:
* (true, 1000),
* (false, 34)
*
* DescriptorConsistencyChecker.h checks that a descriptor is the second type,
* that is contains at most one variable-sized field.
*
* We refer to the datatypes within a group as fields, the actual
* instantiations of a group as blocks. In the examples above, bool, string and
* uint16 are the fields (represented by FieldDescriptorInterface objects) and
* (true, "foo") & (true, 1000) are the blocks.
*/
class FieldDescriptorGroup: public FieldDescriptor {
public:
static const int16_t UNLIMITED_BLOCKS;
FieldDescriptorGroup(const std::string &name,
const std::vector<const FieldDescriptor*> &fields,
uint16_t min_blocks,
int16_t max_blocks)
: FieldDescriptor(name),
m_fields(fields),
m_min_blocks(min_blocks),
m_max_blocks(max_blocks),
m_populated(false),
m_fixed_size(true),
m_limited_size(true),
m_block_size(0),
m_max_block_size(0) {
}
virtual ~FieldDescriptorGroup();
// This is true iff all fields in a group are of a fixed size and the
// number of blocks is fixed
bool FixedSize() const { return FixedBlockSize() && FixedBlockCount(); }
// True if the number of blocks has some bound, and all fields also have
// some bound.
bool LimitedSize() const;
// This is the max size of the group, which is only valid if LimitedSize()
// is true, otherwise it returns 0.
unsigned int MaxSize() const;
// Field information
// the number of fields in this group
unsigned int FieldCount() const { return m_fields.size(); }
// True if all the fields in this group are a fixed size. This is then a
// type 2 group as described above.
bool FixedBlockSize() const;
// If this block size is fixed, this returns the size of a single block,
// otherwise it returns 0;
unsigned int BlockSize() const;
// If this block size is bounded, this returns the size of the block.
unsigned int MaxBlockSize() const;
// Blocks
// The minimum number of blocks, usually 0 or 1.
uint16_t MinBlocks() const { return m_min_blocks; }
// A max size of UNLIMITED_BLOCKS means no restrictions on the number of
// blocks
int16_t MaxBlocks() const { return m_max_blocks; }
// True if the block count is fixed.
bool FixedBlockCount() const { return m_min_blocks == m_max_blocks; }
const class FieldDescriptor *GetField(unsigned int index) const {
if (index < m_fields.size())
return m_fields[index];
return NULL;
}
virtual void Accept(FieldDescriptorVisitor *visitor) const;
protected:
std::vector<const class FieldDescriptor *> m_fields;
private:
uint16_t m_min_blocks;
int16_t m_max_blocks;
mutable bool m_populated;
mutable bool m_fixed_size, m_limited_size;
mutable unsigned int m_block_size, m_max_block_size;
void PopulateIfRequired() const;
};
/**
* A descriptor is a group of fields which can't be repeated
*/
class Descriptor: public FieldDescriptorGroup {
public:
Descriptor(const std::string &name,
const std::vector<const FieldDescriptor*> &fields)
: FieldDescriptorGroup(name, fields, 1, 1) {}
void Accept(FieldDescriptorVisitor *visitor) const;
};
} // namespace messaging
} // namespace ola
#endif // INCLUDE_OLA_MESSAGING_DESCRIPTOR_H_
|