<commit_before>/**
 * Copyright 2013 opencxa.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <cxa_config.h>
#include "cxa_stateMachine.h"


/**
 * @author <NAME>
 */


// ******** includes ********
#include <cxa_assert.h>
#include <cxa_runLoop.h>
#include <cxa_timeBase.h>

#define CXA_LOG_LEVEL		CXA_LOG_LEVEL_INFO
#include <cxa_logger_implementation.h>


// ******** local macro definitions ********


// ******** local type definitions ********


// ******** local function prototypes ********
static void cb_onRunLoopUpdate(void* userVarIn);

static cxa_stateMachine_state_t* getState_byId(cxa_stateMachine_t *const smIn, int idIn);


// ********  local variable declarations *********


// ******** global function implementations ********
void cxa_stateMachine_init(cxa_stateMachine_t *const smIn, const char* nameIn)
{
	cxa_assert(smIn);
	cxa_assert(nameIn);
	
	// set some sensible defaults
	smIn->currState = NULL;
	smIn->nextState = NULL;
	
	// setup our internal state
	cxa_array_init(&smIn->states, sizeof(*smIn->states_raw), (void*)smIn->states_raw, sizeof(smIn->states_raw));
	
	// setup our logger if it's enabled
	#ifdef CXA_STATE_MACHINE_ENABLE_LOGGING
	cxa_logger_vinit(&smIn->logger, "fsm::%s", nameIn);
	#endif
	
	// a timediff was _not_ supplied so we cannot do timed states
	// even if they are enabled
	#ifdef CXA_STATE_MACHINE_ENABLE_TIMED_STATES
	cxa_timeDiff_init(&smIn->td_timedTransition, true);
	smIn->timedStatesEnabled = true;
	#endif

	// register for run loop execution
	cxa_runLoop_addEntry(cb_onRunLoopUpdate, (void*)smIn);
}


void cxa_stateMachine_addState(cxa_stateMachine_t *const smIn, int idIn, const char* nameIn,
	cxa_stateMachine_cb_enter_t cb_enterIn, cxa_stateMachine_cb_state_t cb_stateIn, cxa_stateMachine_cb_leave_t cb_leaveIn,
	void *userVarIn)
{
	cxa_assert(smIn);
	cxa_assert(nameIn);
	cxa_assert(idIn != CXA_STATE_MACHINE_STATE_UNKNOWN);

	// make sure we don't already have this state added
	cxa_assert(getState_byId(smIn, idIn) == NULL);

	// create our new state
	cxa_stateMachine_state_t newState = {.type=CXA_STATE_MACHINE_STATE_TYPE_NORMAL, .stateId=idIn, .stateName=nameIn,
		.cb_enter=cb_enterIn, .cb_state=cb_stateIn, .cb_leave=cb_leaveIn, .userVar=userVarIn};

	// add the new state to our array of states
	cxa_assert(cxa_array_append(&smIn->states, &newState));
	
	// if we're currently not in a known state, enter this state (when update is called)
	if( smIn->currState == NULL ) cxa_stateMachine_transition(smIn, idIn);
}


#ifdef CXA_STATE_MACHINE_ENABLE_TIMED_STATES<commit_msg>Changed function name to match previous changes, added useful assert message<commit_after>/**
 * Copyright 2013 opencxa.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "cxa_stateMachine.h"


/**
 * @author <NAME>
 */


// ******** includes ********
#include <cxa_assert.h>
#include <cxa_runLoop.h>
#include <cxa_timeBase.h>

#define CXA_LOG_LEVEL		CXA_LOG_LEVEL_INFO
#include <cxa_logger_implementation.h>


// ******** local macro definitions ********


// ******** local type definitions ********


// ******** local function prototypes ********
static void cb_onRunLoopUpdate(void* userVarIn);

static cxa_stateMachine_state_t* getState_byId(cxa_stateMachine_t *const smIn, int idIn);


// ********  local variable declarations *********


// ******** global function implementations ********
void cxa_stateMachine_init(cxa_stateMachine_t *const smIn, const char* nameIn)
{
	cxa_assert(smIn);
	cxa_assert(nameIn);
	
	// set some sensible defaults
	smIn->currState = NULL;
	smIn->nextState = NULL;
	
	// setup our internal state
	cxa_array_init(&smIn->states, sizeof(*smIn->states_raw), (void*)smIn->states_raw, sizeof(smIn->states_raw));
	
	// setup our logger if it's enabled
	#ifdef CXA_STATE_MACHINE_ENABLE_LOGGING
	cxa_logger_init_formattedString(&smIn->logger, "fsm::%s", nameIn);
	#endif
	
	// a timediff was _not_ supplied so we cannot do timed states
	// even if they are enabled
	#ifdef CXA_STATE_MACHINE_ENABLE_TIMED_STATES
	cxa_timeDiff_init(&smIn->td_timedTransition, true);
	smIn->timedStatesEnabled = true;
	#endif

	// register for run loop execution
	cxa_runLoop_addEntry(cb_onRunLoopUpdate, (void*)smIn);
}


void cxa_stateMachine_addState(cxa_stateMachine_t *const smIn, int idIn, const char* nameIn,
	cxa_stateMachine_cb_enter_t cb_enterIn, cxa_stateMachine_cb_state_t cb_stateIn, cxa_stateMachine_cb_leave_t cb_leaveIn,
	void *userVarIn)
{
	cxa_assert(smIn);
	cxa_assert(nameIn);
	cxa_assert(idIn != CXA_STATE_MACHINE_STATE_UNKNOWN);

	// make sure we don't already have this state added
	cxa_assert(getState_byId(smIn, idIn) == NULL);

	// create our new state
	cxa_stateMachine_state_t newState = {.type=CXA_STATE_MACHINE_STATE_TYPE_NORMAL, .stateId=idIn, .stateName=nameIn,
		.cb_enter=cb_enterIn, .cb_state=cb_stateIn, .cb_leave=cb_leaveIn, .userVar=userVarIn};

	// add the new state to our array of states
	cxa_assert_msg(cxa_array_append(&smIn->states, &newState), "increase 'CXA_STATE_MACHINE_MAX_NUM_STATES'");
	
	// if we're currently not in a known state, enter this state (when update is called)
	if( smIn->currState == NULL ) cxa_stateMachine_transition(smIn, idIn);
}


#ifdef CXA_STATE_MACHINE_ENABLE_TIMED_STATES
{'fraction_non_alphanumeric': 0.12222044472884339, 'fraction_numerical': 0.0025595904655255157, 'mean_word_length': 5.30241935483871, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7231135', 'n_tokens_mistral': 2191, 'n_tokens_neox': 2102, 'n_words': 555}