<commit_before>/// \copyright Copyright 2011-2015 <NAME> <<EMAIL>>. All rights reserved.
/// 
/// \license This project is released under the <a href="https://opensource.org/licenses/MIT">MIT License</a>.
/// 
/// Permission is hereby granted, free of charge, to any person obtaining
/// a copy of this software and associated documentation files (the
/// "Software"), to deal in the Software without restriction, including
/// without limitation the rights to use, copy, modify, merge, publish,
/// distribute, sublicense, and/or sell copies of the Software, and to
/// permit persons to whom the Software is furnished to do so, subject to
/// the following conditions:
/// 
/// The above copyright notice and this permission notice shall be included
/// in all copies or substantial portions of the Software.
/// 
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
/// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
/// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
/// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
/// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
/// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
/// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
///
/// \brief arc - a realtime applications framework for native & web

//@file arc.h

/*! \mainpage Main Page
Arc is a lightweight applications development framework for realtime apps, specifically geared for games, simulations, and rich user experiences. See [README](md__r_e_a_d_m_e.html "README") for more. 

Source is available [here](https://github.com/ArcaneIngenuity/arc "arc"). 
 */
#ifndef ARC_H
#define ARC_H

#include <stdbool.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include "klib/kvec.h"
#include "klib/khash.h"
#include "ezxml/ezxml.h"
#include "../log/log.h"
#include "arctypes.h"

//#define SERVICES_MAX 16
#define APPS_MAX 4
#define DEVICES_MAX 16
#define STRLEN_MAX 64

// setup khash for key/value types
// shorthand way to get the key from hashtable or defVal if not found
#define kh_get_val(kname, hash, key, defVal) ({k=kh_get(kname, hash, key);(k!=kh_end(hash)?kh_val(hash,k):defVal);})

// shorthand way to set value in hash with single line command.  Returns value
// returns 0=replaced existing item, 1=bucket empty (new key), 2-adding element previously deleted
#define kh_set(kname, hash, key, val) ({int ret; k = kh_put(kname, hash,key,&ret); kh_value(hash,k) = val; ret;})

#ifndef KH_DECL_STRPTR
#define KH_DECL_STRPTR
static const int StrPtr = 36;
KHASH_DECLARE(StrPtr, kh_cstr_t, uintptr_t)
#endif//KH_DECL_STRPTR

/// Describes a method on some object instance (usually a View) that handles a published event. 
typedef void (*SubHandler)(void * this, void * event);

/// A Sub(scriber) to some Pub(lisher).

/// Wraps the subscribing instance, coupled with its SubHandler-conformant method, into a single structure for Pub(lisher) use.
typedef struct Sub
{
	void * instance; ///< The object instance i.e. the actual subscriber.
	SubHandler handler; ///< The method to call on the instance, to handle a published event.
} Sub;

/// A Pub(lisher) that publishes to Sub(scriber)s.

/// One instance hereof handles one specific event type. For more event types, attach more Pubs to the same (sub)model.
typedef struct Pub
{
	char name[STRLEN_MAX]; ///< The name by which this Pub(lisher) is referenced (used as key).
	kvec_t(Sub) subsList; ///< The list of Sub(scriber)s to which this Pub(lisher) publishers events.
} Pub;

/// Value type for kvec_t(ArcString) (char array not possible).
typedef struct ArcString
{
	char name[STRLEN_MAX];
} ArcString;


/// Base class for a user-defined extension created from config.
typedef struct Extension
{
	char id[STRLEN_MAX]; ///< ID by which a user extension is retrieved from its owning View, Ctrl, App or Hub's ->extensionsById.
} Extension;

/// Base class for a user-defined view that has specific rendering responsibilities.

