Programmer Application Note

2015년 7월 27일 월요일

함수포인터

오후 5:04 Posted by PAN.SPOT , No comments
  • 함수 정의
void cStyleFunction(int param){...}

class Aclass {
 void classFunction(int param){...}
 static void classStaticFunction(int param){...}
};

namespace NameSpace{
 void namespaceFunction(int param){...}
}
  • 함수포인터 선언
void (*FunctionPointer) (int param);
typedef void (*TypedefFunctionPointer) (int param);
void (Aclass::*ClassFunctionPointer) (int param);
  • 함수포인터 사용
void (*FunctionPointer) (int param);
void main(){
 void (*FunctionPointer) (int param);
 FunctionPointer = cStyleFunction; //ok
 FunctionPointer = Aclass::classStaticFunction; //ok
 FunctionPointer = NameSpace::namespaceFunction; //ok 
 FunctionPointer = Aclass::classFunction; //error
 FunctionPointer(param);
}
typedef void (*TypedefFunctionPointer) (int param);
typedef void (*TypedefFunctionPointer) (int param);
void main(){
 TypedefFunctionPointer tfp;
 tfp = cStyleFunction; //ok
 tfp = Aclass::classStaticFunction; //ok
 tfp = NameSpace::namespaceFunction; //ok
 tfp = Aclass::classFunction; //error
 tfp(param);
}
typedef void (Aclass::*ClassFunctionPointer) (int param);
void main(){
 Aclass *a = new Aclass();
 void (Aclass::*ClassFunctionPointer) (int param);
 ClassFunctionPointer = &Aclass::classFunction; //ok
 (a->*ClassFunctionPointer)(param);
}

2015년 7월 23일 목요일

디폴트 생성자 , 소멸자, 복사 생성자 , 대입연산자

오후 5:22 Posted by PAN.SPOT , No comments

컴파일러가 자동으로 생성하는 것들

Empty() {...}; //기본 생성자
~Empty() {...};//기본 소멸자
Empty(const Empty& rhs) {...}; //복사 생성자
Empty& operator=(const Empty& rhs) {...}; //복사 대입 연산자
Empty e1; //기본 생성자,기본 소멸자 Empty e2(e1); // 복사 생성자 e2 = e1; //복사 대입 연산자

만약 하나밖에 없는 자원이라 복사생성자나 대입 연산자가 필요 없다면

pirvate:
    HomeForSale(const HomeForSale&);
    HomeForSale& operator=(const HomeForSale&);

임베디드 webkit browser 한글 폰트 설정

오후 4:20 Posted by PAN.SPOT , No comments
임베디드에서 webkit browser 한글이 깨져 나올때가 있다. 이때 fonts.conf 파일을 찾아서 
아래와 같이 변경 해준다. 

파일을 열어보면 다음과 같이 match 태그들을 볼 수 있다. 기본 폰트인 monospace,sans-serif 등을 볼 수 있는데 아래와 같이 기본폰트를 강제로 한글 표현이 가능한 폰트로 변경 한다. 

아래는 sans serif를 NanumGothic으로 변경한 예이다. 

<match target="pattern">
  <test qual="any" name="family">
          <string>sans serif</string>
  </test>
  <edit name="family" mode="assign" binding="strong">
          <string>NanumGothic</string>
  </edit>
</match>

위와 같은 방법으로 브라우저의 한글 폰트 설정을 강제로 변경 할 수 있다.

2015년 7월 21일 화요일

git 계통관계

오후 11:46 Posted by PAN.SPOT , , No comments
^ : 해당 커밋의 부모
^2 : 두번쨰 부모 (즉 부모가 두가지로 나눠져 있을 떄)
~: 해당 커밋의 부모
~2 : 해당 커밋의 부모의 부모

HEAD^^ = HEAD~2


2015년 7월 1일 수요일

If no other git process is currently running, this probably means a git process crashed in this repository earlier. Make sure no other git process is running and remove the file manually to continue.

