local variables inside switch
authorFrantišek Kučera <franta-hg@frantovo.cz>
Fri, 22 Dec 2017 16:10:56 +0100
changeset 121ecfa42ca0d2
parent 11 1d1cc6552542
child 13 2a4b8b3abe14
local variables inside switch
c++/rgb-assembler/rgb-assembler.cpp
     1.1 --- a/c++/rgb-assembler/rgb-assembler.cpp	Fri Dec 22 00:21:29 2017 +0100
     1.2 +++ b/c++/rgb-assembler/rgb-assembler.cpp	Fri Dec 22 16:10:56 2017 +0100
     1.3 @@ -14,6 +14,8 @@
     1.4  typedef uint8_t sleep_t;
     1.5  typedef uint8_t color_t;
     1.6  
     1.7 +// TODO: strong typedefs http://www.boost.org/doc/libs/1_61_0/libs/serialization/doc/strong_typedef.html ?
     1.8 +
     1.9  const address_t MEMORY_SIZE = 1024;
    1.10  
    1.11  const command_t CMD_GOTO = 0x70;
    1.12 @@ -76,34 +78,38 @@
    1.13  		command_t ch = read<command_t>(memory, i);
    1.14  		wprintf(L"%02X  ", ch);
    1.15  
    1.16 -		color_t r;
    1.17 -		color_t g;
    1.18 -		color_t b;
    1.19 -
    1.20 -		sleep_t delay;
    1.21 -
    1.22  		switch (ch) {
    1.23  			case CMD_GOTO:
    1.24 +			{
    1.25  				i = read<address_t>(memory, i);
    1.26  				wprintf(L"GOTO %*d\n", 5, i);
    1.27  				break;
    1.28 +			}
    1.29  			case CMD_SLEEP:
    1.30 -				delay = read<sleep_t>(memory, i);
    1.31 +			{
    1.32 +				sleep_t delay = read<sleep_t>(memory, i);
    1.33  				wprintf(L"SLEEP %*d ms\n", 4, delay);
    1.34  				this_thread::sleep_for(chrono::milliseconds(delay));
    1.35  				break;
    1.36 +			}
    1.37  			case CMD_COLOR:
    1.38 -				r = read<color_t>(memory, i);
    1.39 -				g = read<color_t>(memory, i);
    1.40 -				b = read<color_t>(memory, i);
    1.41 +			{
    1.42 +				color_t r = read<color_t>(memory, i);
    1.43 +				color_t g = read<color_t>(memory, i);
    1.44 +				color_t b = read<color_t>(memory, i);
    1.45  				wprintf(L"COLOR  %02X %02X %02X\n", r, g, b);
    1.46  				break;
    1.47 +			}
    1.48  			case CMD_END:
    1.49 +			{
    1.50  				wprintf(L"END\n");
    1.51  				i = MEMORY_SIZE;
    1.52  				break;
    1.53 +			}
    1.54  			default:
    1.55 +			{
    1.56  				wprintf(L"invalid command\n");
    1.57 +			}
    1.58  		}
    1.59  
    1.60  	}