vesc_packet_factory.h
Go to the documentation of this file.
1 // Copyright 2020 F1TENTH Foundation
2 //
3 // Redistribution and use in source and binary forms, with or without modification, are permitted
4 // provided that the following conditions are met:
5 //
6 // 1. Redistributions of source code must retain the above copyright notice, this list of conditions
7 // and the following disclaimer.
8 //
9 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
10 // of conditions and the following disclaimer in the documentation and/or other materials
11 // provided with the distribution.
12 //
13 // 3. Neither the name of the copyright holder nor the names of its contributors may be used
14 // to endorse or promote products derived from this software without specific prior
15 // written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
18 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
19 // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
20 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
24 // WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 // -*- mode:c++; fill-column: 100; -*-
27 
28 #ifndef VESC_DRIVER_VESC_PACKET_FACTORY_H_
29 #define VESC_DRIVER_VESC_PACKET_FACTORY_H_
30 
31 #include <cstdint>
32 #include <functional>
33 #include <map>
34 #include <memory>
35 #include <string>
36 #include <vector>
37 
39 
40 namespace vesc_driver
41 {
42 
47 {
48 public:
50  static VescPacketFactory* getFactory();
51 
72  static VescPacketPtr createPacket(const Buffer::const_iterator& begin,
73  const Buffer::const_iterator& end,
74  int* num_bytes_needed, std::string* what);
75 
76  typedef std::function<VescPacketPtr(std::shared_ptr<VescFrame>)> CreateFn;
77 
79  static void registerPacketType(int payload_id, CreateFn fn);
80 
84  VescPacketFactory(const VescPacketFactory &) = delete;
85  VescPacketFactory & operator=(const VescPacketFactory &) = delete;
86 
87 private:
89  typedef std::map<int, CreateFn > FactoryMap;
90  static FactoryMap* getMap();
91 };
92 
93 template<typename PACKETTYPE>
95 {
96 public:
97  explicit PacketFactoryTemplate(int payload_id)
98  {
100  }
101 
102  static VescPacketPtr create(std::shared_ptr<VescFrame> frame)
103  {
104  return VescPacketPtr(new PACKETTYPE(frame));
105  }
106 };
107 
109 #define REGISTER_PACKET_TYPE(id, klass) \
110 static PacketFactoryTemplate<klass> global_##klass##Factory((id));
111 
112 } // namespace vesc_driver
113 
114 #endif // VESC_DRIVER_VESC_PACKET_FACTORY_H_
static VescPacketPtr create(std::shared_ptr< VescFrame > frame)
VescPacketFactory & operator=(const VescPacketFactory &)=delete
static VescPacketPtr createPacket(const Buffer::const_iterator &begin, const Buffer::const_iterator &end, int *num_bytes_needed, std::string *what)
std::map< int, CreateFn > FactoryMap
std::shared_ptr< VescPacket > VescPacketPtr
Definition: vesc_packet.h:106
static void registerPacketType(int payload_id, CreateFn fn)
std::function< VescPacketPtr(std::shared_ptr< VescFrame >)> CreateFn
static VescPacketFactory * getFactory()


vesc_driver
Author(s): Michael T. Boulet , Joshua Whitley
autogenerated on Sun Apr 18 2021 02:48:01