Thursday, June 9, 2011

making google preview


With phantomjs, I made a simple search preview like Google's preview.


When user input query, thumbnail for each search result is gathered simultaneously. This works with render function of phantomjs(webkit based script binary). The image it made is saved with hashed url as a filename so that it would not render twice. And it also highlight with red box where the query appears in the page(You can see little tiny red bordered box in the thumbnail above, right?). Though it consumes lots of system resources, it works.

Friday, March 4, 2011

C++ pointer, reference - **, *&, &&, &*


Let's think about pointer and reference with - &&, &*, ** and *&

  • int &&a;
    ILLEGAL : '&&' is a comparison syntax, not a pointer nor reference thing. Pass.

  • int &*a;
    ILLEGAL : (pointer-to-reference) Can't declare a pointer to int&. Reference is another name of an object(you can't set a pointer to a 'name').

  • int **a;
    pointer-to-pointer : You can use
    value(**a), pointer address(*a) and pointer's pointer address(a).

  • int *&a;
    reference-to-pointer : You can use
    value(*a), pointer address(a) and pointer's pointer address(&a).
Get confused :) ?

Thursday, February 10, 2011

C++ Exception

Exception is consist of detection and handling.

When an exception happens, program stops, finds where to handle, handles exception, and resumes from where it handled an eception.

Class, integer or string can be an exception object to be thrown.

Throwing an exception is done by

throw exceptionObject;

Try always goes with catch.

try {
throw "happened!";
}
catch (const char *msg) {
}
catch (...)
{
// handles all exceptions.
}


Exception can be sent to upper routine after handling.

catch (...) {
// handles exception
...
throw; // propagates again for the other handler
}


If no handler found, program ends with terminate().

Thinks

When a class constructor fails, only exception can make it known to the program.

Use of exception reduces 'if ... else ...' statements.

// Before,
if (condition) {
// do something
}
else {
// handle by itself
}

// After,
check_condition(); // raise exception.
// do something

Level of exception handler in the program is where it knows the exception and the way to handle it.(In other words, it's good to propagate an exception to the upper routine when it's not adequate to handle it there.)

Exception is different from hardware exception like seg-fault. That means there's 'throw' codes in the program.

This kind of handling code of resource releasing is not that good.

// not a good example.
try {
// alloc something.
}
catch (...) {
// release them.
throw;
}


Instead, resource alloc, release routine is good to be in the constructor, destructor code of the class("Resource aquisition is initialization" - Bjarne Stroustrup). "Because exception handling guarantees to call all destructors of local variables".

Standard exceptions.
C++ offers 'exception' basic class. This class has a virtual function 'what()' which returns const char * so, we can know what's going on during an exception with this message(throw() means this function will not throw an exception).
class exception {
public:
virtual const char * what() const throw();
...
}


So if your code has a catch statement like below, it catches all kinds of standard exception which inherits from the exception class.

catch (const std::exception &ex) {
std::cout << ex.what() << std::endl;
}
Notes

Auto pointer.

#include <memory>
...
auto_ptr p(new AClass); // will be released automatically.

Wednesday, January 12, 2011

c++ iostream 간단 요약

간단하게 정리해놨네. Good.

http://goo.gl/uh36O

Tuesday, December 28, 2010

Python egg

Very good explanation on Python egg package
  • http://mrtopf.de/blog/en/a-small-introduction-to-python-eggs/

Thursday, December 23, 2010

Strategy pattern implementation using member function pointer.

Strategy pattern is used when there are various ways to solve one problem. Normally each algorithm to solve problem is implemented as a class. And those classes are inherited from base class. However, it's also possible to implement those pattern using member function pointers though it looks a bit quirky like (instace.*instance.functionPointer)(...). :)


#include <iostream>

class Class {
public:
  typedef enum OPERATION {
    SUM = 0,
    MUL
  } operation;

  Class();

  int func(int a, int b, operation op);
  int (Class::*fp[2])(int a, int b);
private:

  int sum(int a, int b);
  int mul(int a, int b);
};

Class::Class()
{
  fp[SUM] = &Class::sum;
  fp[MUL] = &Class::mul;
}

int
Class::func(int a, int b, Class::operation op)
{
  return (this->*fp[op])(a, b);
}

int
Class::sum(int a, int b)
{
  return a + b;
}

int
Class::mul(int a, int b)
{
  return a * b;
}

int main()
{
  Class c;

  std::cout << c.func(2, 4, Class::SUM) << std::endl;
  std::cout << (c.*c.fp[Class::MUL])(2, 4) << std::endl;
  return 0;
}

Tuesday, November 23, 2010

번역 완료 - HTML5 활용



휴~ 번역 언제 끝나나 싶더니 벌써 예판 시작했네요. 이번 역서는 한참 회자되고 있는 HTML5 내용입니다. 구글의 개발자 어드버킷 마크 필그림의 책이구요. 얇은 책 내용안에 HTML5에 관해 알아야할 부분들을 조목조목 설명하고 있습니다. 자칫 일부 기능만 보다가 놓치기 쉬운 큰 그림에 대해 잘 설명해주소 있고, 기존 HTML의 역사라던가 어떻게 발전해왔는지 등에 대해서도 설명하고 있어서 꼭 한번 읽어볼만한 책입니다.