<commit_before>/* 
 * jcom.ui
 * External for Jamoma: provide standard user interface component for modules
 * By <NAME>, Copyright © 2007
 * 
 * License: This code is licensed under the terms of the GNU LGPL
 * http://www.gnu.org/licenses/lgpl.html 
 */

#include "jcom.ui.h"

// TODO: This file, lamely, duplicates a lot of what is in jcom.hub.internals.cpp.
//			Should create a more DRY way of doing this.


class uiInternalObject {
	public:
	t_object	*theObject;
	//method		action;
	
	uiInternalObject(char *classname, char *subscribername, char *subscribertype, char *ramptype, char *description, char *dataspace, char *nativeUnit, char *activeUnit)
	{
		t_atom		a[13];
	
		theObject = NULL;
		atom_setsym(a+0, gensym(subscribername));
		atom_setsym(a+1, gensym("@type"));
		atom_setsym(a+2, gensym(subscribertype));
		atom_setsym(a+3, gensym("@ramp/drive"));
		atom_setsym(a+4, gensym(ramptype));
		atom_setsym(a+5, gensym("@description"));
		atom_setsym(a+6, gensym(description));
		if(dataspace && nativeUnit && activeUnit){
			atom_setsym(a+7, gensym("@dataspace"));
			atom_setsym(a+8, gensym(dataspace));
			atom_setsym(a+9, gensym("@unit/native"));
			atom_setsym(a+10, gensym(nativeUnit));
			atom_setsym(a+11, gensym("@unit/active"));
			atom_setsym(a+12, gensym(activeUnit));
			jcom_core_loadextern(gensym(classname), 13, a, &theObject);
		}
		else
			jcom_core_loadextern(gensym(classname), 7, a, &theObject);
	}
	
	~uiInternalObject()
	{
		object_free(theObject);
	}
	
	void setAction(method aCallback, t_object *aCallbackArg)
	{
		object_method(theObject, gensym("setcallback"), aCallback, aCallbackArg);
	}
	
	void setName(char* newName)
	{
		object_attr_setsym(theObject, _sym_name, gensym(newName));
	}
};


void ui_internals_destroy(t_ui *obj)
{
	long				i;
	long				numKeys = 0;
	t_symbol			**keys = NULL;
	uiInternalObject	*anObject;
	t_max_err			err;
	
	hashtab_getkeys(obj->hash_internals, &numKeys, &keys);
	for(i=0; i<numKeys; i++){
		err = hashtab_lookup(obj->hash_internals, keys[i], (t_object**)&anObject);
		if(!err)
			delete anObject;
	}
	
	if(keys)
		sysmem_freeptr(keys);
		
	hashtab_chuck(obj->hash_internals);<commit_msg>don't crash if theObject is null<commit_after>/* 
 * jcom.ui
 * External for Jamoma: provide standard user interface component for modules
 * By <NAME>, Copyright © 2007
 * 
 * License: This code is licensed under the terms of the GNU LGPL
 * http://www.gnu.org/licenses/lgpl.html 
 */

#include "jcom.ui.h"

// TODO: This file, lamely, duplicates a lot of what is in jcom.hub.internals.cpp.
//			Should create a more DRY way of doing this.


class uiInternalObject {
	public:
	t_object	*theObject;
	//method		action;
	
	uiInternalObject(char *classname, char *subscribername, char *subscribertype, char *ramptype, char *description, char *dataspace, char *nativeUnit, char *activeUnit)
	{
		t_atom		a[13];
	
		theObject = NULL;
		atom_setsym(a+0, gensym(subscribername));
		atom_setsym(a+1, gensym("@type"));
		atom_setsym(a+2, gensym(subscribertype));
		atom_setsym(a+3, gensym("@ramp/drive"));
		atom_setsym(a+4, gensym(ramptype));
		atom_setsym(a+5, gensym("@description"));
		atom_setsym(a+6, gensym(description));
		if(dataspace && nativeUnit && activeUnit){
			atom_setsym(a+7, gensym("@dataspace"));
			atom_setsym(a+8, gensym(dataspace));
			atom_setsym(a+9, gensym("@unit/native"));
			atom_setsym(a+10, gensym(nativeUnit));
			atom_setsym(a+11, gensym("@unit/active"));
			atom_setsym(a+12, gensym(activeUnit));
			jcom_core_loadextern(gensym(classname), 13, a, &theObject);
		}
		else
			jcom_core_loadextern(gensym(classname), 7, a, &theObject);
	}
	
	~uiInternalObject()
	{
		if(theObject)
			object_free(theObject);
	}
	
	void setAction(method aCallback, t_object *aCallbackArg)
	{
		if(theObject)
			object_method(theObject, gensym("setcallback"), aCallback, aCallbackArg);
	}
	
	void setName(char* newName)
	{
		if(theObject)
			object_attr_setsym(theObject, _sym_name, gensym(newName));
	}
};


void ui_internals_destroy(t_ui *obj)
{
	long				i;
	long				numKeys = 0;
	t_symbol			**keys = NULL;
	uiInternalObject	*anObject;
	t_max_err			err;
	
	hashtab_getkeys(obj->hash_internals, &numKeys, &keys);
	for(i=0; i<numKeys; i++){
		err = hashtab_lookup(obj->hash_internals, keys[i], (t_object**)&anObject);
		if(!err)
			delete anObject;
	}
	
	if(keys)
		sysmem_freeptr(keys);
		
	hashtab_chuck(obj->hash_internals);
{'fraction_non_alphanumeric': 0.14458105646630237, 'fraction_numerical': 0.012295081967213115, 'mean_word_length': 5.001366120218579, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '6031956', 'n_tokens_mistral': 1884, 'n_tokens_neox': 1710, 'n_words': 339}