dimanche 30 avril 2017

c++ static_assert and compile-time

I want to write for ARM 32-bit microcontroller and I want to use modern C++ for education. My goals is: readable code, configurable in compile-time and help compiler to generate maximum optimizations for size and speed. What language tools should I use to archive my goals? I write some code and static assertions does not work... Please help me to fix this code (add assertions and no overhead).

#include <initializer_list>
#include <vector>

typedef uint port_type;
typedef uint pin_type;

class Pin {
private:
    const port_type _port;
    const pin_type _pin;
public:
    Pin(const port_type port, const pin_type pin) :
            _port(port), _pin(pin) {
    }
};

class Actuator {
private:
    const Pin _enable_pin;
    const Pin _dir_pin;
    const Pin _step_pin;
public:
    Actuator(const Pin enable_pin, const Pin dir_pin, const Pin step_pin) :
            _enable_pin(enable_pin), _dir_pin(dir_pin), _step_pin(step_pin) {
    }

};

class Machine {
private:
    const std::vector<Actuator> _actuators;
public:
    Machine(const std::initializer_list<Actuator> actuators) :
            _actuators(actuators) {
        /*check: all actuators _enable_pin ports are same*/
        /*check: all actuators _dir_pin ports are same*/
        /*check: all actuators _step_pin ports are same*/
        /*check: all port/pin pairs are unique*/
    }
};

int main() {
    /*example: good sequence*/
    Actuator act1(Pin(1, 1), Pin(1, 2), Pin(1, 3));
    Actuator act2(Pin(1, 4), Pin(1, 5), Pin(1, 6));
    Machine machine1( { act1, act2 });
    /*example: bad sequence*/
    Actuator act3(Pin(2, 1), Pin(2, 2), Pin(2, 2)); // NOK! Pin(2,2) already used!
    Actuator act4(Pin(2, 1), Pin(2, 3), Pin(2, 4)); // NOK! Pin(2,1) already used in act3!
    Machine machine2( { act3, act4 });
} 

Aucun commentaire:

Enregistrer un commentaire