오후 10:55 Posted by PAN.SPOT , No comments

git 사용중 아래와 같은 에러발생 시 

If no other git process is currently running, this probably means a
git process crashed in this repository earlier. Make sure no other git
process is running and remove the file manually to continue.
$rm /PROJECTDIR/.git/index.lock
$git reset

2015년 5월 5일 화요일

udev를 이용한 usb mount

오후 7:22 Posted by PAN.SPOT No comments

udev를 이용한 usb auto mount

임베디드 리눅스 시스템에서 usb를 auto mount/umount 할 수 있는 방법을 알아보자.
  1. 먼저 타켓 시스템에서 udevd daemon 동작 하고 있는지 확인 한다.
  • $ps -ef | grep udevd 검색되는게 없다면 데몬이 돌지 않는 것이다.
  • which udevd 또는 find / -name "udevd"로 설치되어 있는지 확인 한다.
  1. udevd daemon이 없다면, udev source 에서 다운받아서 타켓 시스템에 맞게 cross compile 후 타겟 시스템에 추가 한다.
  • 각각의 타켓 시스템에 맞게 컴파일 한 후 rootfs에 복사하여 적용 한다.
  1. 이벤트가 발생 할 때 실행 되는 rules 파일 생성 etc/udev/rules.d/XX-XXXX.rules
  • 기본적으로 etc/udev/rules.d/ 폴더에 있는 rules 스크립트를 차례로 실행 하게 되는데 해당 폴더에 스크립트를 작성한다. 여기서는 샘플로 파일명을 10-local.rules로 생성한다.
    udevd --daemon --debug로 실행 해보면 만약 rules.d 경로가 틀리면 error 메세지가 출력되며 폴더 경로가 어디인지 확인 할 수 있다.
     아래와 같이 스크립트를 작성 한다.스크립트 작성방법은 여러가지가 있다.
     
     ACTION=="add",KERNEL=="sd??",SUBSYSTEM=="block", RUN+="/usr/local/sbin/usbmount.sh 1 %k"
     ACTION=="remove",KERNEL=="sd??",SUBSYSTEM=="block", RUN+="/usr/local/sbin/cstusbmount.sh 2 %k"
     #ACTION=="remove",ENV{DEVNAME}=="/dev/sd[a-z][0-9]",SUBSYSTEM=="block",RUN+="/usr/local/etc/udev/rules.d/cstusbmount.sh 2 %k
    
    • ACTION : 이벤트가 발생 했을 때
    • KERNEL,SUBSYSTEM... : udevadm info를 이용하여 해당 usb를 삽입 한 후 정보를 확인 해보고 알맞는 정보를 사용 하면 된다.
    • RUN : 해당 상태가 맞으면 실행되는 스크립트
    첫번째줄은 해석 하면 삽입(add)이 발생 하였고 kernel 이 sdXX 이고 subsystem이 block 이면 usbmount.sh를 실행 하라는 스크립트 이다. 두번째,세번째줄은 반대로 usb가 제거 되었을때 umount 하려는 스크립트 이며 두개중에 아무거나 사용하여도 무방하다. 단 정보를 확인 하여 타겟시스템에 맞는 것을 사용하도록 한다. %k는 $KERNEL 과 같다. 보다 자세한 정보를 원한다면 udev rules를 이용 한다.
  1. 실제 usb를 마운트 하는 script 생성
     #!/bin/sh
     case $1 in
     1)
     #echo $2 >> /etc/udev/rules.d/add.txt
     mkdir -p /mnt/usb/$2
     mount /dev/$2 /mnt/usb/$2
     ;;
     2)
     #!/bin/sh
     #echo $2 > /etc/udev/rules.d/remove.txt
     umount /mnt/usb/$2
     rmdir /mnt/usb/$2
    
    단순히 rules 동작만 확인 하고 싶다면 RUN에 주석을 제거 하고 확인 해본다.
  2. 시작시에 udevd daemon이 동작 할 수 있도록 시작 script에 추가
    임베디드 리눅스의 경우 대부분 /etc/init.d/rcS에 추가 하면 된다. udevd --daemon 추가
