CPP target

Monkey Targets Forums/User Targets/CPP target

AdamRedwoods(Posted 2013) [#1]
Here's an extension to the CPP target.

Place these two files under targets/cpptool/modules

cpp_ext.monkey



cpp_ext.cpp


TO USE
import cpp_ext
Add BBGameInit() at the top of your Main() to use Filestreaming.

CAUTION
I've added helper objects, but really this target does not hold your hand for anything. No debugging, no input, no window. No garbage collection unless you call it. FileStreaming is the only way for file access.

Transcc will NOT copy over any files in your "data" folder. You could however, path from the .build folder by using "../myfile.txt".

If you plan on using garbage collection (GC), you must use GCMark() on EVERYTHING you wish to retain before each GCCollect() call.


AdamRedwoods(Posted 2013) [#2]
Please note this example file uses a file called "test.txt" which is NOT in a data folder, but rather in the .build folder.
Example file:
[monkeycode]
Import monkey
Import cpp_ext

Import brl.filestream


Function Main:Int()

BBGameInit()
Print "*** started ***"


Local f:=FileStream.Open("test.txt","r")
Local ms:Int = Millisecs()

If f
While f.Eof()=0
Local b:Int = f.ReadByte()
Print b
Wend

f.Close()

Endif

For Local i:Int=0 To 100000
Local j:Float = Sqrt(123.0 * i)
next

Print "ms "+(Millisecs()-ms)

Local foo:Foo = New Foo()


GCMark(foo)
GCCollect() ''will clean up f:FileStream

If Exists(foo) Then Print foo.goo '' if monkey thinks its there, MEMORY ACCESS VIOLATION!

Print "*** finished ***"
Return 0
End



Class Foo
Field goo:String = "Exists!"
End
[/monkeycode]


Sammy(Posted 2013) [#3]
Excellent post Adam, I learned quite a bit with this code, thank you! :)


nigelibrown(Posted 2014) [#4]
I am getting lots of errors using the above code with MonkeyXPro77f, ideas please?

TRANS monkey compiler V1.64
Parsing...
Semanting...
Translating...
Building...
main.cpp:2096:7: error: redefinition of 'class BBGame'
main.cpp:1718:7: error: previous definition of 'class BBGame'
main.cpp:2113:17: error: redefinition of 'BBGame* BBGame::_game'
main.cpp:1788:9: error: 'BBGame* BBGame::_game' previously declared here
main.cpp:2115:9: error: redefinition of 'static BBGame* BBGame::Game()'
main.cpp:1724:17: error: 'static BBGame* BBGame::Game()' previously defined here
main.cpp:2119:5: error: redefinition of 'int BBGame::Millisecs()'
main.cpp:1819:5: error: 'virtual int BBGame::Millisecs()' previously defined here
main.cpp:2123:14: error: prototype for 'signed char* BBGame::LoadData(String, int*)' does not match any in class 'BBGame'
main.cpp:1917:16: error: candidate is: virtual unsigned char* BBGame::LoadData(String, int*)
main.cpp:2127:7: error: redefinition of 'FILE* BBGame::OpenFile(String, String)'
main.cpp:1901:7: error: 'virtual FILE* BBGame::OpenFile(String, String)' previously defined here
main.cpp: In function 'void BBGameInit()':
main.cpp:2132:2: error: 'SetGame' is not a member of 'BBGame'
main.cpp: In function 'int bbMain()':
main.cpp:2953:2: error: 'gc_force_sweep' was not declared in this scope
TRANS FAILED: Error executing 'g++ -Wno-free-nonheap-object -O3 -DNDEBUG -o main_winnt main.cpp -lwinmm -lws2_32', return code=1
Done.


AdamRedwoods(Posted 2014) [#5]
this add-on is not really needed anymore. you can use the #CPP_GC_MODE=2 for the incremental garbage collection.
is there something specific you need?