Skip to content

Commit 09e54fd

Browse files
committed
refactor: get rid of unordered_map in multiton classes
1 parent 9a6d67f commit 09e54fd

10 files changed

Lines changed: 109 additions & 53 deletions

File tree

firmware/src/chipset/Servo/Servo.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
#include "Servo.h"
22

3+
#include <array>
4+
#include <memory>
5+
36
#include "system_clock.h"
47

58
Servo& Servo::get_instance(ServoType type) {
6-
static std::unordered_map<ServoType, Servo *> instances;
7-
auto it = instances.find(type);
8-
if (it == instances.end()) {
9-
instances[type] = new Servo(type);
9+
static std::array<std::pair<ServoType, std::unique_ptr<Servo>>, 2> instances{
10+
std::make_pair(ServoType::SERVO_LEFT, std::make_unique<Servo>(Servo(ServoType::SERVO_LEFT))),
11+
std::make_pair(ServoType::SERVO_RIGHT, std::make_unique<Servo>(Servo(ServoType::SERVO_RIGHT)))
12+
};
13+
14+
for (auto& p : instances) {
15+
if (p.first == type) {
16+
return *p.second;
17+
}
1018
}
11-
return *instances[type];
1219
}
1320

1421
uint32_t Servo::init() {

firmware/src/chipset/Servo/include/Servo.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,17 @@ class Servo {
4040
}
4141
}
4242

43-
Servo(ServoType type) : pwm(get_pwm_instance(type)), adc(get_adc_instance(type)) {
44-
type_ = type;
45-
};
43+
explicit Servo(ServoType type) : pwm(get_pwm_instance(type)), adc(get_adc_instance(type)), type_(type) {};
4644

47-
~Servo() {};
4845
Servo(const Servo &obj) = delete;
4946
Servo &operator=(const Servo &obj) = delete;
5047

5148
public:
49+
Servo(Servo &&obj) noexcept = default;
50+
Servo &operator=(Servo &&obj) noexcept = default;
51+
52+
~Servo() = default;
53+
5254
static Servo &get_instance(ServoType type);
5355

5456
uint32_t init();

firmware/src/chipset/ads1115/ads1115.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "ads1115.h"
22

3-
#include <unordered_map>
3+
#include <array>
4+
#include <memory>
45

56
#include "ads1115_registers.h"
67

@@ -18,12 +19,16 @@ ADS1115::ADS1115(ADCServoType id) {
1819
}
1920

2021
ADS1115& ADS1115::get_instance(ADCServoType type) {
21-
static std::unordered_map<ADCServoType, ADS1115 *> instances;
22-
auto it = instances.find(type);
23-
if (it == instances.end()) {
24-
instances[type] = new ADS1115(type);
22+
static std::array<std::pair<ADCServoType, std::unique_ptr<ADS1115>>, 2> instances{
23+
std::make_pair(ADCServoType::ADC_SERVO_1, std::make_unique<ADS1115>(ADS1115(ADCServoType::ADC_SERVO_1))),
24+
std::make_pair(ADCServoType::ADC_SERVO_2, std::make_unique<ADS1115>(ADS1115(ADCServoType::ADC_SERVO_2)))
25+
};
26+
27+
for (auto& p : instances) {
28+
if (p.first == type) {
29+
return *p.second;
30+
}
2531
}
26-
return *instances[type];
2732
}
2833

2934
uint32_t ADS1115::init(void) {

firmware/src/chipset/ads1115/include/ads1115.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ enum class ADCServoType : uint8_t {
1212

1313
class ADS1115 {
1414
private:
15-
ADS1115(ADCServoType id);
16-
~ADS1115() {};
17-
ADS1115(const ADS1115 &obj);
18-
ADS1115 &operator=(const ADS1115 &obj);
1915

2016
I2C &i2c_handle = I2C::getInstance();
2117
uint8_t i2c_address = 0x00;
@@ -24,7 +20,16 @@ class ADS1115 {
2420

2521
uint32_t config(void);
2622

23+
explicit ADS1115(ADCServoType id);
24+
25+
ADS1115(const ADS1115 &obj) = delete;
26+
ADS1115 &operator=(const ADS1115 &obj) = delete;
2727
public:
28+
ADS1115(ADS1115 &&obj) noexcept = default;
29+
ADS1115 &operator=(ADS1115 &&obj) noexcept = default;
30+
31+
~ADS1115() = default;
32+
2833
uint32_t init();
2934
static ADS1115 &get_instance(ADCServoType type);
3035
uint32_t read_value(uint16_t& value);

firmware/src/chipset/wheel_encoder/include/wheel_encoder.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#define TRUCK_HW_CHIPSET_WHEEL_ENCODER_INCLUDE_WHEEL_ECNODER_H_
44

55
#include <cstdint>
6-
#include <unordered_map>
76

87
#include "encoder_timer.h"
98

@@ -21,17 +20,15 @@ class WheelEncoder {
2120
static constexpr float low_pass_coef = 0.1f;
2221

2322
bool is_initialized = false;
24-
enum WheelType type;
23+
enum WheelType type_;
2524
EncoderTimer& encoder_timer_handle;
2625
static constexpr uint32_t max_data_size = 50;
2726
std::vector<int> encoder_ticks;
2827

2928
float current_speed = 0.0f;
3029
uint32_t last_tick_ts = 0;
3130

32-
~WheelEncoder() {};
33-
WheelEncoder(const WheelEncoder &obj) = delete;
34-
WheelEncoder &operator=(const WheelEncoder &obj) = delete;
31+
void low_pas_filter_apply(float raw_speed);
3532

3633
static EncoderTimer &get_timer_instance(WheelType wheel_type_) {
3734
switch (wheel_type_) {
@@ -46,11 +43,17 @@ class WheelEncoder {
4643
}
4744
}
4845

49-
WheelEncoder(WheelType id) : encoder_timer_handle(get_timer_instance(id)), type(id) {};
46+
explicit WheelEncoder(WheelType type) : encoder_timer_handle(get_timer_instance(type)), type_(type) {};
5047

51-
void low_pas_filter_apply(float raw_speed);
48+
WheelEncoder(const WheelEncoder &obj) = delete;
49+
WheelEncoder &operator=(const WheelEncoder &obj) = delete;
5250

5351
public:
52+
WheelEncoder(WheelEncoder &&obj) noexcept = default;
53+
WheelEncoder &operator=(WheelEncoder &&obj) noexcept = default;
54+
55+
~WheelEncoder() = default;
56+
5457
static WheelEncoder &get_instance(WheelType id);
5558

5659
uint32_t init(void);

firmware/src/chipset/wheel_encoder/wheel_encoder.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
#include "wheel_encoder.h"
22

3+
#include <array>
4+
#include <memory>
5+
36
#include "system_clock.h"
47

5-
WheelEncoder& WheelEncoder::get_instance(WheelType id) {
6-
static std::unordered_map<WheelType, WheelEncoder *> instances;
7-
auto it = instances.find(id);
8-
if (it == instances.end()) {
9-
instances[id] = new WheelEncoder(id);
8+
WheelEncoder& WheelEncoder::get_instance(WheelType type) {
9+
static std::array<std::pair<WheelType, std::unique_ptr<WheelEncoder>>, 4> instances{
10+
std::make_pair(WheelType::LEFT_FRONT, std::make_unique<WheelEncoder>(WheelEncoder(WheelType::LEFT_FRONT))),
11+
std::make_pair(WheelType::RIGHT_FRONT, std::make_unique<WheelEncoder>(WheelEncoder(WheelType::RIGHT_FRONT))),
12+
std::make_pair(WheelType::LEFT_REAR, std::make_unique<WheelEncoder>(WheelEncoder(WheelType::LEFT_REAR))),
13+
std::make_pair(WheelType::RIGHT_REAR, std::make_unique<WheelEncoder>(WheelEncoder(WheelType::RIGHT_REAR))),
14+
};
15+
16+
for (auto& p : instances) {
17+
if (p.first == type) {
18+
return *p.second;
19+
}
1020
}
11-
return *instances[id];
1221
}
1322

1423
uint32_t WheelEncoder::init(void) {

firmware/src/mcu/periphery/encoder_timer/encoder_timer.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
11
#include "encoder_timer.h"
2+
3+
#include <cstdio>
4+
#include <vector>
5+
#include <array>
6+
#include <memory>
7+
28
#include "stm32g4xx_ll_bus.h"
39
#include "stm32g4xx_ll_tim.h"
410
#include "stm32g4xx_ll_gpio.h"
5-
#include <vector>
6-
#include <cstdio>
711

812
bool EncoderTimer::is_common_tim_initialized = false;
913

1014
static volatile bool dma_complete_irq[4] = {false};
1115

12-
EncoderTimer& EncoderTimer::get_instance(EncoderType id) {
13-
static std::unordered_map<EncoderType, EncoderTimer *> instances;
14-
auto it = instances.find(id);
15-
if (it == instances.end()) {
16-
instances[id] = new EncoderTimer(id);
16+
EncoderTimer& EncoderTimer::get_instance(EncoderType type) {
17+
static std::array<std::pair<EncoderType, std::unique_ptr<EncoderTimer>>, 4> instances{
18+
std::make_pair(EncoderType::ID_0, std::make_unique<EncoderTimer>(EncoderTimer(EncoderType::ID_0))),
19+
std::make_pair(EncoderType::ID_1, std::make_unique<EncoderTimer>(EncoderTimer(EncoderType::ID_1))),
20+
std::make_pair(EncoderType::ID_2, std::make_unique<EncoderTimer>(EncoderTimer(EncoderType::ID_2))),
21+
std::make_pair(EncoderType::ID_3, std::make_unique<EncoderTimer>(EncoderTimer(EncoderType::ID_3))),
22+
};
23+
24+
for (auto& p : instances) {
25+
if (p.first == type) {
26+
return *p.second;
27+
}
1728
}
18-
return *instances[id];
1929
}
2030

2131
uint32_t EncoderTimer::init() {

firmware/src/mcu/periphery/encoder_timer/include/encoder_timer.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#define TRUCK_HW_PERIPHERY_ENCODER_TIMER_INCLUDE_ENCODER_TIMER_H_
33

44
#include <cstdint>
5-
#include <unordered_map>
65
#include <array>
76
#include <vector>
87
#include <limits>
@@ -34,14 +33,19 @@ class EncoderTimer {
3433
uint32_t timer_channel;
3534
uint32_t dma_channel;
3635

37-
EncoderTimer(EncoderType id);
38-
~EncoderTimer() {};
36+
uint32_t get_data_size();
37+
38+
explicit EncoderTimer(EncoderType id);
39+
3940
EncoderTimer(const EncoderTimer &obj) = delete;
4041
EncoderTimer &operator=(const EncoderTimer &obj) = delete;
4142

42-
uint32_t get_data_size();
43-
4443
public:
44+
EncoderTimer(EncoderTimer &&obj) noexcept = default;
45+
EncoderTimer &operator=(EncoderTimer &&obj) noexcept = default;
46+
47+
~EncoderTimer() = default;
48+
4549
static EncoderTimer &get_instance(EncoderType id);
4650

4751
uint32_t init();

firmware/src/mcu/periphery/pwm_servo/include/pwm_servo.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#ifndef TRUCK_HW_PERIPHERY_PWM_SERVO_INCLUDE_PWM_SERVO_H_
22
#define TRUCK_HW_PERIPHERY_PWM_SERVO_INCLUDE_PWM_SERVO_H_
33

4-
#include <unordered_map>
54
#include "stm32g4xx_ll_tim.h"
65

76
enum class PWMServoType : uint8_t {
@@ -19,12 +18,17 @@ class PWMServo {
1918
static constexpr uint32_t TIM_AUTORELOAD = 20000;
2019
uint32_t timer_channel;
2120

22-
PWMServo(PWMServoType init_type);
23-
~PWMServo() {};
21+
explicit PWMServo(PWMServoType init_type);
22+
2423
PWMServo(const PWMServo &obj) = delete;
2524
PWMServo &operator=(const PWMServo &obj) = delete;
2625

2726
public:
27+
PWMServo(PWMServo &&obj) noexcept = default;
28+
PWMServo &operator=(PWMServo &&obj) noexcept = default;
29+
30+
~PWMServo() = default;
31+
2832
static PWMServo &get_instance(PWMServoType type);
2933

3034
uint32_t init();

firmware/src/mcu/periphery/pwm_servo/pwm_servo.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#include "pwm_servo.h"
22

3+
#include <array>
4+
#include <memory>
5+
36
#include "stm32g4xx_ll_gpio.h"
47
#include "stm32g4xx_ll_bus.h"
58

@@ -20,12 +23,16 @@ PWMServo::PWMServo(PWMServoType id) {
2023
}
2124

2225
PWMServo& PWMServo::get_instance(PWMServoType type) {
23-
static std::unordered_map<PWMServoType, PWMServo *> instances;
24-
auto it = instances.find(type);
25-
if (it == instances.end()) {
26-
instances[type] = new PWMServo(type);
26+
static std::array<std::pair<PWMServoType, std::unique_ptr<PWMServo>>, 2> instances{
27+
std::make_pair(PWMServoType::PWM_SERVO_1, std::make_unique<PWMServo>(PWMServo(PWMServoType::PWM_SERVO_1))),
28+
std::make_pair(PWMServoType::PWM_SERVO_2, std::make_unique<PWMServo>(PWMServo(PWMServoType::PWM_SERVO_2)))
29+
};
30+
31+
for (auto& p : instances) {
32+
if (p.first == type) {
33+
return *p.second;
34+
}
2735
}
28-
return *instances[type];
2936
}
3037

3138
uint32_t PWMServo::init() {

0 commit comments

Comments
 (0)