21 #ifndef INCLUDE_OLA_MESSAGING_DESCRIPTOR_H_
22 #define INCLUDE_OLA_MESSAGING_DESCRIPTOR_H_
24 #include <ola/messaging/DescriptorVisitor.h>
36 class FieldDescriptorVisitor;
46 virtual const string& Name()
const = 0;
52 virtual bool FixedSize()
const = 0;
55 virtual bool LimitedSize()
const = 0;
59 virtual unsigned int MaxSize()
const = 0;
74 const string& Name()
const {
return m_name; }
90 bool FixedSize()
const {
return true; }
91 bool LimitedSize()
const {
return true; }
92 unsigned int MaxSize()
const {
return 1; }
109 bool FixedSize()
const {
return true; }
110 bool LimitedSize()
const {
return true; }
111 unsigned int MaxSize()
const {
return 4; }
114 visitor->Visit(
this);
128 bool FixedSize()
const {
return true; }
129 bool LimitedSize()
const {
return true; }
130 unsigned int MaxSize()
const {
return 6; }
133 visitor->Visit(
this);
147 m_min_size(min_size),
148 m_max_size(max_size) {
151 bool FixedSize()
const {
return m_min_size == m_max_size; }
152 bool LimitedSize()
const {
return true; }
153 unsigned int MinSize()
const {
return m_min_size; }
154 unsigned int MaxSize()
const {
return m_max_size; }
157 visitor->Visit(
this);
161 uint8_t m_min_size, m_max_size;
170 template <
typename type>
173 typedef std::pair<type, type> Interval;
174 typedef vector<std::pair<type, type> > IntervalVector;
175 typedef std::map<string, type> LabeledValues;
178 bool little_endian =
false,
179 int8_t multiplier = 0)
181 m_little_endian(little_endian),
182 m_multipler(multiplier) {
186 const IntervalVector &intervals,
187 const LabeledValues &labels,
188 bool little_endian =
false,
189 int8_t multiplier = 0)
191 m_little_endian(little_endian),
192 m_multipler(multiplier),
193 m_intervals(intervals),
197 bool FixedSize()
const {
return true; }
198 bool LimitedSize()
const {
return true; }
199 unsigned int MaxSize()
const {
return sizeof(type); }
200 int8_t Multiplier()
const {
return m_multipler; }
201 bool IsLittleEndian()
const {
return m_little_endian; }
203 const IntervalVector &Intervals()
const {
return m_intervals; }
205 bool IsValid(type value)
const {
206 if (m_intervals.empty())
209 typename IntervalVector::const_iterator iter = m_intervals.begin();
210 for (; iter != m_intervals.end(); ++iter) {
211 if (value >= iter->first && value <= iter->second)
217 const LabeledValues &Labels()
const {
return m_labels; }
219 bool LookupLabel(
const string &label, type *value)
const {
220 typename LabeledValues::const_iterator iter = m_labels.find(label);
221 if (iter == m_labels.end())
223 *value = iter->second;
227 const string LookupValue(type value)
const {
228 typename LabeledValues::const_iterator iter = m_labels.begin();
229 for (; iter != m_labels.end(); ++iter) {
230 if (iter->second == value)
237 visitor->Visit(
this);
241 bool m_little_endian;
243 IntervalVector m_intervals;
244 LabeledValues m_labels;
303 static const int16_t UNLIMITED_BLOCKS;
306 const vector<const FieldDescriptor*> &fields,
311 m_min_blocks(min_blocks),
312 m_max_blocks(max_blocks),
315 m_limited_size(
true),
317 m_max_block_size(0) {
323 bool FixedSize()
const {
return FixedBlockSize() && FixedBlockCount(); }
327 bool LimitedSize()
const;
331 unsigned int MaxSize()
const;
335 unsigned int FieldCount()
const {
return m_fields.size(); }
338 bool FixedBlockSize()
const;
341 unsigned int BlockSize()
const;
343 unsigned int MaxBlockSize()
const;
347 uint16_t MinBlocks()
const {
return m_min_blocks; }
350 int16_t MaxBlocks()
const {
return m_max_blocks; }
352 bool FixedBlockCount()
const {
return m_min_blocks == m_max_blocks; }
356 if (index < m_fields.size())
357 return m_fields[index];
364 vector<const class FieldDescriptor *> m_fields;
367 uint16_t m_min_blocks;
368 int16_t m_max_blocks;
369 mutable bool m_populated;
370 mutable bool m_fixed_size, m_limited_size;
371 mutable unsigned int m_block_size, m_max_block_size;
373 void PopulateIfRequired()
const;
383 const vector<const FieldDescriptor*> &fields)
391 #endif // INCLUDE_OLA_MESSAGING_DESCRIPTOR_H_