Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 5, 2025

Direct construction of shared_ptr from raw pointers (std::shared_ptr<T>(new T(...))) triggers clang warnings when the class has virtual functions but non-virtual destructors. This pattern appeared in all peripheral class GetInstance() methods.

Changes

Peripheral classes (Spi, AuxSpi, Gpio, Pwm, Dma):

  • Inherit from std::enable_shared_from_this<T>
  • Add factory struct with Create() static method
  • Declare factory as friend to access private constructors

Factory pattern implementation:

// Before
instances_[index] = std::shared_ptr<Spi>(new Spi(REG_SPI0));

// After
struct SpiFactory {
  static std::shared_ptr<Spi> Create(SpiRegisterMap* register_map) {
    struct EnableMakeShared : public Spi {
      explicit EnableMakeShared(SpiRegisterMap* reg_map) : Spi(reg_map) {}
    };
    return std::make_shared<EnableMakeShared>(register_map);
  }
};
instances_[index] = SpiFactory::Create(REG_SPI0);

The local EnableMakeShared struct grants make_shared access to private constructors while maintaining encapsulation. External API remains unchanged.

Original prompt

This section details on the original issue you should resolve

<issue_title>peripheralのインスタンスをshared_from_this()でshared_ptr化する</issue_title>
<issue_description>- std::shared_ptr<Spi>(new Spi(REG_SPI0));のような書き方ではclangでビルド時にwarningが出る

  • 全ペリフェラルクラスのGetInstanceで、newでインスタンス生成し直接shared_ptrに変換するのではなく、shared_from_this()で変換するように修正</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI self-assigned this Nov 5, 2025
Copilot AI and others added 3 commits November 5, 2025 00:29
Co-authored-by: yutatech <61183418+yutatech@users.noreply.github.com>
- Added std::enable_shared_from_this inheritance to all peripheral classes
- Created Factory structs to enable std::make_shared with private constructors
- Replaced std::shared_ptr<Class>(new Class(...)) with Factory pattern
- This resolves clang warnings about deleting objects with virtual functions
- Modified classes: Spi, AuxSpi, Gpio, Pwm, Dma
- Updated .gitignore to exclude build artifacts

Co-authored-by: yutatech <61183418+yutatech@users.noreply.github.com>
Co-authored-by: yutatech <61183418+yutatech@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix peripheral instances using shared_from_this for shared_ptr Replace shared_ptr construction with factory pattern to eliminate clang warnings Nov 5, 2025
Copilot AI requested a review from yutatech November 5, 2025 00:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

peripheralのインスタンスをshared_from_this()でshared_ptr化する

2 participants