1 /*
2  * This file is part of liblcf. Copyright (c) 2021 liblcf authors.
3  * https://github.com/EasyRPG/liblcf - https://easyrpg.org
4  *
5  * liblcf is Free/Libre Open Source Software, released under the MIT License.
6  * For the full copyright and license information, please view the COPYING
7  * file that was distributed with this source code.
8  */
9 
10 #ifndef LCF_CONTEXT_H
11 #define LCF_CONTEXT_H
12 
13 #include "string_view.h"
14 
15 namespace lcf {
16 
17 /**
18  * Context information for iteraing lcf fields
19  */
20 struct ContextNameBase {
21 	/** Constructor */
ContextNameBaseContextNameBase22 	constexpr ContextNameBase(StringView n, int i) : name(n), index(i) {}
23 
24 	/** Name of the enumerated field */
25 	StringView name;
26 
27 	/** Array index when the object is part of a list (-1 when not) */
28 	int index = -1;
29 };
30 
31 /**
32  * Context information for iteraing lcf fields
33  * @tparam StructType The type of the Struct
34  */
35 template <typename StructType>
36 struct ContextStructBase : ContextNameBase {
37 	/** The type of the struct */
38 	using StructType_t = StructType;
39 
40 	/** Constructor */
ContextStructBaseContextStructBase41 	constexpr ContextStructBase(StringView n, int i, StructType_t* o)
42 		: ContextNameBase(n, i), obj(o) {}
43 
44 	/** Object instance (cast to appropriate RPG-type */
45 	StructType_t* obj = nullptr;
46 };
47 
48 /**
49  * Context information for iteraing lcf fields
50  * @tparam StructType The type of the Struct
51  * @tparam ParentCtx The type of the parent context.
52  */
53 template <typename StructType, typename ParentCtxType>
54 struct Context : ContextStructBase<StructType> {
55 	/** The type of the struct */
56 	using StructType_t = typename ContextStructBase<StructType>::StructType_t;
57 
58 	/** The type of the parent context */
59 	using ParentCtxType_t = ParentCtxType;
60 
ContextContext61 	constexpr Context(StringView n, int i, StructType* o, const ParentCtxType_t* pctx)
62 		: ContextStructBase<StructType>{n, i, o}, parent(pctx) {}
63 
64 	/** Context of the parent (nullptr when no parent) */
65 	const ParentCtxType_t* parent = nullptr;
66 };
67 
68 } //namespace lcf
69 
70 #endif
71