참고로 busybox에서 제공하는 mdev도 있으니 mdev가 있다면 mdev를 사용하자.

predicate / function object / unary / binary

오후 5:38 Posted by PAN.SPOT , No comments

predicate / function object / unary / binary

용어정리

  • function object(함수객체 Functor) : 클래스에서 연산자 ()를 오버로딩 하여 해당 클래스의 인스턴스를 함수 형태로 호출할 수 있게 한 클래스 객체를 말한다.
  • predicate (술어) : 적어도 하나 이상의 개체를 전달받아서 bool 값을 반환 하는 함수 객체 이다.
  • unary : 단항
  • binary : 이항

STL 템플릿 예제로 확인 해보자.

예제

for_each

std::for_each
template <class InputIterator, class Function>
   Function for_each (InputIterator first, InputIterator last, Function fn);

parameters

매개변수를 확인 해보면 아래와 같다. 즉 iterator와 단항 함수 객체를 매개변수로 받는다.
*  @param  __first  An input iterator.
*  @param  __last   An input iterator.
*  @param  __f      A unary function object.

example

// for_each example
#include <iostream>     // std::cout
#include <algorithm>    // std::for_each
#include <vector>       // std::vector

void myfunction (int i) {  // function:
  std::cout << ' ' << i;
}

struct myclass {           // function object type: 
  void operator() (int i) {std::cout << ' ' << i;}
} myobject;

int main () {
  std::vector<int> myvector;
  myvector.push_back(10);
  myvector.push_back(20);
  myvector.push_back(30);

  std::cout << "myvector contains:";
  for_each (myvector.begin(), myvector.end(), myfunction);
  std::cout << '\n';

  // or:
  std::cout << "myvector contains:";
  for_each (myvector.begin(), myvector.end(), myobject);
  std::cout << '\n';

  return 0;
}
Output:
myvector contains: 10 20 30
myvector contains: 10 20 30
  • int i 하나를 매개변수로 가지고 () 연산자를 오버로딩하여 for_each 3번째 인자인 Function에 전달할 단항 함수 객체를 생성
      struct myclass {           //unary function object type: 
      void operator() (int i) {std::cout << ' ' << i;}
      } myobject;
    
위의 예제에서 function object / unary / binary 3가지에 대해 알아보았으니 predicate에 대해 알아보자.

예제

find_if

std::find_if
template <class InputIterator, class UnaryPredicate>
   InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred);

parameters

매개변수를 확인 해보면 , iterator 와 단항 술어 임을 확인 할 수 있다.
*  @param  __first  An input iterator.
*  @param  __last   An input iterator.
*  @param  __pred   A predicate.

example

// find_if example
#include <iostream>     // std::cout
#include <algorithm>    // std::find_if
#include <vector>       // std::vector


struct myclass {           // unary predicate 
  bool operator() (int i) {return ((i%2)==1);}
} myobject;

int main () {
  std::vector<int> myvector;

  myvector.push_back(10);
  myvector.push_back(25);
  myvector.push_back(40);
  myvector.push_back(55);

  std::vector<int>::iterator it = std::find_if (myvector.begin(), myvector.end(), myobject);
  std::cout << "The first odd value is " << *it << '\n';

  return 0;
}
Output:
The first odd value is 25
  • int i 하나를 매개변수로 가지고 () 연산자를 오버로딩하며 bool을 반환하는 find_if 3번째 인자인 Function에 전달할 단항 함수 개체를 생성 얼핏 보면 위의 예제와 다를 것이 없지만 bool을 반환 하는 함수 객체 즉 predicate라는 것을 알 수 있다.
      struct myclass {           // unary predicate 
        bool operator() (int i) {return ((i%2)==1);}
      } myobject;