Leviathan  0.8.0.0
Leviathan game engine
ObjectFileProcessor.h
Go to the documentation of this file.
1 // Leviathan Game Engine
2 // Copyright (c) 2012-2017 Henri Hyyryläinen
3 #pragma once
4 #include "Define.h"
5 // ------------------------------------ //
6 #include "ObjectFile.h"
7 #include "ErrorReporter.h"
8 #include <string>
9 #include <memory>
10 
11 namespace Leviathan{
12 
15 public:
16 
17  ObjectFileProcessor() = delete;
18  ~ObjectFileProcessor() = delete;
19 
20  DLLEXPORT static void Initialize();
21  DLLEXPORT static void Release();
22 
24  DLLEXPORT static std::unique_ptr<ObjectFile> ProcessObjectFile(const std::string &file,
25  LErrorReporter* reporterror);
26 
27  static DLLEXPORT std::unique_ptr<ObjectFile> ProcessObjectFileFromString(
28  const std::string &filecontents, const std::string &filenameforerrors,
29  LErrorReporter* reporterror);
30 
31 
37  DLLEXPORT static bool WriteObjectFile(ObjectFile &data, const std::string &file,
38  LErrorReporter* reporterror);
39 
45  DLLEXPORT static bool SerializeObjectFile(ObjectFile &data, std::string &receiver);
46 
49  DLLEXPORT static void RegisterValue(const std::string &name, VariableBlock* valuetokeep);
50 
51  // Utility functions //
52 
53 
54  // function to shorten value loading in many places //
55  template<class T>
56  static bool LoadValueFromNamedVars(NamedVars* block, const std::string &varname,
57  T &receiver, const T &defaultvalue, LErrorReporter* ReportError = nullptr,
58  const std::string &errorprefix = "")
59  {
60  // try to get value and convert to receiver //
61  if(!block->GetValueAndConvertTo<T>(varname, receiver)){
62  // variable not found / wrong type //
63  // report error if wanted //
64  if(ReportError)
65  ReportError->Error(errorprefix+" invalid variable "+varname+
66  ", not found/wrong type");
67 
68  // set value to provided default //
69  receiver = defaultvalue;
70 
71  return false;
72  }
73  return true;
74  }
75 
76  // function to call the one before //
77  template<class T>
79  const std::string &varname, T &receiver, const T &defaultvalue,
80  LErrorReporter* ReportError = nullptr, const std::string &errorprefix = "")
81  {
82  return LoadValueFromNamedVars<T>(&block, varname, receiver, defaultvalue,
83  ReportError, errorprefix);
84  }
85 
86 
87  template<class RType, class SingleType, int VarCount>
89  const std::string &varname, RType &receiver, const RType &defaultvalue,
90  LErrorReporter* ReportError = nullptr, const std::string &errorprefix = "")
91  {
92  // get pointer to value list //
93  std::shared_ptr<NamedVariableList> curvalues = block->GetValueDirect(varname);
94 
95  if(curvalues.get() == NULL){
96 
97  // not found //
98  if(ReportError)
99  ReportError->Error(errorprefix+" invalid variable "+varname+", not found");
100 
101  // set as default //
102  receiver = defaultvalue;
103  return;
104  }
105 
106  // call assigning function //
107  int varindex = 0;
108  LoadMultiPartValueFromNamedVariableList<RType, SingleType, VarCount>(curvalues.get(),
109  varindex, receiver, defaultvalue, ReportError, errorprefix);
110 
111  }
112 
113  template<class RType, class SingleType, int VarCount>
115  int &valuestartindex, RType &receiver, const RType &defaultvalue,
116  LErrorReporter* ReportError = nullptr, const std::string &errorprefix = "")
117  {
118  // make sure that size is right and types are correct //
119  if(block->GetVariableCount()-valuestartindex < VarCount ||
120  !block->CanAllBeCastedToType<SingleType>(
121  valuestartindex, valuestartindex+VarCount-1))
122  {
123  // not enough values / wrong types //
124  if(ReportError){
125  ReportError->Error(errorprefix+" invalid variable "+block->GetName()+
126  ", not enough values ("+Convert::ToString<int>(VarCount)+
127  " needed) or wrong types");
128  }
129  // set as default //
130  receiver = defaultvalue;
131  return false;
132  }
133 
134  // iterate over how many are wanted and assign //
135  for(int i = 0; i < VarCount; i++){
136 
137  // convert and set //
138  receiver[i] = (SingleType)block->GetValue(valuestartindex+i);
139  }
140 
141  // values copied //
142  // increment the index before returning //
143  valuestartindex += VarCount;
144 
145  return true;
146  }
147 
148  // function to call the one before //
149  template<class RType, class SingleType, int VarCount>
151  const std::string &varname, RType &receiver, const RType &defaultvalue,
152  LErrorReporter* ReportError = nullptr, const std::string &errorprefix = "")
153  {
154  return LoadMultiPartValueFromNamedVars<RType, SingleType, VarCount>(&block,
155  varname, receiver, defaultvalue, ReportError, errorprefix);
156  }
157 
158 
160  template<class T>
162  const std::string &varname,
163  std::vector<std::unique_ptr<T>> &receiver, size_t mustbedivisableby = 1,
164  LErrorReporter* ReportError = nullptr, const std::string &errorprefix = "")
165  {
166  NamedVariableList* inanimlist = block.GetValueDirectRaw(varname);
167 
168  if(inanimlist && inanimlist->GetVariableCount() && inanimlist->GetVariableCount() %
169  mustbedivisableby == 0 &&
170  inanimlist->CanAllBeCastedToType<T>())
171  {
172 
173  receiver.reserve(inanimlist->GetVariableCount());
174 
175  for(size_t i = 0; i < inanimlist->GetVariableCount(); i++){
176 
177  const T tmpresult =
178  inanimlist->GetValueDirect(i)->ConvertAndReturnVariable<T>();
179 
180  receiver.push_back(std::move(std::unique_ptr<T>(new T(tmpresult))));
181  }
182 
183  return true;
184  }
185 
186  // Something isn't right //
187  if(ReportError)
188  ReportError->Error(errorprefix+" invalid variable list "+varname+
189  ", not found/wrong type/wrong amount");
190  return false;
191  }
192 
193 private:
194 
196  static std::shared_ptr<NamedVariableList> TryToLoadNamedVariables(const std::string &file,
197  StringIterator &itr, const std::string &preceeding, LErrorReporter* reporterror);
198 
200  static bool TryToHandleTemplate(const std::string &file, StringIterator &itr,
201  ObjectFile &obj, const std::string &preceeding, LErrorReporter* reporterror);
202 
204  static std::shared_ptr<ObjectFileObject> TryToLoadObject(const std::string &file,
205  StringIterator &itr, ObjectFile &obj, const std::string &preceeding,
206  LErrorReporter* reporterror);
207 
208 
209  // Object loading split into parts //
211  static bool TryToLoadVariableList(const std::string &file, StringIterator &itr,
212  ObjectFileObject &obj, size_t startline, LErrorReporter* reporterror);
213 
214 
216  static bool TryToLoadTextBlock(const std::string &file, StringIterator &itr,
217  ObjectFileObject &obj, size_t startline, LErrorReporter* reporterror);
218 
219 
221  static bool TryToLoadScriptBlock(const std::string &file, StringIterator &itr,
222  ObjectFileObject &obj, size_t startline, LErrorReporter* reporterror);
223 
224  static std::map<std::string, std::shared_ptr<VariableBlock>> RegisteredValues;
225 };
226 
227 }
228 
hosts one or more VariableBlocks keeping only one name for all of them
Definition: NamedVars.h:27
bool GetValueAndConvertTo(const std::string &name, T &receiver) const
Definition: NamedVars.h:261
DLLEXPORT std::shared_ptr< NamedVariableList > GetValueDirect(const std::string &name) const
Definition: NamedVars.cpp:1084
static DLLEXPORT std::unique_ptr< ObjectFile > ProcessObjectFileFromString(const std::string &filecontents, const std::string &filenameforerrors, LErrorReporter *reporterror)
static DLLEXPORT void RegisterValue(const std::string &name, VariableBlock *valuetokeep)
Registers a new value alias for the processor.
static bool LoadValueFromNamedVars(NamedVars *block, const std::string &varname, T &receiver, const T &defaultvalue, LErrorReporter *ReportError=nullptr, const std::string &errorprefix="")
Non-template class for working with all types of DataBlocks.
Definition: DataBlock.h:425
static FORCE_INLINE bool LoadValueFromNamedVars(NamedVars &block, const std::string &varname, T &receiver, const T &defaultvalue, LErrorReporter *ReportError=nullptr, const std::string &errorprefix="")
DLLEXPORT VariableBlock & GetValue()
Definition: NamedVars.cpp:462
static DLLEXPORT std::unique_ptr< ObjectFile > ProcessObjectFile(const std::string &file, LErrorReporter *reporterror)
Reads an ObjectFile to an in-memory data structure.
bool CanAllBeCastedToType() const
Definition: NamedVars.h:103
ConvertT ConvertAndReturnVariable() const
Definition: DataBlock.h:743
static DLLEXPORT bool SerializeObjectFile(ObjectFile &data, std::string &receiver)
Serializes an ObjectFile object into a string.
#define FORCE_INLINE
Definition: Include.h:126
static DLLEXPORT bool WriteObjectFile(ObjectFile &data, const std::string &file, LErrorReporter *reporterror)
Writes an ObjectFile&#39;s data structure to a file.
DLLEXPORT size_t GetVariableCount() const
Definition: NamedVars.cpp:733
DLLEXPORT VariableBlock * GetValueDirect()
Definition: NamedVars.cpp:718
Iterator class for getting parts of a string.
Static class for handling ObjectFiles.
static bool LoadVectorOfTypeUPtrFromNamedVars(NamedVars &block, const std::string &varname, std::vector< std::unique_ptr< T >> &receiver, size_t mustbedivisableby=1, LErrorReporter *ReportError=nullptr, const std::string &errorprefix="")
Loads a vector of type from a NamedVars.
static void LoadMultiPartValueFromNamedVars(NamedVars *block, const std::string &varname, RType &receiver, const RType &defaultvalue, LErrorReporter *ReportError=nullptr, const std::string &errorprefix="")
static DLLEXPORT void Initialize()
static FORCE_INLINE void LoadMultiPartValueFromNamedVars(NamedVars &block, const std::string &varname, RType &receiver, const RType &defaultvalue, LErrorReporter *ReportError=nullptr, const std::string &errorprefix="")
static bool LoadMultiPartValueFromNamedVariableList(NamedVariableList *block, int &valuestartindex, RType &receiver, const RType &defaultvalue, LErrorReporter *ReportError=nullptr, const std::string &errorprefix="")
Interface for object file objects to implement.
Definition: ObjectFile.h:122
DLLEXPORT NamedVariableList * GetValueDirectRaw(const std::string &name) const
Definition: NamedVars.cpp:1096
#define DLLEXPORT
Definition: Include.h:115
The access mask controls which registered functions and classes a script sees.
Definition: GameModule.h:12
std::string GetName() const
Definition: NamedVars.h:139