Jump to content
naught

Object Oriented SQF Scripting and Compiling

Recommended Posts

Hello all.
Is it possible to call methods in the context of class?. ie call static methods. For example, for the realization of the "singleton" pattern?
 
Something like:

 

_myObj = ["getInstance", _params] call MySingletonClass;
or
_myObj = _params call MySingletonClass_getInstance;

Share this post


Link to post
Share on other sites

This is a simple class to connect to the database for extDB2, but the implementation via the singleton will look easier and stable for any instance:

 

	
	#include "oop.h"

	CLASS("DatabaseExtDB2Connect")

		PRIVATE STATIC_VARIABLE("string", "connectID");

		PUBLIC FUNCTION("array", "constructor") {
			
			private ["_version", "_database", "_protocol", "_protocolOptions"];
			
			_database =        [_this, 0, "", [""]] call BIS_fnc_param;
			_protocol =        [_this, 1, "", [""]] call BIS_fnc_param;
			_protocolOptions = [_this, 2, "", [""]] call BIS_fnc_param;

			if (isNil {uiNamespace getVariable "extDB_SQL_CUSTOM_ID"}) then {
				
				_version = MEMBER("getVersion", nil);
				if(_version != "") then {
					MEMBER("sendLog", "Version: " + _version);
					
					//Add Database
					if(MEMBER("addDatabase", _database)) then {
						
						//Load protocol
						private ["_protocolParams"];
						_protocolParams = [_database, _protocol, str(round(random(999999))), _protocolOptions];
						
						if(MEMBER("loadDatabaseProtocol", _protocolParams)) then {
						
							MEMBER("connectID", _protocolParams select 2);
							uiNamespace setVariable ["extDB_SQL_CUSTOM_ID", _protocolParams select 2];
							
							MEMBER("lock", nil);							
						};
					};
				}else{
					MEMBER("sendError", "Failed to Load");
				};
			}else{
				MEMBER("connectID", uiNamespace getVariable "extDB_SQL_CUSTOM_ID");
				MEMBER("sendLog", "Already connected");
			};
		};
		
		PUBLIC FUNCTION("string", "deconstructor") {
			DELETE_VARIABLE("connectID");
		};
		
		PUBLIC FUNCTION("", "getConnectID") FUNC_GETVAR("connectID");
				
		PRIVATE FUNCTION("string", "addDatabase") {
			private ["_return", "_result"];
			
			_result = call compile ("extDB2" callExtension format["9:ADD_DATABASE:%1", _this]);
			_return = false;
			
			if ((_result select 0) isEqualTo 1) then {
				MEMBER("sendLog", "Database added: " + _this);
				_return = true;
			}else{
				MEMBER("sendError", _result select 1);
			};
			
		_return
		};
		
		PRIVATE FUNCTION("array", "loadDatabaseProtocol") {
			_this params ["_database", "_protocol", "_id", "_protocolOptions"];
			private ["_return", "_result"];
			
			_return = false;
			_result = call compile ("extDB2" callExtension format["9:ADD_DATABASE_PROTOCOL:%1:%2:%3:%4", 
				_database,
				_protocol,
				_id,
				_protocolOptions
			]);
			
			if ((_result select 0) isEqualTo 1) then {
				MEMBER("sendLog", "Protocol loaded - " + _protocol);
				_return = true;
			}else{
				MEMBER("sendError", _result select 1);
			};
			
		_return
		};
		
		PRIVATE FUNCTION("", "getVersion") {
			"extDB2" callExtension "9:VERSION";
		};
		
		PRIVATE FUNCTION("", "lock") {
			private ["_return", "_result"];
			
			_result = call compile ("extDB2" callExtension "9:LOCK");
			_return = false;
			
			if ((_result select 0) isEqualTo 1) then {
				_return = true;
				MEMBER("sendLog", "Locked");
			};
			
		_return
		};
		
		PRIVATE FUNCTION("string", "sendLog") {
			diag_log (format ["extDB2 log: %1", _this]);
		};
		
		PRIVATE FUNCTION("string", "sendError") {
			private ["_error"];
			_error = format["extDB2 Error: %1", _this];
			_error call BIS_fnc_error;
			diag_log _error;
		};
		
	ENDCLASS;

Code:

 

_databaseConn = ["new", ["MySQL_test_database_1", "SQL_CUSTOM_V2", "/"]] call DatabaseExtDB2Connect;

Share this post


Link to post
Share on other sites

 

Hello all.

Is it possible to call methods in the context of class?. ie call static methods. For example, for the realization of the "singleton" pattern?

 

Something like:

 

_myObj = ["getInstance", _params] call MySingletonClass;
or
_myObj = _params call MySingletonClass_getInstance;

 

Hi Aloe :)

 

Happy to see you again ! sadly, currently static method other than new / delete are not supported.

 

i will look to see if we can add the support

 

for extdb2 connector, nice work

 

i also work currently on the same project cause i need a driver to manage extdb2. You seems to want to use several class. is there a reason ?

 

