Lite Foundation - A powerful Lite-C library

Posted By: WretchedSid

Lite Foundation - A powerful Lite-C library - 04/19/11 01:27

Sooo, again, my project Lite Foundation... But this time, its completely rewritten from scratch to be way more easy to use and better extendable. The single parts that make up the whole packet are better separated and everything should be much more accessible.
Long story short; Everything is better than ever, although incompatible with the older versions... sorry about that.

If you are new to Lite Foundation, let me give you a short overview:
Lite Foundation is written in C with the purpose to extend C with the concept of classes, delegated protocols and all this fancy object oriented stuff, all in a way that allows it to easily create class instances and classes. It also introduces a reference counting environment on top of the classes system, this means that you don't have to bother with the "when to free what" question anymore!
Every class instance has a reference counter, the so called retain count. You can retain and release instances. Retaining is like telling Lite Foundation "Hey, I want this object, please don't free it", it will increment the retain count by one. Releasing is like telling "Alright, thanks for holding this object in memory, but you can free it now!", it will decrement the counter by one.
If the retain count of an object hits zero, Lite Foundation will take care of freeing its memory so you can work headache free with memory allocations!

Of course, just having this is doesn't help much as one had to implement every class that one is interested in, thus Lite Foundation also has a standard library with common used classes that you can use right away in any project.


Now remark: This is just a "beta" release so that you can play with it and give me feedback, only a fraction of the standard library is currently implemented as it was not my main focus for this release. But there will come a lot more in the future! Also; The runtime code might be subject to change, I'll try my best to not break anything.



Currently implemented is a very powerful string class (way more powerful than Lite-C STRING object), a scanner class that can be used to parse/scan through strings, a autorelease pool class and a character set class (which is used by strings and scanners).
This is imo enough to give you a taste of what is possible and what might come in the future!

The current release also contains a getting started guide that introduces you to everything you need to know about Lite Foundation and a runtime documentation which helps you to understand how Lite Foundation works internally and how to extend it.
There are also two examples on how to use strings and scanners plus a reference that documents every function, struct and type that comes with Lite Foundation.

As always, everything is released under the MIT license!
You can grab your copy here (always the latest version)
Or just read the reference, or the Getting Started guide, or the Runtime Guide.

If you have any questions, please aks! I'm not good at explaining and Lite Foundation is probably a bit more complicated than the normal Gamestudio stuff. I tried my best to make the beginning as easy as possible, but if I forgot to cover a part that you don't understand: Ask!
I'm open for C&C, feature requests, enhancement requests etc.
Posted By: Hummel

Re: Lite Foundation - A powerful Lite-C library - 04/20/11 14:22

People who always cried for classes support in Lite-C should have a wild party in this thread, so I wonder why it seems that nobody really cares...

However, Sid invested a lot of his precious time for an implementation of what people (should have) missed in Lite-C when working on big projects.
So, at least, every serious GS user who plans to develop more than a simple casual game should support Sid´s work most urgently.
Posted By: ratchet

Re: Lite Foundation - A powerful Lite-C library - 04/20/11 14:51

I think i will make in bad mood people , but here is my
personnal taste !
The Class at final only represents something containing Methods and Variables ! If you don't use inheritence you won't win a lot !
If you use "include files (.h)" that contains also variables, methods, it can be similar in some ways to a Class !

--------

I don't see interest of making Class system in a script language !
The script is meaned to go fast and simple and easy for newbbies or 3D artist, that i think don't wan't to understand
all Classes and all Object Oriented Stuff laugh !


It's the way some engine works like Unity offering 2 solutions :
- no Classes with script language
For newbbies, 3D artists , people wanting to go fast
- Classes with C#
for programmers, advanced people etc ...

So won't it be more simple to use C# directly with A8 no ?

It's only my personnal taste !

I stop here, i don't say it's a bad idea at all !
Perhaps some people will find it usefull indeed, perhaps it's
the next evolution of Lite C laugh ??



Posted By: WretchedSid

Re: Lite Foundation - A powerful Lite-C library - 04/20/11 15:06

Originally Posted By: ratchet
The Class at final only represents something containing Methods and Variables ! If you don't use inheritence you won't win a lot !

Well, class might be the wrong name, but give me a better one. Its certainly not just a struct and not just a lot of functions but something that combines both.

About the inheritance, you can do this in Lite Foundation too, you can even build class clusters that use a proxy object to present a standard interface to work with the objects, you can also implement classes that can turn into faults and such things.
But I see that there is no way for class to name its superclass, I will add such a thing into the next runtime update so that classes can pose as other classes.

EDIT: Actually, you WIN a lot even without inheritance, you get the whole reference counting environment and lots of other nice things for free. Eg. you can ask _ANY_ Lite Foundation object for its hash or if its equal to something without inheriting from anything at all.

Originally Posted By: ratchet
If you use "include files (.h)" that contains also variables, methods, it can be similar in some ways to a Class !

Certainly no, thats not a OOP approach as you can't create instances of the .h file or so, so you have just the one instance and thats it.


Originally Posted By: ratchet
I don't see interest of making Class system in a script language !
The script is meaned to go fast and simple and easy for newbbies or 3D artist, that i think don't wan't to understand
all Classes and all Object Oriented Stuff laugh !

Luckily, Lite-C can be compiled into directly executable binaries, so why compare it with a scripting language? If you want to gain more power on a high level way, you can use Lite Foundations runtime and leave the whole standard library out, its certainly simpler than doing all this stuff by yourself, especially as a 3D artist or newbie.

EDIT; If you don't want to code a line as 3D artist thats just fine but then you either have a programmer that will do this for you or you don't use Gamestudio.


Originally Posted By: ratchet
So won't it be more simple to use C# directly with A8 no ?

Sure, people who like C# might get the better deal by using it. But although the name Lite Foundation implies that its only for Lite-C, its certainly not developed for Lite-C (just look into the scripts and the whole #ifdef's that are inserted just to support Lite-C). However, it is developed to serve as a foundation for my Kernel project which is just C and I hate C# so its the better tradeoff for me. And as I'm a nice person, I took the time to port it so it can also be compiled with Lite-C

Originally Posted By: ratchet
perhaps it's
the next evolution of Lite C laugh ??

