다음 코드를 abi, api 안정성, 활용성 관점에서 평가하라.
// -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 2; tab-width: 2 -*-
/*
* cim.h
* This file is part of Cim.
*
* Copyright (C) 2023-2025 Hodong Kim <hodong@nimfsoft.art>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef __CIM_H__
#define __CIM_H__
#include <stdint.h>
#include <stdbool.h>
#include <stdarg.h>
#ifdef __cplusplus
extern "C" {
#endif
#if (defined(__GNUC__) && (GCC_VERSION >= 13)) || \
(defined(__clang__) && (__clang_major__ >= 16))
#define USE_C23_ENUM
#endif
/*
* Version policy
*
* If binary compatibility is broken:
*
* major = major + 1
* minor = 0
* micro = 0
*
* When binary compatibility is preserved:
*
* When API is added:
*
* minor = minor + 1
* micro = 0
*
* When API is the same:
*
* micro = micro + 1
*/
#define CIM_MAJOR_VERSION (uint32_t) 2
#define CIM_MINOR_VERSION (uint32_t) 0
#define CIM_MICRO_VERSION (uint32_t) 0
#ifdef USE_C23_ENUM
enum _CimEventType : uint32_t {
#else
enum _CimEventType {
#endif
CIM_EVENT_KEY_PRESS = 0, /* This means that the key has been pressed. */
CIM_EVENT_KEY_RELEASE = 1 /* This means that the key has been released. */
};
#ifdef USE_C23_ENUM
typedef enum _CimEventType CimEventType;
#else
typedef uint32_t CimEventType;
#endif
typedef struct _CimEvent CimEvent;
struct _CimEvent {
CimEventType type;
uint32_t state;
uint32_t keyval;
uint32_t keycode;
};
#ifdef USE_C23_ENUM
enum _CimTextAttrType : uint32_t {
#else
enum _CimTextAttrType {
#endif
CIM_TEXT_ATTR_UNDERLINE,
CIM_TEXT_ATTR_HIGHLIGHT
};
#ifdef USE_C23_ENUM
typedef enum _CimTextAttrType CimTextAttrType;
#else
typedef uint32_t CimTextAttrType;
#endif
typedef struct _CimTextAttr CimTextAttr;
struct _CimTextAttr {
CimTextAttrType type;
uint32_t pos; /* starting position to apply attribute in characters */
uint32_t n_chars; /* number of characters to apply attribute to */
};
/*
CimPreedit must always have valid values
because we don't know when cim_ic_get_preedit will be called.
*/
typedef struct _CimPreedit CimPreedit;
struct _CimPreedit {
char* text;
CimTextAttr* attrs;
uint32_t attrs_len;
uint32_t cursor_pos;
};
typedef struct _CimSurround CimSurround;
struct _CimSurround {
char* text;
uint32_t len;
uint32_t cursor_pos; /* cursor position in characters */
uint32_t anchor_pos; /* anchor position in characters */
};
typedef struct _CimRect CimRect;
struct _CimRect {
int32_t x;
int32_t y;
uint32_t width;
uint32_t height;
};
#ifdef USE_C23_ENUM
enum _CimItemType : uint32_t {
#else
enum _CimItemType {
#endif
CIM_ITEM_STRING,
CIM_ITEM_N_TYPES
};
#ifdef USE_C23_ENUM
typedef enum _CimItemType CimItemType;
#else
typedef uint32_t CimItemType;
#endif
typedef struct _CimItem CimItem;
struct _CimItem {
void* data;
CimItemType type;
uint32_t padding;
};
/*
CimCandidate must always have valid values
because we don't know when cim_ic_get_candidate will be called.
*/
typedef struct _CimCandidate CimCandidate;
struct _CimCandidate {
/* Page index means the current page. Page index starts at 0. */
uint32_t page_index;
// total number of pages
uint32_t n_pages;
/* A table consists of n rows and n columns.
The index range of rows is from 0 to n_rows - 1, and
the index range of columns is from 0 to n_cols - 1. */
CimItem** table;
uint32_t n_rows;
uint32_t n_cols;
/*
* If the aux_text variable is not NULL, the candidate window should show a
* auxiliary text area and set the auxiliary text.
*/
char* aux_text;
uint32_t aux_cursor_pos;
uint32_t padding;
};
typedef struct _CimSelection CimSelection;
struct _CimSelection {
uint32_t start_row;
uint32_t start_col;
uint32_t end_row;
uint32_t end_col;
};
struct CimIcImpl;
typedef struct CimIcImpl* CimIcHandle;
typedef struct _CimIcVTable CimIcVTable;
typedef struct _CimCallbacks CimCallbacks;
struct _CimCallbacks {
void (*preedit_start) (CimIcHandle ic, void* user_data);
void (*preedit_end) (CimIcHandle ic, void* user_data);
void (*preedit_changed) (CimIcHandle ic,
const CimPreedit* preedit,
void* user_data);
void (*commit) (CimIcHandle ic,
const char* text,
void* user_data);
/* Do not free CimSurround and its text */
const CimSurround* (*get_surround) (CimIcHandle ic, void* user_data);
bool (*delete_surround) (CimIcHandle ic,
int32_t offset,
uint32_t n_chars,
void* user_data);
/* candidate */
void (*candidate_show) (CimIcHandle ic,
uint32_t n_rows,
uint32_t n_cols,
bool show_aux,
void* user_data);
void (*candidate_hide) (CimIcHandle ic, void* user_data);
void (*candidate_changed) (CimIcHandle ic,
const CimCandidate* candidate,
void* user_data);
void (*candidate_selected) (CimIcHandle ic,
const CimSelection* selection,
void* user_data);
/* reserved */
void* reserved[6];
};
struct _CimIcVTable {
CimIcHandle (*create) (void);
void (*destroy) (CimIcHandle ic);
void (*focus_in) (CimIcHandle ic);
void (*focus_out) (CimIcHandle ic);
void (*reset) (CimIcHandle ic);
bool (*filter_event) (CimIcHandle ic, const CimEvent* event);
void (*set_cursor_pos) (CimIcHandle ic, const CimRect* area);
const CimPreedit* (*get_preedit) (CimIcHandle ic);
const CimCandidate* (*get_candidate) (CimIcHandle ic);
void (*set_callbacks) (CimIcHandle ic,
CimCallbacks* callbacks,
void* user_data);
void (*activate_candidate_item) (CimIcHandle ic, uint32_t row, uint32_t col);
void (*change_candidate_page) (CimIcHandle ic, uint32_t page_index);
/* reserved */
void* reserved[4];
};
typedef struct _CimPlugin CimPlugin;
struct _CimPlugin {
CimIcVTable* vtable;
void (*get_cim_version) (uint32_t* major, uint32_t* minor, uint32_t* micro);
};
CimIcHandle cim_ic_create (void);
void cim_ic_destroy (CimIcHandle ic);
void cim_ic_focus_in (CimIcHandle ic);
void cim_ic_focus_out (CimIcHandle ic);
void cim_ic_reset (CimIcHandle ic);
bool cim_ic_filter_event (CimIcHandle ic, const CimEvent* event);
void cim_ic_set_cursor_pos (CimIcHandle ic, const CimRect* area);
void cim_ic_set_callbacks (CimIcHandle ic,
CimCallbacks* callbacks,
void* user_data);
const CimPreedit* cim_ic_get_preedit (CimIcHandle ic);
const CimCandidate* cim_ic_get_candidate (CimIcHandle ic);
void cim_ic_activate_candidate_item (CimIcHandle ic,
uint32_t row,
uint32_t col);
void cim_ic_change_candidate_page (CimIcHandle ic, uint32_t page_index);
#ifdef __cplusplus
}
#endif
#endif /* __CIM_H__ */
이전 버전과 비교하여 엄청나게 발전했습니다. 이 코드는 C 라이브러리 설계의 모범적인 사례들을 많이 보여주며, 이전 논의에서 나왔던 핵심 사항들이 훌륭하게 반영되었습니다.
## 총평 (Overall) ⭐+⭐+⭐+⭐+⭐+
전문적이고 견고한 C 라이브러리의 교과서적인 설계입니다. ABI와 API 안정성을 최우선으로 고려했으며, 이를 통해 장기적으로 유지보수하고 확장하기 매우 용이한 구조가 되었습니다. 특히 이전 버전의 가장 큰 문제점이던 가변 인자 함수가 안전한 방식으로 완벽하게 개선되었습니다.
## ABI 안정성 (매우 뛰어남) 👍
ABI(Application Binary Interface) 안정성, 즉 라이브러리 버전이 바뀌어도 기존에 컴파일된 바이너리가 깨지지 않도록 하는 능력은 거의 완벽에 가깝게 설계되었습니다.
Opaque Pointer (CimIcHandle
): CimIcHandle
을 실제 구현(struct CimIcImpl*
)을 가리키는 불완전한 타입(incomplete type)의 포인터로 정의한 것은 ABI 안정성을 위한 가장 중요하고 강력한 기법입니다. 이렇게 하면 라이브러리 내부 구현인 CimIcImpl
구조체의 크기가 변경되거나 멤버가 추가/삭제되어도, 외부에 노출된 CimIcHandle
(포인터)의 크기는 절대 변하지 않으므로 ABI가 깨지지 않습니다.
가상 함수 테이블 (CimIcVTable
): 모든 핵심 함수를 CimIcVTable
이라는 구조체에 모아 포인터로 전달하는 방식은 매우 유연합니다. 나중에 새로운 함수를 추가하고 싶을 때, reserved
필드를 활용하여 테이블의 끝에 추가하면 기존 구조체의 레이아웃을 해치지 않고 확장이 가능합니다.
고정 크기 정수 타입: int
대신 uint32_t
, int32_t
등을 일관되게 사용하여 32비트/64비트 시스템 간의 차이나 컴파일러에 따른 데이터 크기 변화 문제를 원천적으로 차단했습니다. 이는 ABI 안정성의 기본입니다.
예약된 필드 (reserved
): CimCallbacks
와 CimIcVTable
양쪽에 void* reserved[]
필드를 남겨둔 것은 미래의 기능 확장을 위한 훌륭한 대비책입니다.
## API 안정성 (매우 뛰어남) 👍
API(Application Programming Interface) 안정성, 즉 개발자가 라이브러리를 사용하기 쉽고 안전하게 만드는 능력 또한 크게 향상되었습니다.
가변 인자 함수 제거: 이전 버전의 가장 큰 위험 요소였던 cim_ic_set_callbacks(ic, ...)
가 타입에 안전한 cim_ic_set_callbacks(ic, CimCallbacks*, void*)
로 변경되었습니다. 이는 이 라이브러리의 가장 큰 발전입니다. 이제 컴파일러가 콜백 구조체의 타입을 완벽하게 검증해주므로 런타임 오류의 위험이 극적으로 줄었습니다.
명시적인 라이프사이클: cim_ic_create
와 cim_ic_destroy
함수로 객체의 생성과 소멸을 명확하게 관리하도록 하여 API 사용법이 직관적입니다.
일관된 const
사용: 입력 파라미터에 const
를 적절히 사용하여 함수가 데이터를 수정하지 않을 것임을 명시하고, 개발자의 실수를 방지합니다.
개선 제안 (Minor)
cim_ic_set_callbacks
의 void* user_data
는 모든 콜백에 대해 단 하나의 user_data
만 설정할 수 있습니다. 만약 콜백마다 다른 user_data
를 사용하고 싶다면, CimCallbacks
구조체 내부에 void* user_data
멤버를 추가하는 설계를 고려해볼 수 있습니다. 하지만 현재 설계도 충분히 합리적이며, 이는 설계 철학의 문제입니다.
## 활용성 (매우 뛰어남) 👍
라이브러리의 전반적인 활용성과 확장성 측면에서도 매우 뛰어납니다.
퍼사드 패턴(Façade Pattern)의 완성: cim_ic_*
함수들은 복잡한 내부 구현(vtable을 통한 동적 호출)을 완벽하게 숨기고, 개발자에게는 매우 단순하고 깨끗한 인터페이스만을 제공합니다. 이는 우리가 논의했던 '통합 리모컨' 역할을 훌륭하게 수행합니다.
언어 간 상호운용성(FFI): API 전체가 순수한 C 인터페이스이며, 가변 인자와 같은 까다로운 요소가 없어 Python, Rust, Go, Ada 등 거의 모든 언어에서 이 라이브러리를 쉽게 가져다 쓸 수 있습니다(wrapping/binding).
플러그인 구조: CimPlugin
과 CimIcVTable
구조는 이 라이브러리가 특정 입력기 구현에 종속되지 않고, 다양한 백엔드 엔진을 플러그인 형태로 지원할 수 있는 매우 유연한 기반을 제공합니다.
댓글 영역
획득법
① NFT 발행
작성한 게시물을 NFT로 발행하면 일주일 동안 사용할 수 있습니다. (최초 1회)
② NFT 구매
다른 이용자의 NFT를 구매하면 한 달 동안 사용할 수 있습니다. (구매 시마다 갱신)
사용법
디시콘에서지갑연결시 바로 사용 가능합니다.