Unnamed Structure and Union Fields - Anonymous Struct

검색해보니 대략 구조가 확정적이지 않을 때 사용할 수 있으나 좋은 방법은 아니라는 것 같다
union의 field 중 비구조체와 구조체를 같이 깔끔하게 넣을 수 있다
혹은 자료형을 확정적이지 않게 사용할 수 있다

c로 function overloading 도 가능하다 ㅎㅎ

struct sensors_poll_device_t {
    struct hw_device_t common;
    int (*activate)(struct sensors_poll_device_t *dev,
            int sensor_handle, int enabled);
    int (*setDelay)(struct sensors_poll_device_t *dev,
            int sensor_handle, int64_t sampling_period_ns);
    int (*poll)(struct sensors_poll_device_t *dev,
            sensors_event_t* data, int count);
};

typedef struct sensors_poll_device_1 {
    union {
        /* sensors_poll_device_1 is compatible with sensors_poll_device_t,
         * and can be down-cast to it
         */
        struct sensors_poll_device_t v0;

        struct {
            struct hw_device_t common;

            int (*activate)(struct sensors_poll_device_t *dev,
                    int sensor_handle, int enabled);
            int (*setDelay)(struct sensors_poll_device_t *dev,
                    int sensor_handle, int64_t sampling_period_ns);
            int (*poll)(struct sensors_poll_device_t *dev,
                    sensors_event_t* data, int count);
        };
    };

    int (*batch)(struct sensors_poll_device_1* dev,
            int sensor_handle, int flags, int64_t sampling_period_ns,
            int64_t max_report_latency_ns);
    int (*flush)(struct sensors_poll_device_1* dev, int sensor_handle);
    int (*inject_sensor_data)(struct sensors_poll_device_1 *dev, const sensors_event_t *data);
    void (*reserved_procs[7])(void);
} sensors_poll_device_1_t;

struct sensors_poll_context_t {
    struct sensors_poll_device_t device; // must be first

        sensors_poll_context_t();
        ~sensors_poll_context_t();
    int activate(int handle, int enabled);
    ...

private:
    ...
};

정의는 이렇게 되어있고 실제 사용은 아래와 같다

static inline int sensors_open_1(const struct hw_module_t* module,
        sensors_poll_device_1_t** device) {
    return module->methods->open(module,
            SENSORS_HARDWARE_POLL, (struct hw_device_t**)device);
}

static int open_sensors(const struct hw_module_t* module, const char* id, 
                        struct hw_device_t** device)
{
        sensors_poll_context_t *dev = new sensors_poll_context_t();
        memset(&dev->device, 0, sizeof(sensors_poll_device_t));


        *device = &dev->device.common;    //이 부분
        ...
}

sensors_poll_device_1_t 에서 struct hw_device_t 로 자유로이 형변환 하여 호출한다
overloading 하는 것 같은 느낌이 난다
왜냐하면 sensors_open_1 입장에서는 device 변수는 hw_device_t 로 활용하여 멤버 변수에 접근할 수 도 있고
아니면 그냥 sensors_poll_device_1 로 그대로 사용하여 batch나 flush 같은 애들을 호출할 수도 있다

https://gcc.gnu.org/onlinedocs/gcc/Unnamed-Fields.html

'devel > code' 카테고리의 다른 글

arm hardfault handler  (0) 2017.10.26
argument parsing  (0) 2017.02.17
디버그 on/off에 따라 자료형을 다르게 하는 방법  (0) 2016.12.02
bus_for_each_dev - while 조건문  (0) 2016.10.10
구조체 선언 없이 값 리턴  (0) 2016.04.29
Posted by 쵸코케키

블로그 이미지
chocokeki
쵸코케키

공지사항

Yesterday
Today
Total

달력

 « |  » 2024.4
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30

최근에 올라온 글

최근에 달린 댓글

글 보관함