No, its for sure not a evolution of the language as it doesn't extend the language or changes the compiler or anything like this. Its just a bit of code!
Posted By: lostclimate

Re: Lite Foundation - A powerful Lite-C library - 04/20/11 21:33

excited to try it
Posted By: WretchedSid

Re: Lite Foundation - A powerful Lite-C library - 04/21/11 00:31

Great laugh
Please give me feedback what you think needs to be handled better, because there are certainly a few rough edges but without feedback I won't be able to clean them and the problem with rough edges in API is that it gets deprecated pretty soon which is really not what I want in a library that has "foundation" in its title tongue
Posted By: Schubido

Re: Lite Foundation - A powerful Lite-C library - 04/21/11 07:40

Hi Sid,

having a short look at this - without trying it out - two statements come to my mind.

a)
Great work, very impressive.
b)
I didn't get the purpose of this concept.

E.g. OOP makes live easy because a developer does not need to deal with the internals of a class. Is this also true for Lite Foundation?

E.g. inheritance - how would this example (pseudo code) look in Lite Foundation?
Code:
class samevalue
{
  public function calculate (int v)
  {
    return v;
  }
}

class incrementvalue inherits samevalue
{
  public function calculate (int v)
  {
    return parent.calculate(v)+1;
  }
}

...

samevalue a,b;

a=new samevalue;
b=new incrementvalue;
print (a.calculate(1));  // -> 1
print (b.calculate(1));  // -> 2



My first impression is that this is more a container (how I would call it) concept then OOP.
I think it would help to better get the purpose of this library if you could give some easy to understand examples which show for which problems this is an efficient solution.
So to repeat my first statements with different words:
Looks quite impressive, but as long as I don't see the benefit I would not invest much time to really understand the concept.
Posted By: WretchedSid

Re: Lite Foundation - A powerful Lite-C library - 04/21/11 08:50

Hi Schubido,

about your first question, yes, one does not have to deal with the internals of a class. In many cases its even dangerous because chances are that a classes internal layout changes in the future. Eg. the string class (LFString.c and LFString.h) will be in the future changed to support UTF-8 and some other encodings, depending on the layout of the class will definitely break code in the future. However, just working with the methods won't break as they are maintained by the same person who also maintains the struct around the object.

Your second example is a bit more complex to answer as Lite Foundation gives you three ways to do this particular example;
First of all there is some boilerplate code to create the two classes:
Code:
struct __samevalue
{
    LFRuntimeBase base; // The Lite Foundation object used to store infos about the object
}

struct __incrementvalue
{
    struct __samevalue base; // This time, base is the superclass
}

typedef struct __samevalue* samevalue;
typedef struct __samevalue* incrementvalue;


// In some function that is meant to be invoked by someone outside
LFTypeID __samevalueID = LFRuntimeRegisterClass((const LFRuntimeClass *)&__samevalueClass); // __samevalueClass is assumed to be a filled out LFRuntimeClass object
LFTypeID __incrementvalueID = LFRuntimeRegisterClass((const LFRuntimeClass *)&__incrementvalueClass); // __incrementvalueClass is assumed to be a filled out LFRuntimeClass object



