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