if you are interested by we perhaps could work together on this projet (through gitbhub) to strkie the goal  :)

Share this post


Link to post
Share on other sites
Hi Code34  :).
No, this is not entirely a conscious class name) That should be one class, responsibility is - provide a convenient interface to the database (connection, read, write, etc.).
I am a pioneer in working with git, but I will be glad to cooperation of our efforts towards a common goal and help the community. Give please me your contact-mail or vk.com or skype or jabber :icon_smile:.

Share this post


Link to post
Share on other sites

you can retrieve oop.h (temporary version) on this url :

https://github.com/code34/combat_assault.altis/blob/1.35/warcontext/objects/oop.h

 

i add a _self local variable to retrieve the pointer of the object itself

 

you can use it like this

PUBLIC FUNCTION("", "getThis") { _self; };
MEMBER("getThis", nil);

or directly using the _self variable in the code of object as it a reserved variable :)

Share this post


Link to post
Share on other sites
On 26-9-2017 at 10:14 PM, code34 said:

you can retrieve oop.h (temporary version) on this url :

https://github.com/code34/combat_assault.altis/blob/1.35/warcontext/objects/oop.h

 

i add a _self local variable to retrieve the pointer of the object itself

 

you can use it like this


PUBLIC FUNCTION("", "getThis") { _self; };

MEMBER("getThis", nil);

or directly using the _self variable in the code of object as it a reserved variable :)

 

How would i go about adding a class object to an array ?

If it can not be done, i can add the item ID's to the list but i'm not sure how i would get the "item details" (ex: name) from the ID...
 

I have this 'Item' class:

#include "..\..\lib\OOP\oop.h"

CLASS("Item")

PRIVATE STATIC_VARIABLE("scalar","lastItemID");
PRIVATE VARIABLE("scalar", "id");
PRIVATE VARIABLE("string", "name");

PUBLIC FUNCTION("string", "constructor") { // String: ItemName
	private ["_id"];
	MEMBER("name", _this);

	_id = MEMBER("lastItemID", nil);
	if (isNil "_id") then {_id = 0};
	MEMBER("id", _id);

	INC_VAR("lastItemID");
};
PUBLIC FUNCTION("nil", "deconstructor"){
	DELETE_VARIABLE("id");
	DELETE_VARIABLE("name");
};
PUBLIC FUNCTION("", "getItemId") FUNC_GETVAR("id");
PUBLIC FUNCTION("", "getItemName") FUNC_GETVAR("name");

ENDCLASS;

 

And this 'ItemList' class:

#include "..\..\lib\OOP\oop.h"

CLASS("ItemList")

PRIVATE VARIABLE("array", "items");

PUBLIC FUNCTION("", "constructor") {
	MEMBER("items", []);
};
PUBLIC FUNCTION("nil", "deconstructor"){
	DELETE_VARIABLE("items");
};
PUBLIC FUNCTION("object", "addItem"){ // Object: Item
	private ["_items", "_index"];
	_items = MEMBER("items", nil);
	_index = count _items;
	_items set [_index, _this];
};
PUBLIC FUNCTION("", "getItems") FUNC_GETVAR("items");

ENDCLASS;

 

Share this post


Link to post
Share on other sites

hi

 

Just a simple modification, the type of oop object is code not object :)

 

#include "..\..\lib\OOP\oop.h"

CLASS("ItemList")

PRIVATE VARIABLE("array", "items");

PUBLIC FUNCTION("", "constructor") {
	MEMBER("items", []);
};
PUBLIC FUNCTION("nil", "deconstructor"){
	DELETE_VARIABLE("items");
};
PUBLIC FUNCTION("code", "addItem"){ // Object: Item
MEMBER("items", nil) pushBack _this;
};
PUBLIC FUNCTION("", "getItems") FUNC_GETVAR("items");

ENDCLASS;

 

Share this post


Link to post
Share on other sites

 You should now use this forum thread with the new OOP version :) cause this one is no longer maintain since few years

 

 

  • Thanks 1

Share this post


Link to post
Share on other sites
On 9-1-2018 at 7:05 PM, code34 said:

hi

 

Just a simple modification, the type of oop object is code not object :)

 


#include "..\..\lib\OOP\oop.h"

CLASS("ItemList")

PRIVATE VARIABLE("array", "items");

PUBLIC FUNCTION("", "constructor") {
	MEMBER("items", []);
};
PUBLIC FUNCTION("nil", "deconstructor"){
	DELETE_VARIABLE("items");
};
PUBLIC FUNCTION("code", "addItem"){ // Object: Item
MEMBER("items", nil) pushBack _this;
};
PUBLIC FUNCTION("", "getItems") FUNC_GETVAR("items");

ENDCLASS;

 

On 9-1-2018 at 7:11 PM, code34 said:

 You should now use this forum thread with the new OOP version :) cause this one is no longer maintain since few years

 

 

 

Thanks alot!
I'll try this later..

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

×