/// For every discrete View needed, allocate it and set the appropriate callbacks.<br>
/// When implementing a View this way, remember that it should be monolithic and represent a conceptual viewport that may be activated / deactivated, rather than every instance of something visible. Examples: in a game, a View might be the first person renderer rather than every instance of a game character; in a UI application, a View is better suited to representing a specific UI panel rather than each of the many controls within that Panel.<br>
/// A View instance may then be added to an App by setting App.view; this will be the root View. Further View%s should then be added as children of the root View, and so on, creating a View tree.<br>
/// Views may be accessed either by their View.id (which is set on construction) or zero-based depth/Z index.<br>
/// A View reads from a model in order to update its state. See Ctrl's description for more on models.
///
typedef struct View
{
	//TODO fix this!
	char id[STRLEN_MAX]; ///< (unimplemented) ID by which a View is retrieved from its App->viewsById.
	struct Hub * hub; ///< \brief The Hub which owns and manages this View's owner App.
	struct App * app; ///< \brief The App which owns and manages this View.
	
	bool updating; ///< Should this have View_update() called on it every frame?
	bool initialised; ///< True after first initialise. If re- initialise is required, this should be manually reset to false.
	void * model; ///< The model associated with this View. May or may not be the same as this View's App's model, depending on \link Configuration \endlink.
	
	struct View * root;
	struct View * parent; ///< View's parent View, if any.
	kvec_t(struct View *) childrenByZ; ///< View's children, where index is Z-order (Z is the stacking/draw order, i.e. goes positive out of screen).
	//int childrenCount; ///< The number of child Views held by this parent View. Negative for invalid return values (e.g. on seek).
	
	int width; ///< Width of this View. (NEEDS REVIEW - should be float to accommodate any user units; perhaps should be in user subclass of View)
	int height; ///< Height of this View. (NEEDS REVIEW - should be float to accommodate any user units; perhaps should be in user subclass of View)

	khash_t(StrPtr) * extensionsById; ///< Extensions included on this View instance, if using config.
	kvec_t(ArcString) extensionIds; ///< Array of fixed-length cstrings used as keys to extensionsById (required once XML and its source strings are freed).
	
	void (*start)(struct View * const this); ///< \brief User-supplied callback for when this View start()s.
	void (*stop)(struct View * const this); ///< \brief User-supplied callback for when this View stop()s.
	void (*suspend)(struct View * const this); ///< \brief User-supplied callback for when this View must suspend() due to a loss of rendering context.
	void (*resume)(struct View * const this); ///< \brief User-supplied callback for when this View must resume() due to regaining rendering context.
	void (*initialise)(struct View * const this); ///< \brief User-supplied callback for when this View initialise()s.
	void (*dispose)(struct View * const this); ///< \brief User-supplied callback for when this View dispose()s of its resources.
	void (*update)(struct View * const this); ///< \brief User-supplied callback for when this View update()s, i.e. update this View before its children update.
	void (*updatePost)(struct View * const this); ///< \brief User-supplied callback for when this View updatePost()s, i.e. update this View after its children update.
	void (*onParentResize)(struct View * const this); ///< \brief User-supplied callback for when this View's parent is resized. Root View resize is handled by some external (platform-specific) callback.
	//void (*enable)(struct View * const this); //start
	//void (*disable)(struct View * const this); //stop
} View;

/// Base class for a user-defined controller that handles specific game / business / simulation logic within an App.