So, now about how to implement the functionality, the first option is to use Lite-C's function overloading and breaking the code on all C compilers.
The second option is to use one function that accepts the polymorphic LFTypeRef and then does its work depending on what object it works with:
Code:
int calculate(LFTypeRef ref, int v)
{
    if(LFIsTypeOf(ref, __samevalueID)
        return v;
        
    if(LFIsTypeOf(ref, __incrementvalueID)
        return v + 1;
        
    return 0;
}



Not the best way either, but, you can still fallback to protocols. A protocol is just a definition of a function that must be implemented by classes that implement the protocol. The calculate function would then become a wrapper to a runtime call that then dispatches the classes protocol information and picks the right function which is then called:
Code:
// In some function that is meant to be invoked by someone outside
LFProtocolBody *pbody = LFRuntimeCreateProtocolBody(sharedProtocolID;
LFRuntimeProtocolBodySetIMP(pbody, __calculateSelector, LFProtocolMethod21(__samevlueCalcImp)); // Say the runtime that its a function with 1 argument and a return value

LFTypeID __samevalueID = LFRuntimeRegisterClass((const LFRuntimeClass *)&__samevalueClass);

// ----------

LFProtocolBody *pbody = LFRuntimeCreateProtocolBody(sharedProtocolID);
LFRuntimeProtocolBodySetIMP(pbody, __calculateSelector, LFProtocolMethod21(__incrementvalueCalcImp));

LFTypeID __incrementvalueID = LFRuntimeRegisterClass((const LFRuntimeClass *)&__incrementvalueClass); // __incrementvalueClass is assumed to be a filled out LFRuntimeClass object

// ----------

int __samevlueCalcImp(LFTypeRef ref, int v)
{
    return v;
}

int __incrementvalueCalcImp(LFTypeRef ref, int v)
{
    return __samevlueCalcImp(ref) + 1;
}

// --------

int calculate(LFTypeRef ref, int v)
{
    return (int)LFRuntimeRInvoke1(ref, __calculateSelector, (LFTypeRef)v);
}




There is a little more code needed to create the actual protocol, but that should give you an idea on how to do it. This is for sure not as elegant as a C++ or Java class but mostly because of limitations in the language which I sadly just can't fix.


Quote:
My first impression is that this is more a container (how I would call it) concept then OOP.
I think it would help to better get the purpose of this library if you could give some easy to understand examples which show for which problems this is an efficient solution.

Well, first of all; You can do real OOP with Lite Foundation!
I agree with the example problem, but I can't think of any easy to understand examples, for example I could write a example that shows how a class can implement faulting to help keeping the memory footprint low, however, this would assume that everyone understands the concept of faulting.
The same goes with eg. a example for class clusters, they are meant to float as fragments in the code, so it becomes more complicated to explain which part is what and how it works with the rest.

I wrote examples for the standard library that also ships with Lite Foundation for people who don't want to use the runtime directly, this is where the purpose of the library changes completely again. And as more classes get added to the default library, there will come more examples on how to use them (beside that the naming convention should make this really clear).

The runtime guide covers this part (how to extend it, how to work with protocols etc), but it contains not so much example code that can be directly executed.
Posted By: WretchedSid

Re: Lite Foundation - A powerful Lite-C library - 04/21/11 12:54

Okay, Schubido's post inspired me to work on a better inheritance system and I'm pleased to announce that Lite Foundation has now a real single inheritance system laugh

Classes can define a super class in their description and the runtime will try to fallback to the superclass wherever possible. For example, asking if an object is kind of a certain class (LFIsTypeOf()) will now return true even if the object just inherited from the object or an object that inherited from it (or deeper).
Same goes for things like LFHash() and something useful if you want to build subclasses; The dealloc and init callbacks are now called for every class in the inheritance chain.
This means that a subclass doesn't need to initialize its superclasses member but just its own new, likewise, on de-allocation, the class only needs to worry about its own allocations.

Also; All protocol functions will now be called on the super class if the objects class doesn't implement the protocol or you can call the superclasses implementation directly via a few new functions.


Another nice thing for people who don't care about the runtime and this nasty stuff: I'm currently working on a set class that can store objects in unsorted order, once I've done this, I will make a pretty cool and even useful demo to show what a few lines with Lite Foundation can do. So stay tuned laugh
Posted By: Quad

Re: Lite Foundation - A powerful Lite-C library - 04/21/11 13:32

staying tuned.
Posted By: Uhrwerk

Re: Lite Foundation - A powerful Lite-C library - 04/21/11 15:31

Ok, JustSid, so here is another heretic question. From a technical point of view I think your work is very interesting. On the other hand I don't get your concept of using this. Very roughly speaking you bind functions to structs so they more or less appear as methods. That goes into an oop direction. However, you do not provide inheritance (at least not without a workaround), no polymorphy and no encapsulation. That makes a score of 0.5 out of 3 on the oop properties scale... tongue

So the question is: why should I use this complicated stuff instead of just using the c++ sdk?
Posted By: WretchedSid

Re: Lite Foundation - A powerful Lite-C library - 04/21/11 15:53

Originally Posted By: Uhrwerk
On the other hand I don't get your concept of using this. Very roughly speaking you bind functions to structs so they more or less appear as methods. That goes into an oop direction. However, you do not provide inheritance (at least not without a workaround), no polymorphy and no encapsulation.

I don't see why adding a line of code is a workaround to create inheritance, I mean, even in Java and C++ you have to provide the name of the class you want to subclass. So I don't see why inheritance is a huge problem (including that I just updated the runtime) but maybe I miss a point?
About polymorphism, like I already mentioned in my answer to Schubido, it IS possible and done through protocols. I see that its not the most convenient way and I'm open for any suggestions about this because I find this part a bit dirty too, but it works and since the last update it also works nicely with inheritance.

You are right about encapsulation, however, you can still move your struct into the .c file and thus encapsulate the structs members from the user (plus if you are really crazy you can go ahead and build a public struct that has the same size but not all members but padding stuff in between).
This is where it hits the language limitations really bad and I can't offer any solution for this, but like in C++: Don't use classes as containers can be used as rule of thumb for Lite Foundation classes too and it works just fine.

Quote:
So the question is: why should I use this complicated stuff instead of just using the c++ sdk?

Now that is a good question and the answer is the same as I gave ratchet for his C# question; If you can work with C++, work with it!
For me thats not an option, I hate C++, I hate the memory model of C++ etc. This is the same with any other wrapper for language xyz; If you like the language and can work with it, do it!

However, if you are not sure if you like C++ and are used to C, you can give Lite Foundation a try and might fall in love with its reference counting environment and this other "complicated" stuff laugh
Posted By: Pappenheimer

Re: Lite Foundation - A powerful Lite-C library - 04/23/11 01:00

I'm following your project and the discussions about it with big interest.

I found a grafik about certain 'styles' of programming:

Unfortunately the english wikipedia article http://en.wikipedia.org/wiki/Component-based_software_engineering doesn't use the same classification in four levels.
Question:
Where would you place Lite-C and where Lite-C Foundation?
Or doesn't the scheme of the grafik make any sense?
Posted By: lostclimate

Re: Lite Foundation - A powerful Lite-C library - 04/23/11 04:52

*wishes i could read the german in the graphic* frown
Posted By: Germanunkol

Re: Lite Foundation - A powerful Lite-C library - 04/23/11 07:34

Lostclimate, is that a joke? grin
I can try to translate the pic for you:
Abstraktion -> Abstraction
Komponentenbasierte Programmierung -> component-based software engineering
Distributed Object Computing -> Distributed Object Computing
Objektorientierte Programmierung -> Object-Oriented Programming
Procedurale Programmierung -> Procedural Programming
z.B. -> for example
CCM -> CCM
CORBA -> CORBA
C++ -> C++
C -> C

Maybe now you know why I asked you whether or not it was a joke... tongue

Justsid, I like the idea of having classes in LiteC a lot. But I won't get around to trying them out any time soon. It doesn't make sense to implement them in my game because it's progressed too far, and I want to switch to C++ when this game is finished. So currently I don't have the need for such structures, but the approach is very interesting.
Posted By: WretchedSid

Re: Lite Foundation - A powerful Lite-C library - 04/23/11 21:39

Originally Posted By: Pappenheimer
Question:
Where would you place Lite-C and where Lite-C Foundation?

So first of all, its Lite Foundation and not Lite-C Foundation as its not an exclusive Lite-C project wink [/wise guy]

To answer the question, Lite-C as being a subset of C89 is placed in the "Procedural programming". Lite Foundation tries to bring C and thus also Lite-C to the next "level", the "object oriented programming".
This is the work made by the Lite Foundation runtime as it allows you to define classes and create instances so you have objects that you can work with, rather than just functions and a global set of variables.

However, if you count the whole acknex foobar to Lite-C, then Lite-C is half procedural and half object oriented. The whole ENTITY stuff and such with its functions like ent_create() ent_do_something() etc is completely object oriented while the normal program flow is usual procedural.


Its btw time for a new update, this time its just the default library but I think its pretty awesome as it brings two new objects and proves Uhrwerks "no polymorphism" wrong.

The first new object is the LFSetRef, a basic hash table implementation that works based on prime numbers and such. As with every hash table, its pretty hard to iterate through it and at this place the second new objects kicks in; LFEnumeratorRef.
The LFEnumeratorRef is a wrapper class to allow enumerating of _every_ collection class, read: When I add an array class, it can be enumerated in the very same way! Likewise, if you create an array class, it also works with your arrays the very same way.

Now some of you will say "this is garbage, its impossible unless you restrict the way collection classes are build" and I can tell you "its true and you are free to build you collection class in whatever way you want".
Like I said, the LFEnumeratorRef class is a wrapper, it wraps the LFEnumerator protocol. So for every class that wants to be enumerated using a LFEnumeratorRef object, the class must implement the protocol and one simple function: "give me the next n objects".

Here is a short example to show how easy the LFEnumeratorRef is:
Code:
LFEnumeratorRef enumerator = LFEnumeratorCreate(someObject);
LFTypeRef object;
 while((object = LFEnumeratorNextObject(enumerator)))
 {
 	// Do something with the object
 }




The release also contains two bug fixes; The LFString class used a local variable called "result" in the hash and equal functions, the Lite-C compiler never said anything about this and compiled the code just fine, but the result of the functions were pretty random.
It took me a while to figure out why my LFSetRef test scripts never worked in Lite-C (they make heavy use of both functions).


The download link is still the same; http://www.widerwille.com/litefoundation/litefoundation.zip


Time to work on the promised demo, it will sadly require A7/A8 pro or ANet, but I guess most users here have both of it? Oh and no, its not a multiplayer demo.
EDIT: Ah darn, looks like I first need to write a class that allows HTTP requests other than just stupid HTTP POST... Oh well...
In the meantime; any heretic questions left? tongue


EDIT: I made a horrible mistake while copy'n'pasting some of the LFSetRef's code around to get it working with the Lite-C compiler, calling LFCopy() with an LFSetRef as receiver will fail in a way that the buckets aren't copied! This will certainly crash the program when you the function so please; Redownload Lite Foundation!
Posted By: tzw

Re: Lite Foundation - A powerful Lite-C library - 04/23/11 23:49

i've tried it . very useful lib.thx
Posted By: WretchedSid

Re: Lite Foundation - A powerful Lite-C library - 04/27/11 13:15

You are welcome laugh


Btw, the next version is ready, it includes the new LFArray class which is, surprise, a class that maintains a ordered collection of objects.
The problem with arrays is; They are slow! Random deletion and insertion has, in the worst case, the effect that the functions needs to iterate through the whole array multiple times. It also needs to check its content for double entries and such.

I have two screenshots from my profiler to demonstrate the difference between the LFArray class and the LFSet class:

Inserting and deleting 10024 random items in and from a LFArray takes around 1970 ms!


Inserting and deleting the same amount of items from a LFSet takes just 13 ms!

Those numbers are from a very old iMac with a 2Ghz CPU, the test binary was built as x86 Intel binary with no compiler optimizations.


You can download the new version using the links from the previous posts.
Posted By: tzw

Re: Lite Foundation - A powerful Lite-C library - 04/27/11 14:38

hi justsid. is the lib support "this"? if yes , how to emulate this feature? sorry for my eng
Posted By: WretchedSid

Re: Lite Foundation - A powerful Lite-C library - 04/27/11 17:32

No, there is no workaround for 'this'.
In my implementations, the first argument is used as the receiver and for the sake fo simplicity I would suggest that others adopt this convention too.
Posted By: tzw

Re: Lite Foundation - A powerful Lite-C library - 04/28/11 04:05

i think it can be emulated by using marcos.
i compile it fail in lite c. but in c99--gcc, everything is ok
Posted By: WretchedSid

Re: Lite Foundation - A powerful Lite-C library - 04/28/11 16:28

Mhh, no, I don't see how macros could help. I mean, if I define a macro like
Code:
#define someMacro(this, argument1, argument2)


it would still look like a function call. Or am I missing something?

About the compiling problem; Yep, I probably forgot to define the target environment as Lite-C before uploading the version, you can change this in the LFBase.h file.


Anyway, a new version is out and I would recommend the update. First of all: It fixes two bugs with the string comparison:
1) LFStringCompare() searched only the whole string -1 character
2) The functions would compare a lowercase letter greater than a uppercase letter. This means that "BBBBB" compared to "aaaaa" would be smaller, while "aaaaa" should actually smaller than "BBBBBB".

The next thing is also about string comparison: Numerical comparison. This means that "Charles 1" is smaller than "Charles 2" and greater than "Charles 0". This only works with unsigned integers but its pretty neat.

Another thing is for comparison in general: The LFComparisonResult type could not hold kLFCompareLessThan because Lite-C doesn't respect "signed char".

The really big new thing: Arrays can be sorted.
Okay, array sorting is not that fancy, everyone can write a simple bubble sort. But because bubble sort is the most slowest sorting algorithm on earth, I decided to take some time and implement a sorting algorithm that sorts the array in O(N log N) worst case and normally in O(log N).
The used algorithm is a introsearch algorithm, the function will first try to sort the array with quicksort and if it ends in a degenerate case (it checks the recursion depth), the function will switch to heapsort which is guaranteed to sort the array in O(N log N). Actually its pretty hard to end in the heapsort, in 99,9999% of all cases the quicksort algorithm will do its job.
However, as recursion is expensive, the quicksort implementation will not sort the whole array but only up to a certain threshold (which is subarrays must be larger than 10).
The rest is done via an insertion sort which is way faster for almost sorted arrays than quicksort.

I have updated the Arrays.c example to show how easy it is to sort an array with 63 strings in it (populating the array takes way more space in the demo).
As always, the old links still work. And: I would love to hear your feedback laugh
Posted By: tzw

Re: Lite Foundation - A powerful Lite-C library - 04/29/11 05:08

i find a solution. use a global ptr . use a marcro to invoke obj's method.
like this:
void * this_g= NULL;
#define tzw_invoke(obj,meth) this_g = obj; obj-> meth

and in called func,we should make sure the"this"'type is what.
place the following command in the top of function 's body;
#define set_this(tzw_type) void * this =(tzw_type *) this_g

example:
set the struct type is "chinese", it's obj is tangziwen(my name~~),one of the methods is say_hello
like this:
void say_hello(var num)
{
set_this(chinese);
......
printf("ni-hao!");
this.stuff=num;
......
}
int main(void)
{
tzw_invoke(tangziwen,say_hello)(5);
........
return 0;
}

well,the syntax is a bit ugly. but work fine.
Posted By: tzw

Re: Lite Foundation - A powerful Lite-C library - 04/29/11 05:29

in fact , i make a group of codes to emulate oop (dis/constructor,interface,namespace,etc.).but i can't upload it to internet because my country 'boss --CCP forbid a lot of foreign websites,even youtube.....
Posted By: WretchedSid

Re: Lite Foundation - A powerful Lite-C library - 04/29/11 06:56

Ah, I see what you meant. Well, in this case, Lite Foundation offers you something like "this".
Here would be the example using LF, assuming that there is a class named foo:

Code:
void sayHello(foo *this, int num)
{
   printf("Hello world");
   this->num = num;
}

foo *bar = createFooInstance();
sayHello(bar, 10);



It works basically the same way, but without the need of a invoke function. However, LF also has a invoke function which is used in cases where polymorphism is needed (protocols), but the functions also pass the receiving object ("this") as the first parameter to functions.
Posted By: tzw

Re: Lite Foundation - A powerful Lite-C library - 04/29/11 10:13

yep, but make sure clients pass this argument may be very hard.and they don't like this way... BTW,nice lib.
Posted By: WretchedSid

Re: Lite Foundation - A powerful Lite-C library - 05/06/11 22:30

Okay, before I can issue the next update, I need to wait for JCL to implement setjmp() and longjmp() which then allow me to introduce three new classes exclusively for the GStudio version with setjmp.h support (don't worry, there will be a fallback for older versions).
Along with these three new classes, there are a few other new classes that come with the next update and that I hope you find as awesome as I do:

(Need setjmp.h)
LFException (well, exceptions... not much to say)
LFUnitTest (a class that implements one unit test)
LFUnitTestSuite (a class that collects multiple unit tests into one test suite)

(Available for every Lite-C user)
LFData (a class that can hold and alter binary data)
LFDictionary (a hash table like LFSet but which allows you to set your own keys for the values)
LFArchiver (a class that can serialize almost any other object in machine independent binary data)
LFUnarchiver (the reverse class to LFArchiver)
LFStorage (a balanced tree class which can save a large number of binary data effectively (is used as backend for the LFArchiver and LFUnarchiver class))
LFNumber (a class which can hold the primitive C types which also provides comparison and conversion etc)


The classes are so far done, although I'm not completely happy with some of the functions and the documentation for the new classes is almost non-existent. However, I guess there will be a lot of time until the update comes out which introduces the setjmp.h header so if you want to see a special class in the next update: Ask now for it! But keep in mind that Lite Foundation is meant to be a general purpose library, so specific things like an ENTITY wrapper won't make it into the library.
Posted By: WretchedSid

Re: Lite Foundation - A powerful Lite-C library - 05/12/11 14:26

Looks like my webhoster has some fuckup so my website is down for a few days. While its down, you can download the latest version here:
http://cl.ly/6ghG

There is one little change; The array demo closed itself in the actual release which wasn't intended, the array demo now shows the sorted array in the engines window.
Posted By: Rackscha

Re: Lite Foundation - A powerful Lite-C library - 05/15/11 00:31

Quote:
foo *bar = createFooInstance();
sayHello(bar, 10);


Thats how my simple GUI system works^^(other naming convention, but the same way).

for example if i have a struct called TFrame (the T is from Type used in Delphi....)

'Methods' of it are called like
TFrame_Free
TFrame_Create
TFrame_Paint

etc
(First parameter is always the instance)


Greets
Rackscha
Posted By: WretchedSid

Re: Lite Foundation - A powerful Lite-C library - 05/15/11 14:25

Yes, thats the common way for C functions that do something more object oriented. Gamestudio has the very same convention.

Underscore or camel case is just a matter of what one likes more, I for one prefer camel case but everyone should use whatever they like more.
Its the same with syntax styles, some people like K&R more, I like the Allman style more and some people even prefer the GNU style. As long as the code is consistent, there is nothing wrong with the styles.
Posted By: Rackscha

Re: Lite Foundation - A powerful Lite-C library - 05/15/11 14:39

Well, my System here is currently a Mix of Both conventions. "Class"-Name as prefix(Camelcase), underscore, Methodname(CamelCase).

Like:

TBigWindow_DrawSomething(MyWindow);

Thought about this to keep a visual gap between the Structname and methodname.
Havent tried full Camelcase for this approach yet(no underscore).


Greets
Rackscha
Posted By: WretchedSid

Re: Lite Foundation - A powerful Lite-C library - 05/15/11 14:45

I would go with one way, either completely camel case or just underscores because its easier to learn as a beginner. Plus: I would throw out the T and replace it with a two or three letter abbreviation to avoid function name collisions and as a replacement for namespaces.
But thats just my opinion.
Posted By: Rackscha

Re: Lite Foundation - A powerful Lite-C library - 05/15/11 15:18

Normally, CamelCase is what i prefer. Iam using it at work in Delphi(for 10Month now).

When i started to do some OOP like things in LiteC, i wanted a Name for a method that States what it does, and which class it is part of. Thats why i started to use the ClassName_MethodName approach.

I have been a bit scared about something like:

TMyClassThatDoesSomeFancyShit() as a method name. You have to look twice to see which class its part of. So i went TMyClass_DoSomeFancyShit();
In Java, Delphi Iam still using normal CamelCase since they support real classes.

And maybe i should stop here, because i dont want to bash you Thread with offtopic things^^

Greets
Rackscha
Posted By: WretchedSid

Re: Lite Foundation - A powerful Lite-C library - 05/15/11 16:28

Originally Posted By: Rackscha
And maybe i should stop here, because i dont want to bash you Thread with offtopic things^^

Nah, don't worry, you help spreading lite foundation (6 dls since your first post), so keep the Off-Topic talk as long as you want tongue

Btw, I doubt that the underscore helps, here are a few examples from Lite Foundation:
Code:
LFStringCreateWithCString();
LFSetAddValue();
LFAutoreleasePoolDrain();
LFEnumeratorNextObject()



Its visible to which class the function belongs although its camel case. I find this more easily to read plus its also states what it does (which is also a naming convention I favor (say what you do without saying how you do it)).
Posted By: Rackscha

Re: Lite Foundation - A powerful Lite-C library - 05/15/11 21:21

Quote:
Nah, don't worry, you help spreading lite foundation (6 dls since your first post), so keep the Off-Topic talk as long as you want tongue

Okay^^

Quote:
Btw, I doubt that the underscore helps, here are a few examples from Lite Foundation:


Mh, see what you mean. The underscore had another reason: My typerythm grin.

May try out to use PureCamelCase since i have to rewrite most parts of my game, so rewriting some ugly parts of my guy seems to be something on my checklist too.

But i have a mainproblem whenerver i write code thats not Delphi.. i start to write delphi in LiteC for example v.v:

Value := Value + 1;

Serious who had this nightmare when developing pascal >.<.

Anyway. Going to bed now, tommorow is time for work again laugh

Greets
Rackscha
Posted By: WretchedSid

Re: Lite Foundation - A powerful Lite-C library - 05/16/11 23:53

Okay, ten downloads so far since my server is down.
This means that there are at least eleven opinions about Lite Foundation and I want to hear them. Wether it is a "its too complicated", "I don't get it" or a "I fucking hate you, please die", please write it down.

If you don't like certain parts, go ahead an blame me! Otherwise I can't improve the whole stuff.
Posted By: tzw

Re: Lite Foundation - A powerful Lite-C library - 05/17/11 04:53

i think the problem is that majority of users need not use such ADTs,they hold small projs,they focus on developing rapidly.
so your lib wont help them so much though your lib is veeeeeery powerful.
imp a path finding system i guess is more useful to them,and reach 1K downloads…

well, sorry for my English.
Posted By: WretchedSid

Re: Lite Foundation - A powerful Lite-C library - 05/17/11 07:45

Yes you are probably right, the problem is that a pathfinding solution doesn't fit into my kernel project where I need Lite Foundation instead ^^
Anyway, imo even small hoby projects can profit from Lite Foundation, eg. the array class can be dropped into any project and works perfectly fine with storing eg. entities. This would help beginners because they don't need to implement their own (which probably does some horrible memory faults), without the need that they understand how eg. the LF runtime works.

But yeah, your point is nonetheless valid and I would love to hear from the "average" user what they wish and what could have been done better to even catch the average user.
Posted By: FlorianP

Re: Lite Foundation - A powerful Lite-C library - 05/17/11 08:41

Server down? o_O
Posted By: WretchedSid

Re: Lite Foundation - A powerful Lite-C library - 05/17/11 08:45

Yes, I already posted an alternate link (can't edit my first post anymore).
But here is the link again, just in case someone missed it:
http://cl.ly/6ghG
Posted By: FlorianP

Re: Lite Foundation - A powerful Lite-C library - 05/17/11 09:45

Okay...at first I was wondering why i didnt post here already as this seemed pretty familiar, but then it came back to me...

I gotta say - I'm sorry - in advance; i usually don't like people bitching about manuals if they dont understand it, but...
your documentation is total crap.
It has several grammatical mistakes, uses _really_ unusual vocabulary, doesnt seem to have any strcture, jumps from informal crap ("It does exactly what it says;" and afterwards you tell me what it really does?) to complete jibberish ("This is useful to provide one way to achieve a result, without knowing the targets." ????) - its just written awfully.
f.i.
Quote:
Returns the string and delegating its ownerhship to the caller.
! This forbidden as the function signals that it doesn't delegate the
! ownership to the caller!

delegate? ownership? What the...!!??

...the doxygen is fine...

Alright...sorry for the little outburst - and to the point:
I think I can finally grasp the concept and I gotta say - I really like the idea. Nevertheless I just dont know why to use it as it is now. I got to work with (visual) c++ anyway...also beacuse theres just no way to do a serious project in SED.
Also i dont get why someone who writes in C and needs classes would use LiteFoundation instead of C++ (or objective-c) as it seems to be pretty invasive not really worth the effort (but maybe thats just me)

Something like a pre-compiler that lets you write C++ syntax (classes, inheritance and stuff...) and translates it into Lite-C would be cool...dunno if someone already did this or if its even possible - just an idea that might get some more feedback

so long
Posted By: WretchedSid

Re: Lite Foundation - A powerful Lite-C library - 05/17/11 10:20

Originally Posted By: FlorianP

your documentation is total crap.
It has several grammatical mistakes, uses _really_ unusual vocabulary, doesnt seem to have any strcture, jumps from informal crap[...]

Yup yup and yup. The problem is, it follows an structure. The thing is, the getting started guide is to tell people how things are meant to be. I can't throw a huge bunch of code at people and say "hey, learn it!".
This is why the informal "crap" is there, it is meant to tell you the naming conventions, memory conventions and all this stuff. Plus the global concept of Lite Foundation, how things work in general etc. pp.
This knowledge is supposed to give you an advantage, because it sets a default way for how things work and saves you from trips to the reference just to look what the function does or what kind of object it returns.

Its not really easy to explain all this stuff and looking at the rest of your post it looks like you didn't tried to understand it (or you just failed because of my grammar). I will try to write it better, and this is also why I asked for feedback (without, I can't improve it).

Originally Posted By: FlorianP

Quote:
Returns the string and delegating its ownerhship to the caller.
! This forbidden as the function signals that it doesn't delegate the
! ownership to the caller!

delegate? ownership? What the...!!??

What the "ownership" is, is described in broad detail in the chapter before your quoted line. And I really hope that you don't count "delegate" to the unusual vocabulary, because it isn't unusual. In Deutschland würden wir übrigens "delegieren" dazu sagen.
"Es ist verboten die ownership an den caller zu delegieren weil der Funktionsname signalisiert das genau das nicht passiert."


Originally Posted By: FlorianP

Nevertheless I just dont know why to use it as it is now. I got to work with (visual) c++ anyway...also beacuse theres just no way to do a serious project in SED.

Yes, but it is possible do a serious project in LED. Beside that, I don't want you to use Lite Foundation just because I think its cool. If you like C++, C#, ObjC or whatever more than C, feel free to use it.
But I have discussed this already two times in this thread and a third time isn't needed. Just read my response to eg. ratchet, I have discussed it there and then letter in response to Uhrwerk again.

Quote:
Also i dont get why someone who writes in C and needs classes would use LiteFoundation instead of C++ (or objective-c) as it seems to be pretty invasive not really worth the effort (but maybe thats just me)

Again, this has been already discussed two times here. Its not just the concept of classes that makes Lite Foundation what it is.

Quote:
Something like a pre-compiler that lets you write C++ syntax (classes, inheritance and stuff...) and translates it into Lite-C would be cool...dunno if someone already did this or if its even possible - just an idea that might get some more feedback

Probably, but I hate C++ (yes, its that simple)
Posted By: FlorianP

Re: Lite Foundation - A powerful Lite-C library - 05/17/11 11:05

Originally Posted By: JustSid
The problem is, it follows an structure.


Hum? I couldnt find any structure but way too long sentences.
I think it was Goethe who once wrote something like this to a local duke - 'I'm sorry my letter is that long but i couldnt find the time to make it shorter'
My point: The shorter the better.

Originally Posted By: JustSid

The thing is, the getting started guide is to tell people how things are meant to be.


If I read a 'getting started' I want to get started and not be told why I should use Lite-Foundation in the first place.
("Now you might come up with the question: Why all the hassle?" - No i wont...)

Originally Posted By: JustSid

I can't throw a huge bunch of code at people and say "hey, learn it!".
This is why the informal "crap" is there, it is meant to tell you the naming conventions, memory conventions and all this stuff. Plus the global concept of Lite Foundation, how things work in general etc. pp.
This knowledge is supposed to give you an advantage, because it sets a default way for how things work and saves you from trips to the reference just to look what the function does or what kind of object it returns.


Then maybe call it 'All about Lite-Foundation' not 'Getting started'.

Originally Posted By: JustSid

Its not really easy to explain all this stuff and looking at the rest of your post it looks like you didn't tried to understand it


Ye well I tried to _get started_ and got already lost on the first page.

Originally Posted By: JustSid
this is also why I asked for feedback (without, I can't improve it).


I consider this to be productive feedback.

Originally Posted By: JustSid
Yes, but it is possible do a serious project in LED.


Whats LED?


So my advice is - write a real 'Getting started'. Theres way too much information thats supposed to be in a Manual or a 'reading further'
You can shorten it by half (well almost^^) only if you cut out all the
"Lite Foundation needs to trick a little bit;"
"surprise"
"Now you might come up with the question: Why all the hassle? Why not just use structs
and ship a few functions with them and everything is just fine?"
"Now thats all fine, but it leads
to a few problems: The main question is: When do I have to free what?"
“Hey, I&#700;m interested in this object, please keep it alive”
“Alright, thanks for keeping the object alive, but I don&#700;t
need it anymore” (informal crap aleart^^)
"You probably have already noticed it"
...and on and on...
These sentences either contain none information at all or could be said in 3 words max.


I hope this might help you interest some more people in Lite Foundation - Im pretty sure this is a main reason why 'nobody gets it'.
I also have some more suggestions for your documentation if youre interested pm me or so.
Posted By: Rackscha

Re: Lite Foundation - A powerful Lite-C library - 05/17/11 15:57

@FlorianP: i have some first simple conceptional notes here for a PreCompiler.
Thought about it for some time now. Hopefully i have some time todo a simple prototype during the next week(this one is already full>.<).

My goal is to provide a precompiler that can process a class, an reconstructs it as a struct with function bindings, being able to derive from another class.

Precompiler automatically constructs the base constructor of the class(which includes the function binding).

LATER support for This/Self yet. This needs a second step, going through the whole code and adding new parameters to the medthos(an autoadded parameter).

Classes will be plased in .class files(easier for me to just go through the classfile instead of going through the whole project, maybe screwing womething up. LAter maybe in normal cfiles). class files are precomputed to c files, and collected in a file called Classes.c. This should be the only file the user has to include.

Thats my concept, already wrote a simple parsing systems at work for another purpose. Lets see.

Greetings
Rackscha


Posted By: WretchedSid

Re: Lite Foundation - A powerful Lite-C library - 05/29/11 21:16

Wow, the next Lite Foundation release is here. And it is totally awesome, I'm really excited!

Just to get you started right away, here is the download link:
http://cl.ly/7Btf

And now about the new features:
The first new feature is of course multithreading, this gives you three new classes: LFThreadRef, LFLockRef, LFConditionRef. Now the sad part: Lite-C users will certainly have crashes when using the multithreaded Lite Foundation. Why? Because of the tricks I had to use to get Lite Foundation running, it uses a lot of global variables and function pointers. There is no way around this and it makes no sense to introduce a big lock because I can't handle everything on the Lite Foundation level.
I'm sorry and will certainly file a lot of feature requests!

But, there are way more exciting features. Lets start with a really useful one: Zombies.
Zombies is a operating mode of the runtime, in this mode the runtime won't purge objects from memory but let there bodies leak intentionally. Why? For a good reason: It will log EVERY access you make to the death object! So you can be sure that you can track over releasing of objects and using of deallocated objects as fast as possible.
The runtime will log the function that caused the problem, the objects class and the objects pointer, enough to give you an idea where the problem is. This is incredible useful for beginners new to the Lite Foundations memory model.

Here is an actual example and the output in the console:
Code:
LFRuntimeSetZombieLevel(kLFZombieLevelScribble); // Enable zombies
	
LFStringRef string = LFStringWithCString("Hello"); // Create a new, autoreleased string
LFRelease(string); // Release the string. This will deallocate the string because we are the only owner

LFStringAppendCString(string, "World"); // Do something bad!



This piece of code will log:
Code:
Sent LFStringAppendCString to already deallocated instance <LFString 0x1005b0>
Sent LFRelease to already deallocated instance <LFString 0x1005b0>


Pretty cool, hu?


More cool stuff? Oh yea, remember the first Lite Foundation version? It had a LFDateRef class and to be honest it sucked. One couldn't do any math with it and it couldn't represent fractions of seconds.
Now Lite Foundation knows again a LFDateRef object. Together with its buddies LFDateComponentsRef and LFTimeZoneRef, it is crazy powerful. And I mean really crazy powerful!
LFDateRef just wraps the time in seconds and sub-miliseconds since 1st January 2001. LFDateComponentsRef can get the single components (days, hours, etc) from a LFDateRef plus it can calculate with them. The difference between my birth and today in minutes? No problem, just two lines of code!
LFTimeZoneRef is an abstract wrapper for a time zone (currently no daylight saving time support, sorry). It holds an offset to GMT in seconds, thats it.

Here is an actual example:
Code:
// This function prints the minutes and hours passed since Lite Foundations release
// Lite Foundation was released on 19/04/2011 03:27 CET
void printTimeSinceLFRelease()
{
	LFDateComponentsRef components = LFDateComponentsCreate(2011, 04, 19, 3, 27, 0.0); // Create a new LFDateComponentsRef containing the date of the Lite Foundation release
	LFTimeZoneRef timeZone = LFTimeZoneCreateWithAbbreviation(LFSTR("CET")); // The thread was created in the central europe time zone
	
	LFDateRef releaseDate = LFDateComponentsGetDate(components, timeZone);
	LFDateRef currentDate = LFDateCreate(); // Grab the current date
	
	
	LFDateComponentsRef difference = LFDateComponentsGetDifferenceFromDates(currentDate, releaseDate, timeZone, kLFHoursComponent | kLFMinutesComponent); // Get the hours and minutes passed since the release
	// For simplicity we use the same time zone (CET) again, but you can interchange the time zone with yours to get the time since the post and the current date in your time zone
	
	printf("%i hours and %i minutes passed since the inital Lite Foundation release!", difference->hour, difference->minute);
	
	// Cleanup
	LFRelease(components);
	LFRelease(timeZone);
	LFRelease(currentDate);
}


All time function respect leap years and all the such and are really crazy powerful and useful. The download folder contains an example.

With working dates object, one could also revive the LFTimer class, right? Right! There is a new LFTimer class, it allows firing in the sub-milisecond range, or in an hour. Or at a specific date.

There is a lot of other stuff, but I guess I have already mentioned the most awesome stuff. Like I said, I'm really, really excited about this release and I hope you like it.

@FlorainP: Sorry, there is no new getting started guide in this release. But I'm working on it! If you don't mind, I will send you a draft via PN so you can say something about it?

Edit: Fun fact; The line count of this release is almost twice as large as the one from the previous release. 12333 vs 6402 lines.
Posted By: Quad

Re: Lite Foundation - A powerful Lite-C library - 05/29/11 21:53

I did not really took a deep look in lite foundation yet, but retain count thing alone is a reason for me to use it. (that comes from objective-c if i am not mistaken?)

Next time(not soon) i start something with lite-c i am definitely going to use it.
Posted By: WretchedSid

Re: Lite Foundation - A powerful Lite-C library - 05/29/11 22:11

Yay, awesome laugh

Originally Posted By: Quadraxas
that comes from objective-c if i am not mistaken?

Not really. ObjC by itself isn't an reference counting environment, but Foundation is. This is, everything that inherits from NSObject is automatically part of the reference counting environment and usually if one programs something for OS X or iOS, everything inherits von NSObject.
But its not only ObjC, many other languages also have the concept of an reference counting environment, eg. boost:: shared_ptr (afair its also in the tr1 namespace and C++0x has its in its default library).

Anyway, if you are familiar with Foundation, you will also be familiar with Lite Foundation. I tried to be very near to Foundation because I work with it every day (although the name Lite Foundation doesn't imply that its a Foundation spin off). There are also different concepts that I don't like in Foundation, but you will see what I mean if you are familiar with it.
Posted By: Quad

Re: Lite Foundation - A powerful Lite-C library - 05/29/11 22:22

Yeah, i only used obj-c with foundation so far(enterprise survey app with lots of features for ipad for a company/internal use.), my mistake.

I did not like(did not hate either) obj-c except for some neat features like retain counting etc... Personally liked android approach for screens more.(written same app for android too.)

maybe i just did not fully understand it... maybe.
Posted By: WretchedSid

Re: Lite Foundation - A powerful Lite-C library - 05/29/11 22:31

Mh, you mix up the language and its frameworks a lot here. For example, UIKit (the high level screen rendering stuff framework) is not part of Foundation.
What most people hate about ObjC is the [] stuff, but I personally like it a lot because it makes nested function calls way easier to read.
Posted By: Quad

Re: Lite Foundation - A powerful Lite-C library - 05/29/11 22:43

yeah, by saying obj-c, i generally refer to objc+foundation+uikit(ios development platform as a whole).
Posted By: Joozey

Re: Lite Foundation - A powerful Lite-C library - 05/30/11 01:04

A bit about the naming convention: C and C++ use underscores and all lowercase for functions, and camelcase without underscores for structure (etc) names. Common practice is to use names that are gramatically correct and clear: create_entity rather than ent_create. But since 3dgs doesn't follow that convention, it might be a wiser decision to follow 3dgs' choice anyway.

Nice lib, such things are very useful, and every programmer should built likewise on his own as he goes laugh. Having a starting lib like yours saves a lot of time.
Posted By: WretchedSid

Re: Lite Foundation - A powerful Lite-C library - 05/30/11 09:40

I wouldn't say that C and C++ has its own naming convention. Its more like with the different syntax styles, some like K&R more, some other like the Allman style more.
The default libraries use underscores for functions and types, but caps for defines (ERRNO for example).

Originally Posted By: Joozey
Common practice is to use names that are gramatically correct and clear: create_entity rather than ent_create.

I guess ent_create came from the idea to have a three letter abbreviation as prefix, we have also snd_create and such things. I guess the idea was to have an convenient way to do things.

Originally Posted By: Joozey
But since 3dgs doesn't follow that convention, it might be a wiser decision to follow 3dgs' choice anyway.

No. Functions have to be verbose, they have to tell one what they do. This saves one from going to the manual over and over again, you just need an idea of what you want and how it might be named, and you will most likely find it right away.
But thats just my personal preference and it was really hard with SED to have this style because it lacked code completion. Luckily we have LED now which makes it really easy to work with heavy verbose libraries like Lite Foundation.


Originally Posted By: Joozey
every programmer should built likewise on his own as he goes laugh.

Yes, couldn't agree more. There will always be things that one doesn't like without doing it on its own. But Lite Foundation can also be used in such cases, eg. one could remove classes (or all classes) that one don't like and work with the runtime directly. Or remove the runtime and rewrite it from scratch while still having the default classes.
Posted By: Ch40zzC0d3r

Re: Lite Foundation - A powerful Lite-C library - 11/03/11 06:41

All links down frown
Please reupload it, i need it :|
© 2024 lite-C Forums