Echo Writes Code

boot.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "crucible/core/boot.hpp"

#include "crucible/core/environment.hpp"
#include "crucible/core/handlers.hpp"
#include "crucible/core/iterators.hpp"

#include <exception>
#include <type_traits>
#include <utility>

namespace crucible
{
	auto boot_from_main(int argc, char **argv, Boot_Function const boot_function) -> int
	{
		// Very first thing: set the terminate handler so that we get a backtrace if anything crashes
		std::set_terminate(handle_terminate);

		// Parse environment variables
		environment().initialize();

		// Convert argv into Crucible data structures
		auto const arguments {
			iterate(argv, argc)
				.map([](char const *argument) {
					return String_View { argument, std::strlen(argument) };
				})
				.materialize<Heap_Buffer<String_View>>()
		};

		// Call into client code. If you're looking for where your program starts, it's here :)
		Exit_Status const status { boot_function(arguments.view()) };

		// Convert Exit_Status back into an int and finish up
		static_assert(std::is_same_v<std::underlying_type_t<Exit_Status>, int>);
		return static_cast<int>(status);
	}
}