Leviathan  0.8.0.0
Leviathan game engine
Leviathan::ObjectFile Class Reference

#include <ObjectFile.h>

Public Member Functions

DLLEXPORT ObjectFile ()
 
DLLEXPORT ObjectFile (NamedVars &stealfrom)
 Constructs a ObjectFile with predefined variables. More...
 
DLLEXPORT ~ObjectFile ()
 
DLLEXPORT bool AddNamedVariable (std::shared_ptr< NamedVariableList > var)
 Adds a NamedVariableList to this file. More...
 
DLLEXPORT FORCE_INLINE bool AddNamedVariable (NamedVariableList *var)
 Adds a NamedVariableList to this file. More...
 
DLLEXPORT bool AddObject (std::shared_ptr< ObjectFileObject > obj)
 Adds a ObjectFileObject to this file. More...
 
DLLEXPORT void AddTemplateInstance (std::shared_ptr< ObjectFileTemplateInstance > tiobj)
 Adds a ObjectFileTemplateInstance to this file. More...
 
DLLEXPORT bool AddTemplate (std::shared_ptr< ObjectFileTemplateDefinition > templatedef)
 Adds a ObjectFileTemplateInstance to this file. More...
 
DLLEXPORT FORCE_INLINE bool AddObject (ObjectFileObject *obj)
 Adds a ObjectFileObject to this file. More...
 
DLLEXPORT NamedVarsGetVariables ()
 Returns a raw pointer to HeaderVars. More...
 
DLLEXPORT size_t GetTotalObjectCount () const
 Gets the total number of objects (objects + template instances) More...
 
DLLEXPORT ObjectFileObjectGetObjectFromIndex (size_t index) const
 Gets an ObjectFileObject from an index ExceptionInvalidArgument when the index is out of bounds. More...
 
DLLEXPORT ObjectFileObjectGetObjectWithType (const std::string &typestr) const
 Gets an ObjectFileObject matching the type name. More...
 
DLLEXPORT std::vector< ObjectFileObject * > GetAllObjectsWithType (const std::string &type) const
 Gets all ObjectFileObjects that have the type. More...
 
DLLEXPORT bool IsObjectNameInUse (const std::string &name) const
 Checks whether the given name is in use. More...
 
DLLEXPORT bool GenerateTemplatedObjects (LErrorReporter *reporterror)
 Instantiates all all templates to actual objects. More...
 
DLLEXPORT std::shared_ptr< ObjectFileTemplateDefinitionFindTemplateDefinition (const std::string &name) const
 Finds the template definition matching the name. More...
 
DLLEXPORT size_t GetTemplateDefinitionCount () const
 
DLLEXPORT std::shared_ptr< ObjectFileTemplateDefinitionGetTemplateDefinition (size_t index)
 
DLLEXPORT std::shared_ptr< ObjectFileObjectGetObject (size_t index)
 
DLLEXPORT size_t GetTemplateInstanceCount () const
 
DLLEXPORT std::shared_ptr< ObjectFileTemplateInstanceGetTemplateInstance (size_t index)
 

Protected Attributes

std::vector< std::shared_ptr< ObjectFileObject > > DefinedObjects
 Holds the defined objects. More...
 
std::vector< std::shared_ptr< ObjectFileTemplateDefinition > > TemplateDefinitions
 Holds all the template definitions. More...
 
std::vector< std::shared_ptr< ObjectFileTemplateInstance > > TemplateInstantiations
 Holds all objects that are template instantiations. More...
 
NamedVars HeaderVars
 Holds all the named variables that were in the file. More...
 

Detailed Description

Defines the main data structure for an ObjectFile

Warning
The user is responsible for locking this class if multi threaded access is desired

Definition at line 364 of file ObjectFile.h.

Constructor & Destructor Documentation

◆ ObjectFile() [1/2]

DLLEXPORT Leviathan::ObjectFile::ObjectFile ( )

Definition at line 23 of file ObjectFile.cpp.

23  {
24 
25 }

◆ ObjectFile() [2/2]

DLLEXPORT Leviathan::ObjectFile::ObjectFile ( NamedVars stealfrom)

Constructs a ObjectFile with predefined variables.

Note
The NamedVariables passed in will be empty after this

Definition at line 19 of file ObjectFile.cpp.

19  : HeaderVars(&stealfrom){
20 
21 }
NamedVars HeaderVars
Holds all the named variables that were in the file.
Definition: ObjectFile.h:501

◆ ~ObjectFile()

DLLEXPORT Leviathan::ObjectFile::~ObjectFile ( )

Definition at line 27 of file ObjectFile.cpp.

27  {
28 
29 }

Member Function Documentation

◆ AddNamedVariable() [1/2]

DLLEXPORT bool Leviathan::ObjectFile::AddNamedVariable ( std::shared_ptr< NamedVariableList var)

