Friday, June 24, 2011

Recording Desktop at Ubuntu

Very simple.

Record with gtk-RecordMyDeskTop. Outfile format will be .ogv

To convert ogv to avi,

> mencoder -idx out.ogv -ovc lavc -oac mp3lame -o out.avi


Monday, June 13, 2011

C++: Building custom vector class.

Making custom container class with iterator would be a good example for C++ beginner.


#ifndef VECTOR_H
#define VECTOR_H

#include <vector>
#include "VectorIterator.h"

class Vector {
    typedef VectorIterator iterator;

    Vector(int size) { data_.resize(size); }

    int size() const;
    void push_back(const int val);

    VectorIterator begin();
    VectorIterator end();

    int& operator[](const int& idx);
    std::vector<int> data_;

inline int &
Vector::operator[](const int& idx) {
    return data_[idx];
inline int
Vector::size() const {
    return data_.size();

inline void
Vector::push_back(const int val) {

#endif /* VECTOR_H */


#include "Vector.h"

Vector::begin() {
    return VectorIterator(&data_);

Vector::end() {
    VectorIterator vec(&data_);
    return vec;



#include <vector>

class VectorIterator {

    VectorIterator(std::vector<int> * data);

    void setIndex(int v);
    int operator*();
    int operator++();
    bool operator==(const VectorIterator& rhs) const;
    bool operator!=(const VectorIterator& rhs) const;
    std::vector<int>* pvec_;
    int index_;

inline int
    return (*pvec_)[index_];

inline int
    return (*pvec_)[index_];

inline bool
VectorIterator::operator==(const VectorIterator& rhs) const
    return (pvec_ == rhs.pvec_ && index_ == rhs.index_);

inline bool
VectorIterator::operator!=(const VectorIterator& rhs) const
    return !(*this == rhs);


#include "VectorIterator.h"

VectorIterator::setIndex(int v) {
    index_ = v;

    : pvec_(NULL), index_(0) {}

VectorIterator::VectorIterator(std::vector<int> * data)
    : pvec_(data), index_(0) {}


#include <iostream>
#include "Vector.h"

int main()
    int size = 10;
    Vector vec(size);

    for (int i = 0; i < vec.size(); i++)
        vec[i] = 2 * i;

    for (Vector::iterator iter = vec.begin();
        iter != vec.end(); ++iter)
        std::cout << *iter << std::endl;

    return 0;

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().


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.

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 {
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;

Auto pointer.

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

Wednesday, January 12, 2011

c++ iostream 간단 요약

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