gpt4 book ai didi

c++ - 我怎样才能使我的类更加独立于平台?

转载 作者:行者123 更新时间:2023-11-28 07:17:25 24 4
gpt4 key购买 nike

我有一个问题。我编写了一个 GPS 模块,可以动态检测消息的类型并在需要时进行配置。我已经通过几个类(class)的组合来完成它。为了使代码更加独立于平台 (stm32),我创建了一个具有基本 i/o 操作的 IStreamDevice 接口(interface)。有用。一切似乎都很棒,但类(class)显然是耦合的。这就是为什么我有几个问题:

  1. 如何避免将 IStreamDevice 传递给所有设备?
  2. 如何使整个设计更加独立于平台(和操作系统)?我们计划在不久的将来转移到另一个操作系统。它符合 POSIX 标准。我想我将能够在那里实现我的 IStreamDevice 接口(interface)(我最终可以使用的总线是 UART 和 SPI。在我当前的版本中,我只使用 UART)。我错了吗?

     class IStreamDevice
    {
    public:
    virtual ~IStreamDevice() {}
    virtual uint32_t read(uint8_t* data, uint32_t size) = 0;
    virtual uint32_t write(const uint8_t* data, uint32_t size) = 0;
    virtual uint32_t bytesToRead() const = 0;
    virtual uint32_t bytesToWrite() const = 0;
    };

    class GPSModule {
    public:

    GPSModule(periph::IStreamDevice *source);
    ~GPSModule();

    void turnDevice1Messages();
    void turnDevice2Messages();
    void configureDevice1(...);
    void configureDevice2(...);
    void Scan();

    private:
    Device1Configurator *_device1_configurator;
    Device2Configurator *_device2_configurator;
    StreamDeviceScanner*_scanner;
    periph::IStreamDevice *_source;
    };

    GPSModule::GPSModule(periph::IStreamDevice *source): _source(source)
    {
    _scanner= new StreamDeviceScanner(_source);
    _device1_configurator = new Device1Configurator(_source);
    _device2_configurator = new Device2Configurator(_source);
    }

    GPSModule::~GPSModule()
    {
    delete _scanner;
    }

    void GPSModule::Scan()
    {
    _scanner->Scan();
    }

    void GPSModule::turnDevice1Messages() {
    _device1_configurator->turnMessages();
    }

    class StreamDeviceScanner{
    public:
    StreamDeviceScanner(periph::IStreamDevice *source);
    ~StreamDeviceScanner();
    void Scan();

    private:
    typedef enum
    {
    WAITING_SYNC,
    WAITING_DEVICE1_MSG,
    WAITING_DEVICE2_MSG
    } states_t;

    periph::IStreamDevice *_source;
    ProtocolScanner *_protocol_scanner;
    states_t _state;
    private:

    states_t _catchSync();
    uint32_t _read(uint8_t* data, uint32_t length) { return _source->read(data,length); }
    uint32_t _bytesToRead() const { return _source->bytesToRead(); }
    };

    StreamDeviceScanner::StreamDeviceScanner(periph::IStreamDevice *source):
    _source(source),
    _state(WAITING_SYNC)
    {
    _protocol_scanner = new ProtocolScanner(source);
    }


    StreamDeviceScanner::~StreamDeviceScanner()
    {
    delete _protocol_scanner;
    }


    void StreamDeviceScanner::Scan()
    {
    while (_source->bytesToRead()) {

    switch (_state)
    {
    case WAITING_SYNC:
    _state = _catchSync();

    break;

    case WAITING_DEVICE1_MSG:
    _protocol_scanner->Device1Scan()
    _state = WAITING_SYNC;

    break;

    case WAITING_DEVICE2_MSG:
    _protocol_scanner->Device2Scan()
    _state = WAITING_SYNC;
    break;
    }

    }
    }

    class ProtocolScanner {
    private:
    Device1Scanner *_Device1Scanner;
    Device2Scanner *_Device2Scanner;
    public:
    ProtocolScanner(periph::IStreamDevice *source)
    {
    _Device1Scanner = new Device1Scanner(source);
    _Device2Scanner = new Device2Scanner(source);
    }

    ~ProtocolScanner()
    {
    delete _Device1Scanner;
    delete _Device1Scanner;
    }

    bool Device1Scan() const { return _Device1Scanner->Scan(); }
    bool Device2Scan() const { return _Device2Scanner->Scan(); }
    };

    class Device1Scanner {
    public:
    Device1Scanner(periph::IStreamDevice *source);
    ~Device1Scanner();
    bool Scan();

    private:
    enum { BUFFER_LENGTH = 8192 };

    typedef enum {
    Waiting_Header,
    Waiting_Payload,
    Waiting_Checksum
    } state_t;


    uint8_t _buffer[BUFFER_LENGTH];

    periph::IStreamDevice *_source;

    state_t _state;

    Device1Parser *_destination;
    Device1Scanner::NovatelMessage _message;

    private:
    uint32_t _read(uint8_t* data, uint32_t size) { return _source->read(data,size); }
    const uint32_t _bytesToRead() const { return _source->bytesToRead(); }

    bool _receiveHeader();
    bool _receivePayload();
    bool _receiveChecksum();
    bool _validChecksum() const;

    };

    Device2Scanner 看起来完全一样。我想听听任何人对设计的看法。

最佳答案

我没有发现您的设计存在任何固有问题。您的 IStreamWriter 接口(interface)似乎是底层总线的适当抽象,而不依赖于特定的总线细节。这符合依赖倒置原则和契约设计方法。我也没有在您的类(class)中看到紧密耦合。根据接口(interface)规范,您通过其处理程序访问总线,而不依赖于实际总线处理类的实现。

显示的代码中没有任何平台依赖性。如果总线处理因平台而异,除了根据平台为 IStreamWriter 提供不同的实现之外,您无能为力。

关于c++ - 我怎样才能使我的类更加独立于平台?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20015705/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com