Adds a NamedVariableList to this file.

Returns
False when the variable is not added, only when the name is already used

◆ AddNamedVariable() [2/2]

DLLEXPORT FORCE_INLINE bool Leviathan::ObjectFile::AddNamedVariable ( NamedVariableList var)
inline

Adds a NamedVariableList to this file.

Note
This is a shorthand for AddNamedVariable defined above
Warning
The pointer given will be deleted by this
See also
AddNamedVariable

Definition at line 383 of file ObjectFile.h.

383  {
384  return AddNamedVariable(std::shared_ptr<NamedVariableList>(var));
385  }
DLLEXPORT bool AddNamedVariable(std::shared_ptr< NamedVariableList > var)
Adds a NamedVariableList to this file.

◆ AddObject() [1/2]

DLLEXPORT bool Leviathan::ObjectFile::AddObject ( std::shared_ptr< ObjectFileObject obj)

Adds a ObjectFileObject to this file.

Returns
True when properly added, false if the name collides
Todo:
Disallow adding templates with this function

◆ AddObject() [2/2]

DLLEXPORT FORCE_INLINE bool Leviathan::ObjectFile::AddObject ( ObjectFileObject obj)
inline

Adds a ObjectFileObject to this file.

Warning
The pointer will be deleted by this
See also
AddObject

Definition at line 404 of file ObjectFile.h.

404  {
405  return AddObject(std::shared_ptr<ObjectFileObject>(obj));
406  }
DLLEXPORT bool AddObject(std::shared_ptr< ObjectFileObject > obj)
Adds a ObjectFileObject to this file.

◆ AddTemplate()

DLLEXPORT bool Leviathan::ObjectFile::AddTemplate ( std::shared_ptr< ObjectFileTemplateDefinition templatedef)

Adds a ObjectFileTemplateInstance to this file.

Returns
True when properly added, false if the name collides

Definition at line 165 of file ObjectFile.cpp.

167 {
168  // Make sure the name is unique //
169  for(size_t i = 0; i < TemplateDefinitions.size(); i++){
170 
171  if(TemplateDefinitions[i]->GetName() == templatedef->GetName()){
172 
173  // Conflicting name //
174  return false;
175  }
176  }
177 
178  // Add it //
179  TemplateDefinitions.push_back(templatedef);
180  return true;
181 }
std::vector< std::shared_ptr< ObjectFileTemplateDefinition > > TemplateDefinitions
Holds all the template definitions.
Definition: ObjectFile.h:493

◆ AddTemplateInstance()

DLLEXPORT void Leviathan::ObjectFile::AddTemplateInstance ( std::shared_ptr< ObjectFileTemplateInstance tiobj)

Adds a ObjectFileTemplateInstance to this file.

Definition at line 158 of file ObjectFile.cpp.

160 {
161  // Template instances may have names that are not present yet, the instance can be before the definition //
162  TemplateInstantiations.push_back(tiobj);
163 }
std::vector< std::shared_ptr< ObjectFileTemplateInstance > > TemplateInstantiations
Holds all objects that are template instantiations.
Definition: ObjectFile.h:497

◆ FindTemplateDefinition()

DLLEXPORT std::shared_ptr< ObjectFileTemplateDefinition > Leviathan::ObjectFile::FindTemplateDefinition ( const std::string &  name) const

Finds the template definition matching the name.

Returns
The found object or NULL
Todo:
Allow template overloading with different number of parameters

Definition at line 183 of file ObjectFile.cpp.

185 {
186  // Try to find one with the exact name //
187  for(size_t i = 0; i < TemplateDefinitions.size(); i++){
188 
189  if(TemplateDefinitions[i]->GetName() == name){
190 
191  // Found a match //
192  return TemplateDefinitions[i];
193  }
194  }
195 
196  // Nothing found //
197  return NULL;
198 }
std::vector< std::shared_ptr< ObjectFileTemplateDefinition > > TemplateDefinitions
Holds all the template definitions.
Definition: ObjectFile.h:493

◆ GenerateTemplatedObjects()

DLLEXPORT bool Leviathan::ObjectFile::GenerateTemplatedObjects ( LErrorReporter reporterror)

Instantiates all all templates to actual objects.

Returns
True when all templates had a parent (all names were valid) and no errors occurred

Definition at line 114 of file ObjectFile.cpp.

