SomeRanDev / reflaxe.CPP

An alternative C++ target for Haxe that generates dependent-less, GC-less C++17 code.
MIT License
72 stars 5 forks source link

Update `Array.hx` to have `set` and `get` methods #49

Closed Just-Feeshy closed 3 months ago

Just-Feeshy commented 3 months ago

Example of how it works:

Main.hx:

package;

class Main {
        public static function main() {
            var a = new cxx.std.Array<Int, 5>();
            a.set(0, 5);
            a.set(1, 3);
            a.set(2, 1);

            trace("Hello, World!" + (a.get(0) + a.get(1) + a.get(2)));
        }
}

To Main.cpp:

#include "Main.h"

#include <array>
#include <memory>
#include <string>
#include "_AnonStructs.h"
#include "haxe_Log.h"

using namespace std::string_literals;

void Main::main() {
    std::shared_ptr<std::array<int, 5>> a = std::make_shared<std::array<int, 5>>();

    (*a)[0] = 5;
    (*a)[1] = 3;
    (*a)[2] = 1;
    haxe::Log::trace("Hello, World!"s + std::to_string((((*a)[0]) + ((*a)[1]) + ((*a)[2]))), haxe::shared_anon<haxe::PosInfos>("Main"s, "Main.hx"s, 10, "main"s));
}

Output:

Main.hx:10: Hello, World!9
Just-Feeshy commented 3 months ago

I added a warning comment to let others know that these functions aren't safe.