|
Post by vovchik on Dec 16, 2017 9:43:46 GMT 1
Dear Joe, Good discovery - that we do not need an explicit DECLARE - and that we can simply use "_type" in the sub or function for a previously defind RECORD. And with pointers working, porting some bits just became a whole lot easier. With kind regards, vovchik
|
|
|
Post by bigbass on Dec 16, 2017 15:26:25 GMT 1
Hello vovchik
While all the information is there and documented correctly
sometimes I have a hard time connecting all the pieces because if you just think in bacon it makes sense following along with the code
and if you just think in c /c++ it makes sense of course
The problem is when we have to port code
We really need these types of demo code snippets of porting code to get down to the step by step what is really going on so that later porting of code could be simplified greatly
like the old saying "All is well that ends well"
Joe
|
|
|
Post by bigbass on Dec 16, 2017 20:06:23 GMT 1
Hey Guys maybe you wonder why use struct when we have RECORD here is a good reason when we port code from c++ I was able to replace the class with struct and get the events like this before This code works as is fltkevent.tar.gz (861 B) now will work to port this to use a RECORD (I hope) because of the pointer problem with the typedef struct you have to use USEC for it to compile Joe
|
|
|
Post by vovchik on Dec 17, 2017 0:19:33 GMT 1
Dear Joe,
Thanks. That example works fine. I am having another problem now trying to port an fltk 7-segment digital clock widget and and scratching my head at the moment, since I get runtime segfaults. If I can't figure it out by tomorrow evening, I'll post the sources and maybe one of us will have an insightful solution.
With kind regards, vovchik
|
|
|
Post by bigbass on Dec 23, 2017 2:58:52 GMT 1
Just a pointer test using g++ and all fine just don't forget To declare
Oh Forgot to say I am using my android 7 inch tablet x86 and the latest beta 3.7 compiles and runs without errors
Command line only later will try xsdl to get some GUI code too to test out fltk
PRAGMA INCLUDE <string.h> <stdio.h> <iostream> PRAGMA LDFLAGS PRAGMA COMPILER g++ PRAGMA OPTIONS -Wno-write-strings -Wno-pointer-arith -fpermissive 'PRAGMA BACONLIB OPTION PARSE FALSE
x = 4 '---must declare the pointer DECLARE *x_ptr TYPE int '---get the address of x that is the x pointer x_ptr = &x PRINT *x_ptr '--- we can edit the value of x it is 4 '--- by giving it a new value of 9 *x_ptr = 9 PRINT x
|
|
|
Post by bigbass on Dec 23, 2017 18:06:11 GMT 1
Hey Guys Found the way to use class in Bacon And get the arrow operator with struct /class Using g++ we get many advantages These advantages come at a cost of another Level of complexity to port c++ But with this demo we will be able to do anything Using the c++ classes finally! Joe linkI ported code from this link as a reference PRAGMA INCLUDE <string.h> <stdio.h> <iostream> PRAGMA LDFLAGS PRAGMA COMPILER g++ PRAGMA LDFLAGS -L. PRAGMA OPTIONS -Wno-write-strings -Wno-pointer-arith OPTION PARSE FALSE 'PRAGMA BACONLIB
'--- note that USECPLUS shortens code looks a little more like bacon usec
USEH #define USECPLUS extern "C" { #define ENDCPLUS }
USECPLUS #include <iostream>
struct sample {
int a; int b; void init(int a) {this->a = a;} void display() {std::cout<<"a = "<<a;} };
ENDCPLUS END USEH
'---pointer to an object declaration DECLARE sam TYPE sample* '--- finally got the correct syntax for class sam = new sample() PRINT "Using Arrow Operator (->)" '---value assignment to a and back sam->init(100) sam->b=200 '---printing the values from inside the class/struct '--- we call a function in the class sam->display() '---print from bacon PRINT " b = ",(sam->b)
|
|
|
Post by bigbass on Dec 24, 2017 23:59:43 GMT 1
Hey Guys
This is my Christmas Gift to all that may find it useful
There is a lot if information on the web but like a big puzzle Sometimes we have to put it all together to see the final image
Here are my thoughts not a copy And paste of how to understand it in Bacon
This is all in reference to the above post with full working code
Okay for a moment forget about how things are now in C, bacon and C++ think out-of-the-box for this concept to work correctly
Think about a subroutine just a block of code that we get from a call back
But this is a special subroutine don't establish any limits in your mind of what you can do in this subroutine because in your mind you have no limits
Now let's say it's a low-level subroutine that could call other subroutines and functions and get values from structs and change values in structs but all of this is done in a copy or a layer but not a blob of RAM but a named memory location we can free up later to keep things clean -----------------------------
include <iostream> using namespace std;
class sample { private: int a; public: int b; void init(int a) {this->a = a;} void display() {cout<<"a: "<<a<<endl;} };
int main() { //pointer to an object declaration sample *sam = new sample(); //value assignment to a and back sam->init(100); sam->b=200; //printing the values sam->display(); cout<<"b: "<<sam->b<<endl; return 0; }
Okay now let's talk in real code line by line
Just a list of exact instructions to get that job done
Bacon
sam = new sample()
C++
//pointer to an object declaration note that sample is the C++ class sample *sam = new sample();
This is really beautiful even though the C++ class syntax code part is ugly to read that this calls later
We make a named copy of memory called sam by using the new command. The new command places all that is to the right of the new command in memory then converting the named memory location sam to a pointer all this memory allocation is done automatically in C++
We still need to declare in c++
sample *sam
But we are coding in bacon now so that won't work with that syntax unless we declare the pointer in this case to the C ++ class {but we will use a struct because that makes everything public Automatically }
DECLARE sam TYPE sample*
After declaring sam a pointer to the struct ( the class) we can now
Have full access to all that is in the C ++ class
In bacon we attach the * To the type sample*
now we're going to use the arrow operator (->) and make a value assignment to the 'a' variable in the struct and return
sam->init(100)
in the struct you can see that we have void init(int a) You can see that (a) is defined as an integer but it has no value yet
You can see that b inside the struct doesn't have a value assigned to it yet but we're going to give it a value of 200
sam->b=200
The next part is interesting we're going to call display() from inside the struct get the value and then print it The interesting part is that there is no call back
sam->display( )
The last part PRINT just shows how we can print from bacon syntax directly
Bacon
PRINT "b", (sam->b)
C++
cout<<"b: "<<sam->b<<endl;
|
|
|
Post by vovchik on Dec 25, 2017 11:08:30 GMT 1
Dear Joe,
Many thanks for that. I am still digesting it. And Merry Christmas.
With kind regards, vovchik
|
|
|
Post by bigbass on Dec 26, 2017 0:12:09 GMT 1
Hey vovchik
Thanks I wish you also the best Christmas I made a lot of home made lasagna I would have made a big pan for you
anyway I updated the above code
I know we need the class and events and I see clearly how we can adapt the code to port to bacon
At first I hated c++ and now see better that in many ways it is better when you look at it from the view of a way to code a GUI like fltk /fluid
and the new command is a clever way to get around many problems doing everything in memory and pointing back to a class and keep everything organized in code blocks however I prefer to use struct instead of class because its easier to just work with making everything default to public
Joe
|
|
|
Post by vovchik on Dec 26, 2017 0:43:25 GMT 1
Dear Joe,
Thanks. I love lasagna - and also make it myself, including the pasta, when I'm not lazy - and am extremely pleased that Neopolitan pizza is now a "protected" dish at UNESCO. I will have to study the code tomorrow. I thnk you've got it nailed. It's late here now, but "Buen provecho!" whatever the time of day.
With kind regards, vovchik
|
|
|
Post by bigbass on Dec 19, 2018 17:27:16 GMT 1
there are a lot of things we can do now that we have c++ that are undocumented one complex example is getting classes to work and be seen in bacon here we go I will simplify an older example that was a struct and make it a public class (its the same idea)
and add a const char* so we have strings and we can use bacon's PRINT
Joe
PRAGMA INCLUDE <string.h> <stdio.h> <iostream> PRAGMA LDFLAGS PRAGMA COMPILER g++ PRAGMA LDFLAGS -lbacon++ PRAGMA OPTIONS -Wno-write-strings -Wno-pointer-arith OPTION PARSE FALSE 'PRAGMA BACONLIB
DECLARE a TYPE int DECLARE b TYPE int '--- we can use TYPE const char or simply just TYPE char DECLARE some_str TYPE char ARRAY 5
'--- how to make a simple public class in bacon that contains a function USEH class sample { public: const char* some_str; int a; int b; void display() {std::cout<<"a = "<<a;} }; ENDUSEH
'---pointer to an object declaration (we use a class called sample) DECLARE sam TYPE sample* '--- finally got the correct syntax for class sam = new sample() PRINT "Using Arrow Operator (->)" '---value assignment to a and back sam->a = 100 sam->b = 200 sam->some_str = "hello bacon from a class " '---we want to print from bacon from that class SOME_STR$ = sam->some_str PRINT SOME_STR$ FORMAT "%s" PRINT '---printing the values from inside the class/struct '--- we call a function in the class sam->display() '---print from bacon PRINT " b = ",(sam->b) '--- we can cheat the const char in c++ sam->some_str = "good bye bacon from a class " SOME_STR$ = sam->some_str PRINT SOME_STR$ FORMAT "%s"
|
|
|
Post by vovchik on Dec 20, 2018 13:16:11 GMT 1
Dear Joe,
Thanks. It works nicely. I would replace PRAGMA LDFLAGS -.L with PRAGMA LDFLAGS -lbacon++, though, since you don't need a local copy of that bacon lib and may not have one.
With kind regards, vovchik
|
|
|
Post by bigbass on Dec 20, 2018 17:54:09 GMT 1
Hello vovchik yes your are right a couple of days ago we talked about that but I forgot to update this demo will do so now!
Thanks for all your quality feedback
in the end we need to document these odd things that can be done maybe in the future we will have a need to port some c++ based code or simplify the class syntax for use in bacon
I understand well that most basic language users detest c++ but I see it (as just another tool in the box) in a world of a lot of code written in c++ when it comes to memory the c++(new) command makes everything very easy
using fltk we can go about this several ways because they use headers that are included in this example above there are no headers for the class it is a stand alone example
I guess in the end we all want simple syntax to remember that should be the goal but those that came before us (in the beginning when linux was written in c) used a primitive hardware memory management approach and we are stuck thinking like a simple low level machine instead of lifting the bar to think like a human would do and we can have new ways to solve problems on a higher level
a real world example would be barking like a dog to communicate with a dog instead of teaching the dog to understand human commands it might be easier for the dog but barking isn't something I really want to do
Joe
|
|
|
Post by bigbass on Aug 11, 2019 4:48:53 GMT 1
Hello just want to revisit this thread to use bacon's CLASS ENDCLASS command with a real world demo example www.includehelp.com/cpp-tutorial/arrow-operator-as-class-member-access-operator.aspxI tried various "experimental ways" in 2017 to get the class to compile that worked but now would like to show the official way having real source code examples from c++ that was ported to bacon basic-converter.proboards.com/post/10626/threadsometimes I wonder why I even learned c++ "unofficially" because it looked so strange to me and must admit hated the :: everywhere but makes more sense to me now from the GUI point of view and I like it when using fltk and fluid Joe PRAGMA LDFLAGS -lfltk -L. PRAGMA LDFLAGS -lbacon++ PRAGMA COMPILER g++ PRAGMA OPTIONS -Wno-write-strings -Wno-pointer-arith -Wno-permissive -Wno-multichar PRAGMA INCLUDE <iostream>
OPTION PARSE FALSE
'--- now using the official baconized CLASS ENDCLASS command CLASS sample
private: int a; public: int b; void init(int a) {this->a = a;} void display() {std::cout<<"a: "<<a<<"\n";} ENDCLASS
'--- must DECLARE the pointer to the class and the type DECLARE sam TYPE sample*
'---pointer to an object declaration sam = new sample() '---value assignment to a and back sam->init(100) sam->b=200 '---printing the values sam->display() PRINT "b: ",sam->b
|
|
|
Post by bigbass on Aug 28, 2019 18:24:46 GMT 1
structured data is what we use to build many programs in C they use struct in Bacon we use RECORD in C++ they use classes problem: the formatting is not the same between those languages solution: find a compatible format recently Peter added an excellent JSON parser function in bacon in the latest beta version that has the HASDELIM command I wrote a homemade c++ parser that compiles in bacon that takes JSON strings as is without the need to escape the code then places them in arrays that work in c++ and in bacon as associative arrays dynamically every thing in this old thread was static now we can auto generate the JSON to struct/class the JSON string part here was hand typed to follow the demos in past posts so that the idea could be understood with working demo code basic-converter.proboards.com/thread/493/structures-classes-json?page=3there are examples of the same code as RECORD and struct every time we write code we have to port that part usually manually having JSON format is an organized standard used in many languages so its good to get it working in bacon for many reasons I guess the point here is how can we convert code to use JSON as a universal standard and work in bacon at the same time compiles in g++ struct2json.bac
PRAGMA INCLUDE <iostream> PRAGMA INCLUDE <ostream> PRAGMA INCLUDE <string>
PRAGMA LDFLAGS PRAGMA COMPILER g++ PRAGMA OPTIONS -Wno-write-strings -Wno-pointer-arith -fpermissive PRAGMA BACONLIB OPTION PARSE FALSE
'--- raw-string literal example so that we dont read in a file '--- can now be used also as a "here doc" saved as the variable json '--- std::string json = R"( some multi line data)";
'--- converted a c struct to JSON syntax and works in c++ baconized std::string json = R"({ "title": "C++ no GUI", "author": "bacon user", "subject": "C++ bacon", "bookid": "234575" })" ; '--- show that we can output this easily in c++ std::cout << json << "\n"; '--- we can cheat if we use a string pointer ptr '--- to the RAW string address from c++ DECLARE *ptr TYPE STRING ptr = &json ' Declare array DECLARE Cars$ ASSOC STRING
'--- how to get around the conversion of types '--- since we have the addressed memory already allocated in c++ '--- We can now SPLIT a raw string literal in BaCon '--- if we point to the address of the raw string literal '--- declared as a STRING a nice discovery PRINT PRINT "=========*ptr to raw string literal JSON beautifier===========" PRINT LOCAL dimension
SPLIT *ptr BY "," TO array$ SIZE dimension FOR i = 0 TO dimension -1 '--- a simplified JSON beautifier IF HASDELIM(array$[i] ,":") OR HASDELIM(array$[i] ,"]") THEN filtered$ = REPLACE$(array$[i], "{", "{ \n") filtered$= REPLACE$(filtered$, "}", "\n }") filtered$= REPLACE$(filtered$, "[", "[ \n") filtered$= REPLACE$(filtered$, "]", "\n ] ") PRINT filtered$ '--- make a dynamic bacon associative array '--- automatically generated for you ! itemline$ = CONCAT$("item", STR$(i)) Cars$(itemline$) = array$[i] END IF NEXT
' Lookup the index names (item1, item2, item3) LOOKUP Cars$ TO item$ SIZE size
PRINT PRINT "======automatically generated bacon ASOC STRING item2 =========" PRINT PRINT Cars$("item2") PRINT
PRINT PRINT "========= a bacon associative array special use =====" PRINT
'--- arrary 0 ,1, 2 manually build it how you want i = 1 PRINT Cars$(item$[i-1]) & Cars$(item$[i]) & Cars$(item$[i+1])
PRINT PRINT "========= indexed associative array ===============" PRINT ' Print values we could filter easily here also if needed ' you usually use i = 0 but wanted to start on the fourth array FOR i = 0 TO size-1 PRINT Cars$(item$[i]) NEXT
terminal output { "title": "C++ no GUI", "author": "bacon user", "subject": "C++ bacon", "bookid": "234575" }
=========*ptr to raw string literal JSON beautifier===========
{ "title": "C++ no GUI" "author": "bacon user" "subject": "C++ bacon" "bookid": "234575" }
======automatically generated bacon ASOC STRING item2 =========
"subject": "C++ bacon"
========= a bacon associative array special use =====
{ "title": "C++ no GUI" "author": "bacon user" "subject": "C++ bacon"
========= indexed associative array ===============
{ "title": "C++ no GUI" "author": "bacon user" "subject": "C++ bacon" "bookid": "234575" }
------------------ (program exited with code: 0) Press return to continue
|
|