MIP_SDK  latest-2-g34f3e39
MicroStrain Communications Library for embedded systems
composite_result.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "descriptor_id.hpp"
4 
6 #include <mip/mip_interface.hpp>
7 #include <mip/mip_result.hpp>
8 
9 #include <algorithm>
10 #include <stddef.h>
11 
12 namespace mip
13 {
18  {
19  public:
20  struct Entry
21  {
24 
25  operator bool() const { return result; }
26  bool operator!() const { return !result; }
27  operator CmdResult() const { return result; }
28 
29  bool operator==(mip::C::mip_cmd_result value) const { return result == value; }
30  bool operator!=(mip::C::mip_cmd_result value) const { return result != value; }
31 
35  //Entry(CmdResult r, CompositeDescriptor c) : result(r), descriptor(c) {}
36  };
37 
38  public:
39  CompositeResult() = default;
40  CompositeResult(bool success) : m_results{Entry{success, 0x0000}} {}
41  CompositeResult(CmdResult result) : m_results{result} {}
42  CompositeResult(CompositeDescriptor cmd, CmdResult result) : m_results{{result, cmd}} {}
43  CompositeResult(const Entry& result) : m_results{result} {}
44 
45  bool isEmpty() const { return m_results.empty(); }
46  bool notEmpty() const { return !m_results.empty(); }
47  size_t count() const { return m_results.size(); }
48 
49  bool allSuccessful() const { return std::all_of (m_results.begin(), m_results.end(), [](const Entry& r){ return r.result.isAck(); }); }
50  bool allFailed() const { return std::all_of (m_results.begin(), m_results.end(), [](const Entry& r){ return !r.result.isAck(); }); }
51  bool anySuccessful() const { return std::any_of (m_results.begin(), m_results.end(), [](const Entry& r){ return r.result.isAck(); }); }
52  bool anyFailed() const { return std::any_of (m_results.begin(), m_results.end(), [](const Entry& r){ return !r.result.isAck(); }); }
53  bool noneSuccessful() const { return std::none_of(m_results.begin(), m_results.end(), [](const Entry& r){ return r.result.isAck(); }); }
54  bool noneFailed() const { return std::none_of(m_results.begin(), m_results.end(), [](const Entry& r){ return !r.result.isAck(); }); }
55 
56  bool allMatch (CmdResult result) const { return std::all_of (m_results.begin(), m_results.end(), [result](const Entry& r){ return r.result == result; }); }
57  bool anyMatch (CmdResult result) const { return std::any_of (m_results.begin(), m_results.end(), [result](const Entry& r){ return r.result == result; }); }
58  bool noneMatch(CmdResult result) const { return std::none_of(m_results.begin(), m_results.end(), [result](const Entry& r){ return r.result == result; }); }
59 
60  operator bool() const { return noneFailed(); }
61  bool operator!() const { return anyFailed(); }
62 
64  {
65  if (isEmpty()) return CmdResult::STATUS_NONE;
66  if (count() == 1) return m_results.front().result;
67  if (allSuccessful()) return CmdResult::ACK_OK;
70  }
71 
72  void clear() { m_results.clear(); }
73 
74  // Append result to the list.
75  //void append(bool success, uint8_t id=0) { m_results.push_back({success ? CmdResult::ACK_OK : CmdResult::STATUS_ERROR, 0x0000}); }
76  void append(CmdResult result, CompositeDescriptor desc=0x0000) { m_results.push_back({result, desc}); }
77  void append(Entry result) { m_results.push_back(result); }
78  template<class MipType>
79  void append(CmdResult result, uint16_t index=0) { append({result, DescriptorId(MipType::DESCRIPTOR, index)}); }
80 
81  // Append multiple results.
82  void extend(const CompositeResult& other) { m_results.insert(m_results.end(), other.m_results.begin(), other.m_results.end()); }
83 
84  // Same as append but returns *this.
85  CompositeResult& operator+=(bool result) { append(result); return *this; }
86  CompositeResult& operator+=(CmdResult result) { append(result); return *this; }
87  CompositeResult& operator+=(Entry result) { append(result); return *this; }
88 
89  // Same as append but returns the result.
90  // Useful for code like `if( !results.appendAndCheckThis( doCommand() ) { return results; /* This specific command failed */ })`
91  bool appendAndCheckThisCmd(bool result, uint32_t id) { append({result, id}); return result; }
92  bool appendAndCheckThisCmd(CmdResult result, CompositeDescriptor desc) { append({result, desc}); return result; }
93  bool appendAndCheckThisCmd(Entry result) { append(result); return result; }
94 
95  // Filter results (these would be a lot easier to implement with C++20's ranges filtering stuff)
96  // Returns a new CompositeResult (or C++20 filter view) with only the filtered results.
97  // Todo: Implement these as necessary.
98  //auto filterByStatus(CmdResult result) const {}
99  //auto filterByStatusNot(CmdResult result) const {}
100  //auto filterSuccessful() {}
101  //auto filterFailed() {}
102  //auto filterById(CompositeDescriptor) {}
103 
104  const Entry& first() const { return m_results.front(); }
105  Entry& first() { return m_results.front(); }
106 
107  const Entry& last() const { return m_results.back(); }
108  Entry& last() { return m_results.back(); }
109 
110  CmdResult firstResult() const { return m_results.front().result; }
111  CmdResult lastResult() const { return m_results.back().result; }
112 
113  std::vector<Entry>::iterator begin() { return m_results.begin(); }
114  std::vector<Entry>::iterator end() { return m_results.end(); }
115 
116  std::vector<Entry>::const_iterator begin() const { return m_results.begin(); }
117  std::vector<Entry>::const_iterator end() const { return m_results.end(); }
118 
119  private:
120  std::vector<Entry> m_results;
121  };
122 
123  template<class Cmd, class... Args>
124  CompositeResult::Entry runCommandEx(Interface& device, const Cmd& cmd, Args&&... args)
125  {
126  CmdResult result = device.runCommand(cmd, std::forward<Args>(args)...);
127 
128  return { result, {Cmd::DESCRIPTOR_SET, Cmd::FIELD_DESCRIPTOR} };
129  }
130 } // namespace mip
mip::CompositeResult::allMatch
bool allMatch(CmdResult result) const
Definition: composite_result.hpp:56
mip::CompositeResult::first
Entry & first()
Definition: composite_result.hpp:105
mip::CompositeResult::extend
void extend(const CompositeResult &other)
Definition: composite_result.hpp:82
mip
A collection of C++ classes and functions covering the full mip api.
Definition: commands_3dm.c:11
mip::CompositeResult::Entry::result
CmdResult result
Result of action.
Definition: composite_result.hpp:22
mip::CompositeResult::first
const Entry & first() const
Definition: composite_result.hpp:104
mip_descriptors.hpp
mip::CompositeResult::count
size_t count() const
Definition: composite_result.hpp:47
mip::CompositeResult::operator!
bool operator!() const
Definition: composite_result.hpp:61
mip::CompositeResult::operator+=
CompositeResult & operator+=(bool result)
Definition: composite_result.hpp:85
mip::CompositeResult::begin
std::vector< Entry >::const_iterator begin() const
Definition: composite_result.hpp:116
mip::CompositeResult::last
const Entry & last() const
Definition: composite_result.hpp:107
mip::C::mip_cmd_result
mip_cmd_result
Represents the status of a MIP command.
Definition: mip_result.h:27
mip::CompositeResult::Entry::operator!
bool operator!() const
Definition: composite_result.hpp:26
mip::CompositeResult
Represents a list of zero or more actions and their results.
Definition: composite_result.hpp:17
mip::CompositeResult::last
Entry & last()
Definition: composite_result.hpp:108
mip::CompositeResult::append
void append(CmdResult result, CompositeDescriptor desc=0x0000)
Definition: composite_result.hpp:76
mip::Interface::runCommand
CmdResult runCommand(const Cmd &cmd, Timeout additionalTime=0)
Definition: mip_interface.hpp:202
mip::CompositeResult::CompositeResult
CompositeResult(bool success)
Definition: composite_result.hpp:40
mip::CompositeResult::CompositeResult
CompositeResult(CmdResult result)
Definition: composite_result.hpp:41
mip::CompositeResult::CompositeResult
CompositeResult()=default
mip::CompositeResult::CompositeResult
CompositeResult(CompositeDescriptor cmd, CmdResult result)
Definition: composite_result.hpp:42
mip::CompositeResult::firstResult
CmdResult firstResult() const
Definition: composite_result.hpp:110
mip::CompositeResult::allSuccessful
bool allSuccessful() const
Definition: composite_result.hpp:49
mip::CmdResult::STATUS_ERROR
static constexpr C::mip_cmd_result STATUS_ERROR
Command could not be executed (error sending/receiving)
Definition: mip_result.hpp:27
mip::CompositeResult::lastResult
CmdResult lastResult() const
Definition: composite_result.hpp:111
mip::CompositeResult::operator+=
CompositeResult & operator+=(Entry result)
Definition: composite_result.hpp:87
mip::CompositeResult::anyFailed
bool anyFailed() const
Definition: composite_result.hpp:52
mip::CompositeResult::end
std::vector< Entry >::const_iterator end() const
Definition: composite_result.hpp:117
mip::runCommandEx
CompositeResult::Entry runCommandEx(Interface &device, const Cmd &cmd, Args &&... args)
Definition: composite_result.hpp:124
mip::CompositeResult::operator+=
CompositeResult & operator+=(CmdResult result)
Definition: composite_result.hpp:86
mip::CmdResult::STATUS_TIMEDOUT
static constexpr C::mip_cmd_result STATUS_TIMEDOUT
Reply was not received before timeout expired.
Definition: mip_result.hpp:29
mip::commands_3dm::DESCRIPTOR_SET
@ DESCRIPTOR_SET
Definition: commands_3dm.hpp:31
mip::CompositeResult::isEmpty
bool isEmpty() const
Definition: composite_result.hpp:45
mip::CompositeResult::anyMatch
bool anyMatch(CmdResult result) const
Definition: composite_result.hpp:57
mip::CompositeResult::CompositeResult
CompositeResult(const Entry &result)
Definition: composite_result.hpp:43
mip::CompositeResult::appendAndCheckThisCmd
bool appendAndCheckThisCmd(bool result, uint32_t id)
Definition: composite_result.hpp:91
mip::CmdResult
Represents the status of a MIP command.
Definition: mip_result.hpp:24
mip::CompositeResult::begin
std::vector< Entry >::iterator begin()
Definition: composite_result.hpp:113
mip::DescriptorId
A combination of a MIP descriptor pair and u16 ID value.
Definition: descriptor_id.hpp:14
mip::CompositeResult::append
void append(CmdResult result, uint16_t index=0)
Definition: composite_result.hpp:79
descriptor_id.hpp
mip::CompositeResult::noneMatch
bool noneMatch(CmdResult result) const
Definition: composite_result.hpp:58
mip::CompositeResult::Entry
Definition: composite_result.hpp:20
mip::CompositeResult::summary
CmdResult summary() const
Definition: composite_result.hpp:63
mip::CompositeResult::Entry::Entry
Entry(CmdResult r, DescriptorId d={})
Definition: composite_result.hpp:33
mip_interface.hpp
mip::CompositeResult::notEmpty
bool notEmpty() const
Definition: composite_result.hpp:46
mip::CompositeResult::Entry::descriptor
DescriptorId descriptor
Command or action that was executed.
Definition: composite_result.hpp:23
mip::CompositeResult::noneSuccessful
bool noneSuccessful() const
Definition: composite_result.hpp:53
mip::CompositeResult::append
void append(Entry result)
Definition: composite_result.hpp:77
mip::CmdResult::ACK_OK
static constexpr C::mip_cmd_result ACK_OK
Command completed successfully.
Definition: mip_result.hpp:34
mip::CompositeResult::Entry::Entry
Entry(C::mip_cmd_result r, DescriptorId d={})
Definition: composite_result.hpp:34
mip::CompositeResult::Entry::Entry
Entry(bool r, DescriptorId d={})
Definition: composite_result.hpp:32
mip::CompositeResult::allFailed
bool allFailed() const
Definition: composite_result.hpp:50
mip::CompositeResult::noneFailed
bool noneFailed() const
Definition: composite_result.hpp:54
mip::CompositeResult::clear
void clear()
Definition: composite_result.hpp:72
mip::CmdResult::STATUS_NONE
static constexpr C::mip_cmd_result STATUS_NONE
Command has been initialized but not queued yet.
Definition: mip_result.hpp:32
mip::CompositeResult::end
std::vector< Entry >::iterator end()
Definition: composite_result.hpp:114
mip::CompositeResult::appendAndCheckThisCmd
bool appendAndCheckThisCmd(Entry result)
Definition: composite_result.hpp:93
mip::CompositeDescriptor
Convenience struct holding both descriptor set and field descriptor.
Definition: mip_descriptors.hpp:21
mip::Interface
Represents a connected MIP device.
Definition: mip_interface.hpp:50
mip::CompositeResult::appendAndCheckThisCmd
bool appendAndCheckThisCmd(CmdResult result, CompositeDescriptor desc)
Definition: composite_result.hpp:92
mip::CompositeResult::Entry::operator==
bool operator==(mip::C::mip_cmd_result value) const
Definition: composite_result.hpp:29
mip_result.hpp
mip::CompositeResult::anySuccessful
bool anySuccessful() const
Definition: composite_result.hpp:51
mip::CompositeResult::Entry::operator!=
bool operator!=(mip::C::mip_cmd_result value) const
Definition: composite_result.hpp:30