/// For every discrete Ctrl needed, allocate it and set the appropriate callbacks.<br>
/// A Ctrl instance may then be added to an App by setting App.ctrl; this will be the root Ctrl. Often, this will be the only Ctrl needed; though in some cases the user may prefer to use sub-%Ctrl%s attached to root.<br>
/// Sub-%Ctrl%s may be accessed through the App's root Ctrl.<br>
/// A Ctrl operates on a model. In arc, models need no specific type; they can be anything (and are internally denoted as void *). Ctrl writes new model state by reading extant model state, inputs and View state.
typedef struct Ctrl
{
	char id[STRLEN_MAX]; ///< ID used for Arc debugging mode.
	struct Hub * hub; ///< \brief The Hub which owns and manages this Ctrl's owner App.
	struct App * app; ///< \brief The App which owns and manages this Ctrl.<commit_msg>Removed View width and height in favour of position, dimensions, orientation and scale float arrays.<commit_after>/// \copyright Copyright 2011-2015 <NAME> <<EMAIL>>. All rights reserved.
/// 
/// \license This project is released under the <a href="https://opensource.org/licenses/MIT">MIT License</a>.
/// 
/// Permission is hereby granted, free of charge, to any person obtaining
/// a copy of this software and associated documentation files (the
/// "Software"), to deal in the Software without restriction, including
/// without limitation the rights to use, copy, modify, merge, publish,
/// distribute, sublicense, and/or sell copies of the Software, and to
/// permit persons to whom the Software is furnished to do so, subject to
/// the following conditions:
/// 
/// The above copyright notice and this permission notice shall be included
/// in all copies or substantial portions of the Software.
/// 
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
/// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
/// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
/// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
/// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
/// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
/// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
///
/// \brief arc - a realtime applications framework for native & web

//@file arc.h

/*! \mainpage Main Page
Arc is a lightweight applications development framework for realtime apps, specifically geared for games, simulations, and rich user experiences. See [README](md__r_e_a_d_m_e.html "README") for more. 

Source is available [here](https://github.com/ArcaneIngenuity/arc "arc"). 
 */
#ifndef ARC_H
#define ARC_H

#include <stdbool.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include "klib/kvec.h"
#include "klib/khash.h"
#include "ezxml/ezxml.h"
#include "../log/log.h"
#include "arctypes.h"

//#define SERVICES_MAX 16
#define APPS_MAX 4
#define DEVICES_MAX 16
#define STRLEN_MAX 64

// setup khash for key/value types
// shorthand way to get the key from hashtable or defVal if not found
#define kh_get_val(kname, hash, key, defVal) ({k=kh_get(kname, hash, key);(k!=kh_end(hash)?kh_val(hash,k):defVal);})

// shorthand way to set value in hash with single line command.  Returns value
// returns 0=replaced existing item, 1=bucket empty (new key), 2-adding element previously deleted
#define kh_set(kname, hash, key, val) ({int ret; k = kh_put(kname, hash,key,&ret); kh_value(hash,k) = val; ret;})

#ifndef KH_DECL_STRPTR
#define KH_DECL_STRPTR
static const int StrPtr = 36;
KHASH_DECLARE(StrPtr, kh_cstr_t, uintptr_t)
#endif//KH_DECL_STRPTR

/// Describes a method on some object instance (usually a View) that handles a published event. 
typedef void (*SubHandler)(void * this, void * event);

/// A Sub(scriber) to some Pub(lisher).

/// Wraps the subscribing instance, coupled with its SubHandler-conformant method, into a single structure for Pub(lisher) use.
typedef struct Sub
{
	void * instance; ///< The object instance i.e. the actual subscriber.
	SubHandler handler; ///< The method to call on the instance, to handle a published event.
} Sub;

/// A Pub(lisher) that publishes to Sub(scriber)s.

/// One instance hereof handles one specific event type. For more event types, attach more Pubs to the same (sub)model.
typedef struct Pub
{
	char name[STRLEN_MAX]; ///< The name by which this Pub(lisher) is referenced (used as key).
	kvec_t(Sub) subsList; ///< The list of Sub(scriber)s to which this Pub(lisher) publishers events.
} Pub;

/// Value type for kvec_t(ArcString) (char array not possible).
typedef struct ArcString
{
	char name[STRLEN_MAX];
} ArcString;


/// Base class for a user-defined extension created from config.
typedef struct Extension
{
	char id[STRLEN_MAX]; ///< ID by which a user extension is retrieved from its owning View, Ctrl, App or Hub's ->extensionsById.
} Extension;

/// Base class for a user-defined view that has specific rendering responsibilities.

/// For every discrete View needed, allocate it and set the appropriate callbacks.<br>
/// When implementing a View this way, remember that it should be monolithic and represent a conceptual viewport that may be activated / deactivated, rather than every instance of something visible. Examples: in a game, a View might be the first person renderer rather than every instance of a game character; in a UI application, a View is better suited to representing a specific UI panel rather than each of the many controls within that Panel.<br>
/// A View instance may then be added to an App by setting App.view; this will be the root View. Further View%s should then be added as children of the root View, and so on, creating a View tree.<br>
/// Views may be accessed either by their View.id (which is set on construction) or zero-based depth/Z index.<br>
/// A View reads from a model in order to update its state. See Ctrl's description for more on models.
///
typedef struct View
{
	//TODO fix this!
	char id[STRLEN_MAX]; ///< (unimplemented) ID by which a View is retrieved from its App->viewsById.
	struct Hub * hub; ///< \brief The Hub which owns and manages this View's owner App.
	struct App * app; ///< \brief The App which owns and manages this View.
	
	bool updating; ///< Should this have View_update() called on it every frame?
	bool initialised; ///< True after first initialise. If re- initialise is required, this should be manually reset to false.
	void * model; ///< The model associated with this View. May or may not be the same as this View's App's model, depending on \link Configuration \endlink.
	
	struct View * root;
	struct View * parent; ///< View's parent View, if any.
	kvec_t(struct View *) childrenByZ; ///< View's children, where index is Z-order (Z is the stacking/draw order, i.e. goes positive out of screen).
	//int childrenCount; ///< The number of child Views held by this parent View. Negative for invalid return values (e.g. on seek).
	
	float dimensions[3]; ///< Dimensions of this View in user-defined units.
	float position[3]; ///< Position of this View in user-defined units.
	float orientation[3]; ///< Orientation of this View in user-defined units.
	float scale[3]; ///< Scale of this View in user-defined units.
	
	khash_t(StrPtr) * extensionsById; ///< Extensions included on this View instance, if using config.
	kvec_t(ArcString) extensionIds; ///< Array of fixed-length cstrings used as keys to extensionsById (required once XML and its source strings are freed).
	
	void (*start)(struct View * const this); ///< \brief User-supplied callback for when this View start()s.
	void (*stop)(struct View * const this); ///< \brief User-supplied callback for when this View stop()s.
	void (*suspend)(struct View * const this); ///< \brief User-supplied callback for when this View must suspend() due to a loss of rendering context.
	void (*resume)(struct View * const this); ///< \brief User-supplied callback for when this View must resume() due to regaining rendering context.
	void (*initialise)(struct View * const this); ///< \brief User-supplied callback for when this View initialise()s.
	void (*dispose)(struct View * const this); ///< \brief User-supplied callback for when this View dispose()s of its resources.
	void (*update)(struct View * const this); ///< \brief User-supplied callback for when this View update()s, i.e. update this View before its children update.
	void (*updatePost)(struct View * const this); ///< \brief User-supplied callback for when this View updatePost()s, i.e. update this View after its children update.
	void (*onParentResize)(struct View * const this); ///< \brief User-supplied callback for when this View's parent is resized. Root View resize is handled by some external (platform-specific) callback.
	//void (*enable)(struct View * const this); //start
	//void (*disable)(struct View * const this); //stop
} View;

/// Base class for a user-defined controller that handles specific game / business / simulation logic within an App.

/// For every discrete Ctrl needed, allocate it and set the appropriate callbacks.<br>
/// A Ctrl instance may then be added to an App by setting App.ctrl; this will be the root Ctrl. Often, this will be the only Ctrl needed; though in some cases the user may prefer to use sub-%Ctrl%s attached to root.<br>
/// Sub-%Ctrl%s may be accessed through the App's root Ctrl.<br>
/// A Ctrl operates on a model. In arc, models need no specific type; they can be anything (and are internally denoted as void *). Ctrl writes new model state by reading extant model state, inputs and View state.
typedef struct Ctrl
{
	char id[STRLEN_MAX]; ///< ID used for Arc debugging mode.
	struct Hub * hub; ///< \brief The Hub which owns and manages this Ctrl's owner App.
	struct App * app; ///< \brief The App which owns and manages this Ctrl.
{'fraction_non_alphanumeric': 0.10084830896305391, 'fraction_numerical': 0.0024556312088402726, 'mean_word_length': 3.8469028942385717, 'pattern_counts': {'":': 0, '<': 99, '<?xml version=': 0, '>': 41, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '6896002', 'n_tokens_mistral': 5090, 'n_tokens_neox': 4788, 'n_words': 2467}