Thread: Shortening the code

  1. #1
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683

    Shortening the code

    I have a frw hundred lines of code which looks like this

    Code:
       case 0x0114:
       {
    	struct DisplayNotifyMessage ptr;
    	memcpy((void*)&ptr,s->receivebuffer,sizeof(struct DisplayNotifyMessage));
    	#ifdef DEBUG
    	ptr.m.display();
    	ptr.display();
    				#endif
    	
    	break;
       }
       case 0x0115:
       {
    	struct ClearNotifyMessage ptr;
    	memcpy((void*)&ptr,s->receivebuffer,sizeof(struct ClearNotifyMessage));
    	
    	#ifdef DEBUG
    	ptr.m.display();
    	ptr.display();
    				#endif
    	
    	break;
       }
       case 0x0116:
       {
    	struct ActivateCallplaneMessage ptr;
    	memcpy((void*)&ptr,s->receivebuffer,sizeof(struct ActivateCallplaneMessage));
    	
    	#ifdef DEBUG
    	ptr.m.display();
    	ptr.display();
    				#endif
    	
    	break;
       }
    is there some way i can shorten it... since most of the parts are similar execpt that the structure is different..... please advice

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    why not just write one function then pass the struct as one as the args
    Woop?

  3. #3
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Lightbulb See if this helps

    See if you can declare a macro in place of the
    #ifdef and #endif

    and use that macro.

    -Harsha.
    Last edited by vsriharsha; 08-27-2004 at 07:22 AM. Reason: betterment
    Help everyone you can

  4. #4
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Lightbulb Also...

    HI since you are not checking the condition of #else or #elseif.. why dont you do...
    Code:
    #ifdef DEBUG
     switch(whatever)
     {
         case 0x114:
              struct whatever *ptr;
              memcpy(......);
              break;
         case 0x115:
              struct whatever *ptr;
              memcpy(......);
              break;
            ...
            ...
    }
    ptr.m.display();
    ptr.display();
    ...
    ...
    since by the time the control reached the end of switch statement, the ptr is still valid.

    -Harsha.
    Help everyone you can

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    actully now that i think about it why not use a struct that declares an instance of your other structs then pass that struct with the one that you wish to use in a function that does your memcopy and so on. I don't know if i explained that well maybe this will help
    Code:
    #include <stdio.h>
    
    struct loser
    {
      int power;
    };
    struct brian
    {
      loser whatever;
    };  
    
    void setPower(brian &something);
    
    int main()
    {
      brian me;
      setPower(me);
      printf("Power: %i",me.whatever.power);
      getchar();  
    }
    
    void setPower(brian &something)
    {
      something.whatever.power=10;
    }
    Woop?

  6. #6
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Before I suggest anything:

    You seem to be copying function pointers from receivebuffer to the destination struct. How does this work?

    Could you post an example definition for one of the structs?

    The code you posted only copies a struct and calls a "common" (same signature) function pointer. It is possible that you may be able to get rid of the switch construct if this is really all you need to do. However, if your actual program needs to run different code depending on the message you may need the switch anyway.

    The simplest way to reduce your source code lines while keeping your code exactly the same would be a simple text replacement macro:

    Code:
       case 0x0114:
       {
    	COMMON_MESSAGE_CODE(DisplayNotifyMessage);
            
    	// Other code specific to this message.
       }
    
       case 0x0115:
       {
    	COMMON_MESSAGE_CODE(ClearNotifyMessage);
            
    	// Other code specific to this message.
       }
    Last edited by anonytmouse; 08-27-2004 at 07:41 AM.

  7. #7
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    Quote Originally Posted by vsriharsha
    HI since you are not checking the condition of #else or #elseif.. why dont you do...
    Code:
    #ifdef DEBUG
    switch(whatever)
    {
    case 0x114:
    struct whatever *ptr;
    memcpy(......);
    break;
    case 0x115:
    struct whatever *ptr;
    memcpy(......);
    break;
    ...
    ...
    }
    ptr.m.display();
    ptr.display();
    ...
    ...
    since by the time the control reached the end of switch statement, the ptr is still valid.

    -Harsha.
    nope i dontt think ptr is still valid as it is out of scope... and i cant make it global since i dont know what type of struct ptr will be pointing to..

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    Here's another lame idea

    Code:
    void func(int x)
    {
        A a; B b; C c;
    
        struct {
            void *p;
            size_t size;
        } Structs[] = {
            { &a, sizeof(A) },
            { &b, sizeof(B) },
            { &c, sizeof(C) }
        };
    
        memcpy(Structs[x%3].p, "   ", Structs[x%3].size);
    }

  9. #9
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    Quote Originally Posted by Laserve
    Here's another lame idea

    Code:
    void func(int x)
    {
    A a; B b; C c;
     
    struct {
    void *p;
    size_t size;
    } Structs[] = {
    { &a, sizeof(A) },
    { &b, sizeof(B) },
    { &c, sizeof(C) }
    };
     
    memcpy(Structs[x%3].p, " ", Structs[x%3].size);
    }
    that would solve a part of my problem.. but what abt calling the function display.. i cant do p->display() since p is a void pointer.....

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    union foo
    {
        struct DisplayNotifyMessage d;
        struct ClearNotifyMessage c;
        struct ActivateCallplaneMessage a;
    } bar;
    
    ...
    
        case 0x0114:
        {
            memcpy( &bar.d, s->receivebuffer, sizeof( bar.d ) );
    #ifdef DEBUG
            bar.d.m.display();
            bar.d.display();
    #endif
            break;
        }
        case 0x0115:
        {
            memcpy( &bar.d, s->receivebuffer , sizeof( bar.c ) );
    #ifdef DEBUG
            bar.c.m.display();
            bar.c.display();
    #endif
            break;
        }
        case 0x0116:
        {
            memcpy( &bar.a, s->receivebuffer, sizeof( bar.a ) );
    #ifdef DEBUG
            bar.a.m.display();
            bar.a.display();
    #endif
            break;
        }
    [edit]
    Or wrap the union inside a structure, and have your debug portion in the structure,
    then access the union member as needed.
    [/edit]

    Quzah.
    Last edited by quzah; 08-27-2004 at 03:19 PM.
    Hope is the first step on the road to disappointment.

  11. #11
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Lightbulb

    Quote Originally Posted by quzah
    Code:
    union foo
    {
        struct DisplayNotifyMessage d;
        struct ClearNotifyMessage c;
        struct ActivateCallplaneMessage a;
    } bar;
    
    ...
    
        case 0x0114:
        {
            memcpy( &bar.d, s->receivebuffer, sizeof( bar.d ) );
    #ifdef DEBUG
            bar.d.m.display();
            bar.d.display();
    #endif
            break;
        }
        case 0x0115:
        {
            memcpy( &bar.d, s->receivebuffer , sizeof( bar.c ) );
    #ifdef DEBUG
            bar.c.m.display();
            bar.c.display();
    #endif
            break;
        }
        case 0x0116:
        {
            memcpy( &bar.a, s->receivebuffer, sizeof( bar.a ) );
    #ifdef DEBUG
            bar.a.m.display();
            bar.a.display();
    #endif
            break;
        }
    But this will not reduce the code significantly, will it?
    Quote Originally Posted by quzah
    [edit]
    Or wrap the union inside a structure, and have your debug portion in the structure,
    then access the union member as needed.
    [/edit]
    Quzah.
    This should.

    -Harsha
    Help everyone you can

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM