how to only indent brackets after case labels using clang-format - c++

I wanna a style that only indent brackets after case labels, while keeping case label not indented.
this is what I want:
switch(a)
{
case 1:
{
do_some_thing();
}
break;
}
I find an option IndentCaseLabels, but it will the whole things include the case label, neither true or false isn't what I want
true:
switch(a)
{
case 1:
{
do_some_thing();
}
break;
}
false:
switch(a)
{
case 1:
{
do_some_thing();
}
break;
}
Is this style possible in clang-format? If is, how could I Configure it?

It's just immediate above one you found in the manual.
IndentCaseBlocks: true
Indent case label blocks one level from the case label.
false: true:
switch (fool) { vs. switch (fool) {
case 1: { case 1:
bar(); {
} break; bar();
default: { }
plop(); break;
} default:
} {
plop();
}

Related

How to use multiple IR signals for doing the same thing?

I want to use different signals from different IR remotes to control a wheeled robot.
The robot is the Smart robot car kit v1.0 from Elegoo.
I used the infrared_remote_control_car.ino file from the disc with it.
I just added the #define JVC and the operators at the end.
The code looks like this:
#include <IRremote.h>
int receiverpin = 12;
int in1=9;
int in2=8;
int in3=7;
int in4=6;
int ENA=10;
int ENB=5;
int ABS=130;
unsigned long RED;
#define A 16736925
#define B 16754775
#define X 16712445
#define C 16720605
#define D 16761405
#define JVCfront 49816
#define JVCback 49688
#define JVCright 49704
#define JVCleft 49832
#define JVCmenu 49900
#define JVC3ok 49724
#define JVCstop 49856
IRrecv irrecv(receiverpin);
decode_results results;
void _mForward()
{
digitalWrite(ENA,HIGH);
digitalWrite(ENB,HIGH);
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
digitalWrite(in3,LOW);
digitalWrite(in4,HIGH);
Serial.println("go forward!");
}
void _mBack()
{
digitalWrite(ENA,HIGH);
digitalWrite(ENB,HIGH);
digitalWrite(in1,HIGH);
digitalWrite(in2,LOW);
digitalWrite(in3,HIGH);
digitalWrite(in4,LOW);
Serial.println("go back!");
}
void _mleft()
{
analogWrite(ENA,ABS);
analogWrite(ENB,ABS);
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
digitalWrite(in3,HIGH);
digitalWrite(in4,LOW);
Serial.println("go left!");
}
void _mright()
{
analogWrite(ENA,ABS);
analogWrite(ENB,ABS);
digitalWrite(in1,HIGH);
digitalWrite(in2,LOW);
digitalWrite(in3,LOW);
digitalWrite(in4,HIGH);
Serial.println("go right!");
}
void _mStop()
{
digitalWrite(ENA,LOW);
digitalWrite(ENB,LOW);
Serial.println("STOP!");
}
void setup() {
pinMode(in1,OUTPUT);
pinMode(in2,OUTPUT);
pinMode(in3,OUTPUT);
pinMode(in4,OUTPUT);
pinMode(ENA,OUTPUT);
pinMode(ENB,OUTPUT);
pinMode(receiverpin,INPUT);
Serial.begin(9600);
_mStop();
irrecv.enableIRIn();
}
void loop() {
if (irrecv.decode(&results))
{
RED=results.value;
Serial.println(RED);
irrecv.resume();
delay(150);
if(RED==(A || JVCfront))
{
_mForward();
}
else if(RED==(B or JVCback))
{
_mBack();
}
else if(RED==(C or JVCleft))
{
_mleft();
}
else if(RED==(D or JVCright))
{
_mright();
}
else if(RED==(X or JVCstop or JVCmenu or JVC3ok))
{
_mStop();
}
}
}
I tried different ways I saw on the internet for the OR operator as you can see.
Actually, the robot is always going forward.
Does the Serial.println(RED) always print out the expected value?
Your if-elseblock itself does not seem to be problematic at first glance. It doesn't matter if you use || or or. They are equivalent.
The problem is the way you are checking the value:
Therefore that you have parenthesis around your OR statement, you create a bool-value. if(RED==(A || JVCfront) translates to:
is A set or is JVCfront set; meaning, are they != 0 (yes they both are, so this expression is true
is RED == true (no it is not, because true represents 1 in integer)
so the code in that block is not being executed
If you want to solve it with an if-else, you need to:
if(RED == A or RED == JVCfront)
{
_mForward();
}
Anyway, I would suggest a switch-case statement:
switch(RED):
{
case A:
{
//intentional fallthrough
}
case JVCfront:
{
_mForward();
break;
}
case B:
{
//intentional fallthrough
}
case JVCback:
{
_mBack();
break;
}
case C:
{
//intentional fallthrough
}
case JVCleft:
{
_mleft();
break;
}
case D:
{
//intentional fallthrough
}
case JVCright:
{
_mright();
break;
}
case X:
{
//intentional fallthrough
}
case JVCstop:
{
//intentional fallthrough
}
case JVCmenu:
{
//intentional fallthrough
}
case JVC3ok:
{
_mStop();
break;
}
default:
{
_mStop();
}
}
I personally find this kind of code much easier to read, and more understandable.
Please note: If you dont put a break into a case, it automatically executes the next case as well. This is why I wrote //intentional fallthrough in this part of the code. P.e. when case A is executed, nothing happens. It just falls through into case JVCFront and executes everything there.

C++ #defining enum states

I am wondering if I made a good decision when I defined all states of my enum to their shorter counterparts: just to tidy up the code.Code:Enum:
enum class ESelectedCharacterState : uint8
{
SS_WantsWalk,
SS_WantsJog,
SS_WantsCrouch,
SS_WantsProne,
SS_WantsJump
};
Defining:
#define WantsWalk ESelectedCharacterState::SS_WantsWalk
#define WantsJog ESelectedCharacterState::SS_WantsJog
#define WantsCrouch ESelectedCharacterState::SS_WantsCrouch
#define WantsProne ESelectedCharacterState::SS_WantsProne
#define WantsJump ESelectedCharacterState::SS_WantsJump
Case with no #defined enum states:
switch (StateSelected)
{
case ESelectedCharacterState::SS_WantsWalk:
break;
case ESelectedCharacterState::SS_WantsJog:
break;
case ESelectedCharacterState::SS_WantsCrouch:
break;
case ESelectedCharacterState::SS_WantsProne:
break;
case ESelectedCharacterState::SS_WantsJump:
break;
default:
break;
}
Case with #defined enum states:
switch (StateSelected)
{
case WantsWalk:
break;
case WantsJog:
break;
case WantsCrouch:
break;
case WantsProne:
break;
case WantsJump:
break;
default:
break;
}
This is actually a small bit of code but I use this enum very frequently in my project.
Using a typedef would be a cleaner way to achieve this, by making your enum's type shorter.
typedef ESelectedCharacterState ESCS;

C++ Enum to String Switch Not Working

I am instantiating an object with a few enum types and trying to set some string members based on those enum types. However, when I am debugging and step though, the switch used to set the strings hits every case, and each string gets set to the last case for each enum type.
enum Number {
one,
two,
three
};
enum Color {
purple,
red,
green
};
enum Shading {
solid,
striped,
outlined
};
enum Shape {
oval,
squiggle,
diamond
};
Card::Card(Number num, Color colour, Shading shade, Shape shaper) {
number_ = num;
color_ = colour;
shading_ = shade;
shape_ = shaper;
setStrings();
}
void Card::setStrings() {
switch (number_) {
case one:
number_string = "one";
case two:
number_string = "two";
case three:
number_string = "three";
}
switch(color_) {
case purple:
color_string = "purple";
case red:
color_string = "red";
case green:
color_string = "green";
}
switch (shading_) {
case solid:
shading_string = "solid";
case striped:
shading_string = "striped";
case outlined:
shading_string = "outlined";
}
switch (shape_) {
case oval:
shape_string = "oval";
case squiggle:
shape_string = "squiggle";
case diamond:
shape_string = "diamond";
}
}
Every card I instantiate using the overloaded constructor has number_string = "three", color_string = "green", shading_string = "outlined", and shape_string = "diamond".
You need to use break for switch statements' case clause else it is a fall through. Here is an example and details for you. https://10hash.com/c/cf/#idm45440468325552
#include <stdio.h>
int main()
{
int i = 65;
switch(i)
{
case 'A':
printf("Value of i is 'A'.\n");
break;
case 'B':
printf("Value of i is 'B'.\n");
break;
default:
break;
}
return 0;
}
Your switch-case is not correct. You need to put a break after every case for your solution otherwise it will go into every case until it is finished and not break when it hits the case you want.

Switch statement doesn't work with enum (C++)

enum Maximum_Value{
MAXIMUM_VALUE_1 = 0,
MAXIMUM_VALUE_7 = 1,
MAXIMUM_VALUE_15 = 2,
MAXIMUM_VALUE_26 = 3,
MAXIMUM_VALUE_34 = 4
};
int value_from_function = functionetc();
switch(value_from_function){
MAXIMUM_VALUE_1: printf("MAXIMUM_VALUE_1 :%x\n",value_from_function); break;
MAXIMUM_VALUE_7: printf("MAXIMUM_VALUE_7 :%x\n",value_from_function); break;
MAXIMUM_VALUE_15: printf("MAXIMUM_VALUE_15 %x\n",value_from_function); break;
MAXIMUM_VALUE_26: printf("MAXIMUM_VALUE_26 %x\n",value_from_function); break;
MAXIMUM_VALUE_34: printf("MAXIMUM_VALUE_34 %x\n",value_from_function); break;
default: printf("default :%x\n",value_from_function);
}
The code above always seems to hit the default statement, printing "default :0" even though that should hit MAXIMUM_VALUE_1.
I've tried casting the variable in switch to no effect
I guess I should save the return value into a variable of type "Maximum_Value", but why doesn't the code work anyway?
Edit: Thanks for pointing out the awfully stupid mistake everyone :P. The root of the problem was copying coding from systemverilog, which uses 'case' as a keyword instead of 'switch', and doesn't require 'case' at the start of each case
Enumerators aren't labels but switch statements jump to labels. You use case to create a label switch statements can jump to:
case MAXIMUM_VALUE_1: ...; break;
Add case keyword then it will work.
case MAXIMUM_VALUE_1: printf("MAXIMUM_VALUE_1 :%x\n",value_from_function); break;
case MAXIMUM_VALUE_7: printf("MAXIMUM_VALUE_7 :%x\n",value_from_function); break;
case MAXIMUM_VALUE_15: printf("MAXIMUM_VALUE_15 %x\n",value_from_function); break;
case MAXIMUM_VALUE_26: printf("MAXIMUM_VALUE_26 %x\n",value_from_function); break;
case MAXIMUM_VALUE_34: printf("MAXIMUM_VALUE_34 %x\n",value_from_function); break;
default: printf("default :%x\n",value_from_function);
you are missing the case keyword before every label!
syntax of switch case is-
switch(type){
case type1: ....; break;
case type2: ....; break;
......
default: .....;
}

QmodelIndex and header data

I have a 4 tableviews in my ui screen .In my header data function for 4 views , I just need one 1 row name- temperature for view 1 and 3 and four rows with name field 1x,field 4x, field 10x, field 40x respectively.
My function is
virtual QVariant headerData(int section,Qt::Orientation orientation,
int role = Qt::DisplayRole) const
{
switch(role)
{
case Qt::DisplayRole:
switch (orientation)
{
case Qt::Vertical:
switch (m_channel)
{
case 0:
switch (section) // Range
{
case 0:
return "Temperature1";
}
case 1:
switch (section) // Range
{
case 0:
return "Field 1x range";
case 1:
return "Field 4x range";
case 2:
return "Field 10x range";
case 3:
return "Field 40x range";
}
case 2:
switch (section) // Range
{
case 0:
return "Temperature2";
}
case 3:
switch (section) // Range
{
case 0:
return "Field 1x range";
case 1:
return "Field 4x range";
case 2:
return "Field 10x range";
case 3:
return "Field 40x range";
}
But, the screen when compiled shows temperature,field 4x, field 10x, field 40x for views 1 and view 3, which I don't wont
Please help
You are missing breaks in your switch statement. For example:
switch (m_channel)
{
case 0:
switch (section) // Range
{
case 0:
return "Temperature1";
}
break; // <-- You need this.
case 1:
...
It's also generally a good idea to provide a default label for switch statements.