Struct에 대한 Accessor를 두기??

rain의 이미지


      1
      2 #include <stdio.h>
      3
      4 #ifdef USE_TRACE
      5 #define Trace(fmt, args...) \
      6     printf("[" __FILE__ ":%d:%s] " fmt, __LINE__, __func__, args)
      7 #else // !USE_TRACE
      8 #define Trace(fmt, args...)
      9 #endif // USE_TRACE
     10
     11 typedef struct {
     12     int data;
     13 } Context;
     14
     15 Context gvContext;
     16
     17 #ifdef USE_ACCESSOR
     18 Context*
     19 App_GetContext() {
     20     static Context context;
     21     return &context;
     22 }
     23
     24 #define Context_SetData(value) \
     25     Trace("Context_SetData(%d)\n", (App_GetContext()->data = (value)))
     26 #define Context_GetData() (App_GetContext()->data)
     27
     28 void
     29 example() {
     30     Context_SetData(1234);
     31     Trace("Context_GetData() = %d\n", Context_GetData());
     32 }
     33
     34 #else // !USE_ACCOSSOR
     35 void
     36 example() {
     37     gvContext.data = 1234;
     38 }
     39 #endif // USE_ACCESSOR
     40
     41 int
     42 main() {
     43     example();
     44     return 0;
     45 }

여러 사람이 같이 작업하는 embeded 환경의
프로젝트를 하다 보니
전역변수를 여기 저기 사용하게 되어 버렸습니다.
주로 printf로 찍히는 메세지로 간단한 디버깅을 하거나
T32로 디버깅을 하거나 합니다.
그래서 전역 변수를 위와 같이 accessor로 접근하게 하여
전역 변수를 수정하는 경우에 출력되게 하려고 하는데
괜찮은 방법인지 궁금합니다.

이게 다른 소스에서 보고 따라해 본건데 다른
장단점이 있는 지 궁금합니다.

chadr의 이미지

함수 호출의 오버헤드를 무시해도 되는 상황이시라면 저런식의 접근자는 좋은 설계 같습니다. 특히나 여러 사람이 나누어서 하는 프로젝트에서 공통적으로 사용하는 변수의 경우에는 어떤 사람이 어떻게 변경을 했는지 추적하는 시스템이 없으면 문제가 생겼을때 난감하지요...

-------------------------------------------------------------------------------
It's better to appear stupid and ask question than to be silent and remain stupid.

익명 사용자의 이미지

제가 짠 위의 코드를 다시 보니 문제가 있군요.
^^;

Quote:

gcc -DUSE_ACCESSOR Context.c

로 compile하면 USE_TRACE가 정의 되지 않아서
할당 자체가 않되겠네요.

다행히 저희 프로젝트에선 Trace의 출력 여부를
Runtime으로 셋팅할 수 있도록 해놓아서
저런 컴파일 옵션은 사용하지 않습니다.

Runtime시에 trace를 끈 경우라 하더라도
변수 값을 설정할 때, trace함수를 한번 호출 하는
overhead가 있을 거 같긴하네요.
사용하고자 하는 Context가 자주 바뀌진 않아서 생각한건데...

울 선배들은 어떻게 받아 들여 줄지..ㅡㅡa

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.