[asterisk-commits] pbx.c: Minor code rearangements. (asterisk[master])

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Fri Apr 8 11:59:57 CDT 2016


Joshua Colp has submitted this change and it was merged.

Change subject: pbx.c: Minor code rearangements.
......................................................................


pbx.c: Minor code rearangements.

* Pull out a loop invariant.

* Convert an else-if ladder to a switch statement.

Change-Id: I0a95cfa9474a4600b9865f7b444534d275b37e95
---
M main/pbx.c
1 file changed, 63 insertions(+), 54 deletions(-)

Approvals:
  Joshua Colp: Looks good to me, approved; Verified
  George Joseph: Looks good to me, but someone else must approve



diff --git a/main/pbx.c b/main/pbx.c
index 7efbdee..87d8db0 100644
--- a/main/pbx.c
+++ b/main/pbx.c
@@ -3322,7 +3322,8 @@
 {
 	struct ao2_iterator cb_iter;
 	struct ast_state_cb *state_cb;
-	int state, same_state;
+	int state;
+	int same_state;
 	struct ao2_container *device_state_info;
 	int first_extended_cb_call = 1;
 	char context_name[AST_MAX_CONTEXT];
@@ -3361,7 +3362,8 @@
 	device_state_info = alloc_device_state_info();
 
 	state = ast_extension_state3(*hint_app, device_state_info);
-	if ((same_state = state == hint->laststate) && (~state & AST_EXTENSION_RINGING)) {
+	same_state = state == hint->laststate;
+	if (same_state && (~state & AST_EXTENSION_RINGING)) {
 		ao2_cleanup(device_state_info);
 		return;
 	}
@@ -3370,17 +3372,19 @@
 	hint->laststate = state;	/* record we saw the change */
 
 	/* For general callbacks */
-	cb_iter = ao2_iterator_init(statecbs, 0);
-	for (; !same_state && (state_cb = ao2_iterator_next(&cb_iter)); ao2_ref(state_cb, -1)) {
-		execute_state_callback(state_cb->change_cb,
-				       context_name,
-				       exten_name,
-				       state_cb->data,
-				       AST_HINT_UPDATE_DEVICE,
-				       hint,
-				       NULL);
+	if (!same_state) {
+		cb_iter = ao2_iterator_init(statecbs, 0);
+		for (; (state_cb = ao2_iterator_next(&cb_iter)); ao2_ref(state_cb, -1)) {
+			execute_state_callback(state_cb->change_cb,
+				context_name,
+				exten_name,
+				state_cb->data,
+				AST_HINT_UPDATE_DEVICE,
+				hint,
+				NULL);
+		}
+		ao2_iterator_destroy(&cb_iter);
 	}
-	ao2_iterator_destroy(&cb_iter);
 
 	/* For extension callbacks */
 	/* extended callbacks are called when the state changed or when AST_STATE_RINGING is
@@ -3395,12 +3399,12 @@
 		}
 		if (state_cb->extended || !same_state) {
 			execute_state_callback(state_cb->change_cb,
-					       context_name,
-					       exten_name,
-					       state_cb->data,
-					       AST_HINT_UPDATE_DEVICE,
-					       hint,
-					       state_cb->extended ? device_state_info : NULL);
+				context_name,
+				exten_name,
+				state_cb->data,
+				AST_HINT_UPDATE_DEVICE,
+				hint,
+				state_cb->extended ? device_state_info : NULL);
 		}
 	}
 	ao2_iterator_destroy(&cb_iter);
@@ -3458,12 +3462,12 @@
 	cb_iter = ao2_iterator_init(statecbs, 0);
 	for (; (state_cb = ao2_iterator_next(&cb_iter)); ao2_ref(state_cb, -1)) {
 		execute_state_callback(state_cb->change_cb,
-				       context_name,
-				       exten_name,
-				       state_cb->data,
-				       AST_HINT_UPDATE_PRESENCE,
-				       hint,
-				       NULL);
+			context_name,
+			exten_name,
+			state_cb->data,
+			AST_HINT_UPDATE_PRESENCE,
+			hint,
+			NULL);
 	}
 	ao2_iterator_destroy(&cb_iter);
 
@@ -3471,12 +3475,12 @@
 	cb_iter = ao2_iterator_init(hint->callbacks, 0);
 	for (; (state_cb = ao2_iterator_next(&cb_iter)); ao2_cleanup(state_cb)) {
 		execute_state_callback(state_cb->change_cb,
-				       context_name,
-				       exten_name,
-				       state_cb->data,
-				       AST_HINT_UPDATE_PRESENCE,
-				       hint,
-				       NULL);
+			context_name,
+			exten_name,
+			state_cb->data,
+			AST_HINT_UPDATE_PRESENCE,
+			hint,
+			NULL);
 	}
 	ao2_iterator_destroy(&cb_iter);
 }
@@ -3496,27 +3500,32 @@
 
 	hint = stasis_message_data(msg);
 
-	if (reason == AST_HINT_UPDATE_DEVICE) {
+	switch (reason) {
+	case AST_HINT_UPDATE_DEVICE:
 		device_state_notify_callbacks(hint, &hint_app);
-	} else if (reason == AST_HINT_UPDATE_PRESENCE) {
-		char *presence_subtype = NULL;
-		char *presence_message = NULL;
-		int state;
-
-		state = extension_presence_state_helper(
-			hint->exten, &presence_subtype, &presence_message);
-
+		break;
+	case AST_HINT_UPDATE_PRESENCE:
 		{
-			struct ast_presence_state_message presence_state = {
-				.state = state > 0 ? state : AST_PRESENCE_INVALID,
-				.subtype = presence_subtype,
-				.message = presence_message
-			};
-			presence_state_notify_callbacks(hint, &hint_app, &presence_state);
-		}
+			char *presence_subtype = NULL;
+			char *presence_message = NULL;
+			int state;
 
-		ast_free(presence_subtype);
-		ast_free(presence_message);
+			state = extension_presence_state_helper(
+				hint->exten, &presence_subtype, &presence_message);
+			{
+				struct ast_presence_state_message presence_state = {
+					.state = state > 0 ? state : AST_PRESENCE_INVALID,
+					.subtype = presence_subtype,
+					.message = presence_message
+				};
+
+				presence_state_notify_callbacks(hint, &hint_app, &presence_state);
+			}
+
+			ast_free(presence_subtype);
+			ast_free(presence_message);
+		}
+		break;
 	}
 
 	ast_free(hint_app);
@@ -3981,12 +3990,12 @@
 		cb_iter = ao2_iterator_init(statecbs, 0);
 		for (; (state_cb = ao2_iterator_next(&cb_iter)); ao2_ref(state_cb, -1)) {
 			execute_state_callback(state_cb->change_cb,
-					ast_get_context_name(ast_get_extension_context(e)),
-					ast_get_extension_name(e),
-					state_cb->data,
-					AST_HINT_UPDATE_DEVICE,
-					hint_new,
-					NULL);
+				ast_get_context_name(ast_get_extension_context(e)),
+				ast_get_extension_name(e),
+				state_cb->data,
+				AST_HINT_UPDATE_DEVICE,
+				hint_new,
+				NULL);
 		}
 		ao2_iterator_destroy(&cb_iter);
 	}

-- 
To view, visit https://gerrit.asterisk.org/2559
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I0a95cfa9474a4600b9865f7b444534d275b37e95
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Owner: Richard Mudgett <rmudgett at digium.com>
Gerrit-Reviewer: Anonymous Coward #1000019
Gerrit-Reviewer: George Joseph <george.joseph at fairview5.com>
Gerrit-Reviewer: Joshua Colp <jcolp at digium.com>



More information about the asterisk-commits mailing list