Tips page

Collection of tips and howto-s.

Filesystem tricks

  1. How to create ext3 file system?

    1. Kernel must support ext3. You have to patch the kernel. See ext3 for 2.4
    2. Use mke2fs with -j parameter. See man mke2fs.

Various C++ Tips

  1. How to create a class that can be created with operator new only?

    1. Declare class' destructor private. Implement a member function to destroy the object or maku sure that the class can destroy itself (see cmn_Thread and derivates).

    Example:

    class Detached : public cmn_Thread
    {
    public:
        Detached();
    
    private:
        virtual ~Detached();
    
    
    public:
        /**
         Main function of the thread. It is started when a Start() function
         is invoked on this class. It receives arguments, specified at
         construction time.
         
         NOTE: An instance of this class must be created on a heap with new.
               It is deleted automatically after the thread execution completes by
               calling
               
               delete this;
         */
        virtual void Run(void* a_arg);
    };
        

  2. How to swap contents of a STL container to another without copying members?

    STL containers have a member function swap() that is used to exchange contents of two containers.

    Example:

    map<string> a;
    map<string> b;
    
    // Do some operations to fill map a with 106378 strings.
    // Now swap contents (a gets contents of b and vice versa).
    
    a.swap(b);
    
    // Same can be achieved by using:
    // b.swap(a);
        

  3. How to make instance on willing addres new ((void*) p) type(argumrnts)

    Description: The most useful situation is at vector container. Instead of using copy constructor to put instance to proper vector element storage, it is faster that you instance the oblject directly on willing address using vector iterator as pointer.

    The same is for all other already alocated buffers, except that at buffers you have to call destructor too.

    Example:

    class A {
    public:
     A() { s = NULL;};
     ~A() {
      if (s !=NULL) {
       delete s;
      }
     };
     
     A(char a, char b) {
      i =a;
      j = b;
      k = 'x';
      s = new char[3];
     };
     
     A( const A &a) {
      i =a.i;
      j = a.j;
      k = a.k;
      s = new char[3];
     };
     
    private:
     char i;
     char j;
     char k;
     char *s;
    };
     
    int main(int argc, char *argv[]) {
        log_FUNC_m(main);
     
     char *p = new char[1000];    // some buffer
     
     new ((void*) p) A('g','h');  // instance directly into buffer
     char *r = p;
     r += sizeof(A);		      // increase pointer to next location
     new  (r) A('g','h');		
     
     A *t = (A*)p;				// t is a pointer to first instance
     t->~A();					// call a destructor for first instance
     t++;						// next instance
     t->~A();					// call a destructor for second instance
    
     delete p;	// remove whole buffer, do not need to remove one by one from heap
    
    // instantiation in vector
     vector<A> v;
     v.resize(2);		// make enought space for instances
     vector<A>::iterator i = v.begin();  // set the iterator
     new ((void*) i) A('a','b'); // instantiate oblject directli into vector
     						 // no copy constructor and destructor is need
     i++;					 // next element
     new ((void*) i) A('c','d');  // instance again, this time on secont element
     
     // destructor is no need to call. All is done by vector itself.
    }        
        

    This tip is very useful, but Init method used as constructor is stil faster for 25%

    Example:

    class A {
    public:
     A() { s = NULL;};
     ~A() {
      if (s !=NULL) {
       delete s;
      }
     };
     
     Init(char a, char b) {
      i =a;
      j = b;
      k = 'x';
      s = new char[3];
     };
     
     A( const A &a) {
      i =a.i;
      j = a.j;
      k = a.k;
      s = new char[3];
     };
     
    private:
     char i;
     char j;
     char k;
     char *s;
    };
     
    int main(int argc, char *argv[]) {
     
    	char *p = new char[1000];    // some buffer
    
    	A *t = (A*)p;				// t is a pointer to first instance
    
    	t->Init('a','b');	// this initialization is faster than previous tip
    	t++	 
    	t->Init('c','d');
    	...
    }
        

Threads and synchronization

Multithreading, mutexes, semaphores, conditions.

  1. cmn_Thread is a platform independent thread implementation. See some additional information there.

  2. Use template file templates/ClassThreadTemplate.cpp to create new classes that are inherited from cmn_Thread.

  3. Developer Studio tip: Add the path $IVD3RDPARTY/omni/win32/ to the Tools -> Options -> Directories -> Library Files.
    See CORBA IPC implementation hints for include files.