115 {
116  // Create template instances from the templates //
117  for(size_t i = 0; i < TemplateInstantiations.size(); i++){
118 
119  // Find a template definition for this //
120  auto tmpldef = FindTemplateDefinition(TemplateInstantiations[i]->GetNameOfParentTemplate());
121 
122  if(!tmpldef){
123 
124  reporterror->Error("ObjectFile: could not find template definition with name: "
125  +TemplateInstantiations[i]->GetNameOfParentTemplate());
126  return false;
127  }
128 
129  // Try to generate it //
130  auto resultobj = tmpldef->CreateInstanceFromThis(*TemplateInstantiations[i], reporterror);
131 
132  if(!resultobj){
133 
134  reporterror->Error("ObjectFile: could not instantiate template "
135  "(parameter count probably didn't match), name: "
136  +TemplateInstantiations[i]->GetNameOfParentTemplate());
137  return false;
138  }
139 
140  shared_ptr<ObjectFileObject> tmpobj(resultobj.release());
141 
142  // Add it to us //
143  if(!AddObject(tmpobj)){
144 
145  reporterror->Error("ObjectFile: template instance's result was an object whose "
146  "name is already in use, template name: "
147  +TemplateInstantiations[i]->GetNameOfParentTemplate()+", result object: "+
148  tmpobj->GetName());
149  return false;
150  }
151  }
152 
153 
154  // No errors occurred //
155  return true;
156 }
virtual void Error(const std::string &Text)=0
DLLEXPORT std::shared_ptr< ObjectFileTemplateDefinition > FindTemplateDefinition(const std::string &name) const
Finds the template definition matching the name.
Definition: ObjectFile.cpp:183
std::vector< std::shared_ptr< ObjectFileTemplateInstance > > TemplateInstantiations
Holds all objects that are template instantiations.
Definition: ObjectFile.h:497
DLLEXPORT bool AddObject(std::shared_ptr< ObjectFileObject > obj)
Adds a ObjectFileObject to this file.

◆ GetAllObjectsWithType()

DLLEXPORT std::vector< ObjectFileObject * > Leviathan::ObjectFile::GetAllObjectsWithType ( const std::string &  type) const

Gets all ObjectFileObjects that have the type.

Definition at line 101 of file ObjectFile.cpp.

101  {
102 
103  std::vector<ObjectFileObject*> result;
104 
105  for (size_t i = 0; i < DefinedObjects.size(); i++) {
106 
107  if (DefinedObjects[i]->GetTypeName() == type)
108  result.push_back((DefinedObjects[i].get()));
109  }
110 
111  return result;
112 }
std::vector< std::shared_ptr< ObjectFileObject > > DefinedObjects
Holds the defined objects.
Definition: ObjectFile.h:490

◆ GetObject()

DLLEXPORT std::shared_ptr<ObjectFileObject> Leviathan::ObjectFile::GetObject ( size_t  index)
inline
Returns
object (that may be a template instance) definition if index is valid

Definition at line 464 of file ObjectFile.h.

464  {
465 
466  if (index >= DefinedObjects.size())
467  return nullptr;
468 
469  return DefinedObjects[index];
470  }
std::vector< std::shared_ptr< ObjectFileObject > > DefinedObjects
Holds the defined objects.
Definition: ObjectFile.h:490

◆ GetObjectFromIndex()

DLLEXPORT ObjectFileObject * Leviathan::ObjectFile::GetObjectFromIndex ( size_t  index) const

Gets an ObjectFileObject from an index ExceptionInvalidArgument when the index is out of bounds.

See also
GetTotalObjectCount

Definition at line 74 of file ObjectFile.cpp.

74  {
75  // Return from DefinedObjects if it is in range otherwise from the template instances //
76  if(index < DefinedObjects.size()){
77 
78  return DefinedObjects[index].get();
79  }
80 
81 #ifndef ALTERNATIVE_EXCEPTIONS_FATAL
82  // Invalid index //
83  throw InvalidArgument("index is out of range");
84 #else
85  return nullptr;
86 #endif //ALTERNATIVE_EXCEPTIONS_FATAL
87 }
std::vector< std::shared_ptr< ObjectFileObject > > DefinedObjects
Holds the defined objects.
Definition: ObjectFile.h:490

◆ GetObjectWithType()

DLLEXPORT ObjectFileObject * Leviathan::ObjectFile::GetObjectWithType ( const std::string &  typestr) const

Gets an ObjectFileObject matching the type name.

Note
Only the first object is returned matching the type
Todo:
Add a function which returns all that matched the type

Definition at line 89 of file ObjectFile.cpp.

89  {
90  // Find the first that matches the type //
91  for(size_t i = 0; i < DefinedObjects.size(); i++){
92 
93  if(DefinedObjects[i]->GetTypeName() == typestr)
94  return DefinedObjects[i].get();
95  }
96 
97  // Nothing found //
98  return NULL;
99 }
std::vector< std::shared_ptr< ObjectFileObject > > DefinedObjects
Holds the defined objects.
Definition: ObjectFile.h:490

◆ GetTemplateDefinition()

DLLEXPORT std::shared_ptr<ObjectFileTemplateDefinition> Leviathan::ObjectFile::GetTemplateDefinition ( size_t  index)
inline
Returns
Template definition if index is valid

Definition at line 455 of file ObjectFile.h.

455  {
456 
457  if (index >= TemplateDefinitions.size())
458  return nullptr;
459 
460  return TemplateDefinitions[index];
461  }
std::vector< std::shared_ptr< ObjectFileTemplateDefinition > > TemplateDefinitions
Holds all the template definitions.
Definition: ObjectFile.h:493

◆ GetTemplateDefinitionCount()

DLLEXPORT size_t Leviathan::ObjectFile::GetTemplateDefinitionCount ( ) const
inline
Returns
The number of template definitions there are available for GetTemplateDefinition

Definition at line 449 of file ObjectFile.h.

449  {
450 
451  return TemplateDefinitions.size();
452  }
std::vector< std::shared_ptr< ObjectFileTemplateDefinition > > TemplateDefinitions
Holds all the template definitions.
Definition: ObjectFile.h:493

◆ GetTemplateInstance()

DLLEXPORT std::shared_ptr<ObjectFileTemplateInstance> Leviathan::ObjectFile::GetTemplateInstance ( size_t  index)
inline
Returns
Template instance if index is valid

Definition at line 479 of file ObjectFile.h.

479  {
480 
481  if (index >= TemplateInstantiations.size())
482  return nullptr;
483 
484  return TemplateInstantiations[index];
485  }
std::vector< std::shared_ptr< ObjectFileTemplateInstance > > TemplateInstantiations
Holds all objects that are template instantiations.
Definition: ObjectFile.h:497

◆ GetTemplateInstanceCount()

DLLEXPORT size_t Leviathan::ObjectFile::GetTemplateInstanceCount ( ) const
inline
Returns
The number of template instances there are available for GetTemplateInstance

Definition at line 473 of file ObjectFile.h.

473  {
474 
475  return TemplateInstantiations.size();
476  }
std::vector< std::shared_ptr< ObjectFileTemplateInstance > > TemplateInstantiations
Holds all objects that are template instantiations.
Definition: ObjectFile.h:497

◆ GetTotalObjectCount()

DLLEXPORT size_t Leviathan::ObjectFile::GetTotalObjectCount ( ) const
inline

Gets the total number of objects (objects + template instances)

Definition at line 414 of file ObjectFile.h.

414  {
415  // Add the template objects to actual objects //
416  return DefinedObjects.size();
417  }
std::vector< std::shared_ptr< ObjectFileObject > > DefinedObjects
Holds the defined objects.
Definition: ObjectFile.h:490

◆ GetVariables()

DLLEXPORT NamedVars * Leviathan::ObjectFile::GetVariables ( )

Returns a raw pointer to HeaderVars.

Warning
You need to make sure you hold a reference to this to not delete the variables

Definition at line 70 of file ObjectFile.cpp.

70  {
71  return &HeaderVars;
72 }
NamedVars HeaderVars
Holds all the named variables that were in the file.
Definition: ObjectFile.h:501

◆ IsObjectNameInUse()

DLLEXPORT bool Leviathan::ObjectFile::IsObjectNameInUse ( const std::string &  name) const

Checks whether the given name is in use.

Todo:
Check template names

Definition at line 55 of file ObjectFile.cpp.

55  {
56  // Try to find an object with the same name //
57  for(size_t i = 0; i < DefinedObjects.size(); i++){
58 
59  if(DefinedObjects[i]->GetName() == name)
60  return true;
61  }
62 
63  // Check for matching template names //
64 
65 
66  // Didn't match anything //
67  return false;
68 }
std::vector< std::shared_ptr< ObjectFileObject > > DefinedObjects
Holds the defined objects.
Definition: ObjectFile.h:490

Member Data Documentation

◆ DefinedObjects

std::vector<std::shared_ptr<ObjectFileObject> > Leviathan::ObjectFile::DefinedObjects
protected

Holds the defined objects.

Definition at line 490 of file ObjectFile.h.

◆ HeaderVars

NamedVars Leviathan::ObjectFile::HeaderVars
protected

Holds all the named variables that were in the file.

Definition at line 501 of file ObjectFile.h.

◆ TemplateDefinitions

std::vector<std::shared_ptr<ObjectFileTemplateDefinition> > Leviathan::ObjectFile::TemplateDefinitions
protected

Holds all the template definitions.

Definition at line 493 of file ObjectFile.h.

◆ TemplateInstantiations

std::vector<std::shared_ptr<ObjectFileTemplateInstance> > Leviathan::ObjectFile::TemplateInstantiations
protected

Holds all objects that are template instantiations.

Definition at line 497 of file ObjectFile.h.


The documentation for this class was generated from the following files: