Leviathan  0.8.0.0
Leviathan game engine
Leviathan::ObjectFileTemplateDefinition Class Reference

Class that represents a template definition. More...

#include <ObjectFile.h>

Public Member Functions

DLLEXPORT ObjectFileTemplateDefinition (const std::string &name, std::vector< std::unique_ptr< std::string >> &parameters, std::shared_ptr< ObjectFileObject > obj)
 Creates a ObjectFileTemplateDefinition. More...
 
DLLEXPORT const std::string & GetName () const
 Gets the name of this template. More...
 
DLLEXPORT std::unique_ptr< ObjectFileTemplateObjectCreateInstanceFromThis (const ObjectFileTemplateInstance &instanceargs, LErrorReporter *reporterror=nullptr)
 Creates an instance from this template. More...
 
DLLEXPORT std::string Serialize () const
 

Static Public Member Functions

static DLLEXPORT std::shared_ptr< ObjectFileTemplateDefinitionCreateFromObject (const std::string &name, std::shared_ptr< ObjectFileObject > obj, std::vector< std::unique_ptr< std::string >> &templateargs)
 Creates a ObjectFileTemplateDefinition from an ObjectFileObject and a parameter list. More...
 

Protected Member Functions

void ReplaceStringWithTemplateArguments (std::string &target, const std::vector< std::unique_ptr< std::string >> &args)
 
std::string ReplaceStringTemplateArguments (const std::string &target, const std::vector< std::unique_ptr< std::string >> &args)
 

Protected Attributes

std::string Name
 Name of this template. More...
 
std::vector< std::unique_ptr< std::string > > Parameters
 Template parameter definitions. More...
 
std::shared_ptr< ObjectFileObjectRepresentingObject
 The object from which the instances are created. More...
 

Detailed Description

Class that represents a template definition.

Todo:

Potentially allow changing the definition to update instantiations

Make this more robust and nice and reduce the bloat in the implementation

Definition at line 310 of file ObjectFile.h.

Constructor & Destructor Documentation

◆ ObjectFileTemplateDefinition()

DLLEXPORT Leviathan::ObjectFileTemplateDefinition::ObjectFileTemplateDefinition ( const std::string &  name,
std::vector< std::unique_ptr< std::string >> &  parameters,
std::shared_ptr< ObjectFileObject obj 
)

Creates a ObjectFileTemplateDefinition.

Warning
CreateFromObject might change in the future so please don't use this function directly

Definition at line 512 of file ObjectFile.cpp.

513  :
514  Name(name), Parameters(move(parameters)), RepresentingObject(obj) {
515 
516 }
std::shared_ptr< ObjectFileObject > RepresentingObject
The object from which the instances are created.
Definition: ObjectFile.h:356
std::vector< std::unique_ptr< std::string > > Parameters
Template parameter definitions.
Definition: ObjectFile.h:353
std::string Name
Name of this template.
Definition: ObjectFile.h:350

Member Function Documentation

◆ CreateFromObject()

DLLEXPORT std::shared_ptr< ObjectFileTemplateDefinition > Leviathan::ObjectFileTemplateDefinition::CreateFromObject ( const std::string &  name,
std::shared_ptr< ObjectFileObject obj,
std::vector< std::unique_ptr< std::string >> &  templateargs 
)
static

Creates a ObjectFileTemplateDefinition from an ObjectFileObject and a parameter list.

Parameters
objThe object from which to construct the template, the pointer will be deleted by this

Definition at line 523 of file ObjectFile.cpp.

524  {
525  // This could be changed to a function that tears down the object and creates mess
526  // of templating objects
527  auto resultobj = make_shared<ObjectFileTemplateDefinition>(name, templateargs, obj);
528 
529  return resultobj;
530 }

◆ CreateInstanceFromThis()

DLLEXPORT std::unique_ptr< Leviathan::ObjectFileTemplateObject > ObjectFileTemplateDefinition::CreateInstanceFromThis ( const ObjectFileTemplateInstance instanceargs,
LErrorReporter reporterror = nullptr 
)

Creates an instance from this template.

Todo:

Refactor this function to be smaller

Allow objects to use the special defined values in the ObjectFileProcessor

Definition at line 557 of file ObjectFile.cpp.

558  {
559  // First make sure that template counts match, return NULL otherwise //
560  if (Parameters.size() != instanceargs.Arguments.size()) {
561 
562  return NULL;
563  }
564 
565 
566  // Make sure these are not templated //
567  std::string newname = RepresentingObject->GetName();
568  std::string newtype = RepresentingObject->GetTypeName();
569 
570  std::vector<std::unique_ptr<std::string>> newprefixes;
571 
572  ReplaceStringWithTemplateArguments(newname, instanceargs.Arguments);
573 
574 
575  ReplaceStringWithTemplateArguments(newtype, instanceargs.Arguments);
576 
577  for (size_t i = 0; i < RepresentingObject->GetPrefixesCount(); i++) {
578 
579  unique_ptr<std::string> newprefix(new std::string(RepresentingObject->GetPrefix(i)));
580 
581  ReplaceStringWithTemplateArguments(*newprefix, instanceargs.Arguments);
582 
583  newprefixes.push_back(std::move(newprefix));
584  }
585 
586 
587  // We somehow need to detect all places that might have the template parameters and
588  // change them
589  unique_ptr<ObjectFileTemplateObject> internalobj(new ObjectFileTemplateObject(newname,
590  newtype, std::move(newprefixes)));
591 
592  // Do the contents the same way //
593  for (size_t i = 0; i < RepresentingObject->GetListCount(); i++) {
594 
595  // Copy the data from the list replacing all the templates with values //
596  ObjectFileList* curlist = RepresentingObject->GetList(i);
597 
598  std::string actlistname = curlist->GetName();
599 
600  ReplaceStringWithTemplateArguments(actlistname, instanceargs.Arguments);
601 
602  unique_ptr<ObjectFileList> listobj(new ObjectFileListProper(actlistname));
603 
604  const std::vector<shared_ptr<NamedVariableList>>& vallist =
605  *curlist->GetVariables().GetVec();
606 
607  // Add the values replacing template arguments //
608  for (size_t a = 0; a < vallist.size(); a++) {
609 
610  // First check the name //
611  std::string newvarlistname = vallist[a]->GetName();
612 
613  ReplaceStringWithTemplateArguments(newvarlistname, instanceargs.Arguments);
614 
615  vector<VariableBlock*> newvaluesforthing;
616  newvaluesforthing.reserve(vallist[a]->GetVariableCount());
617 
618  // Try to replace values that are strings //
619  for (size_t b = 0; b < vallist[a]->GetVariableCount(); b++) {
620 
621  VariableBlock* processblock = vallist[a]->GetValueDirect(b);
622 
623  auto typval = processblock->GetBlock()->Type;
624 
625  // Non-string types don't need replacing //
626  if (typval != DATABLOCK_TYPE_STRING && typval != DATABLOCK_TYPE_WSTRING) {
627 
628  newvaluesforthing.push_back(new
629  VariableBlock(processblock->GetBlock()->AllocateNewFromThis()));
630 
631  } else {
632 
633  // Try replacing stuff //
634  std::string valstring;
635 
636  if (!processblock->ConvertAndAssingToVariable<std::string>(valstring)) {
637 
638  DEBUG_BREAK;
639  }
640 
641  // Try to replace //
642  ReplaceStringWithTemplateArguments(valstring, instanceargs.Arguments);
643 
644  // Parse the result into a variable block //
645  try {
646 
647  unique_ptr<VariableBlock> tmpblock(new VariableBlock(valstring, NULL));
648 
649  newvaluesforthing.push_back(tmpblock.release());
650 
651  }
652  catch (const InvalidArgument &e) {
653 
654  if (reporterror) {
655 
656  reporterror->Warning("ObjectFileTemplates: a templated list has an "
657  "invalid value to parse, using empty string instead:");
658  e.Print(reporterror);
659  }
660 
661  newvaluesforthing.push_back(new VariableBlock(new StringBlock(
662  new std::string())));
663  continue;
664  }
665  }
666  }
667 
668  // Add the variable list //
669  listobj->AddVariable(shared_ptr<NamedVariableList>(new
670  NamedVariableList(newvarlistname, newvaluesforthing)));
671  }
672 
673  // Add the list to the new object //
674  internalobj->AddVariableList(std::move(listobj));
675 
676  }
677 
678  // Process the text blocks //
679  for (size_t i = 0; i < RepresentingObject->GetTextBlockCount(); i++) {
680 
681  // Copy the data from the list replacing all the templates with values //
682  ObjectFileTextBlock* curblock = RepresentingObject->GetTextBlock(i);
683 
684  std::string actblockname = curblock->GetName();
685 
686  ReplaceStringWithTemplateArguments(actblockname, instanceargs.Arguments);
687 
688  unique_ptr<ObjectFileTextBlock> textblock(new ObjectFileTextBlockProper(actblockname));
689 
690  // Replace all the lines //
691 
692 
693  for (size_t a = 0; a < curblock->GetLineCount(); a++) {
694 
695  std::string curline = curblock->GetLine(a);
696 
697  ReplaceStringWithTemplateArguments(curline, instanceargs.Arguments);
698 
699  textblock->AddTextLine(curline);
700  }
701 
702  // Add to the object //
703  internalobj->AddTextBlock(std::move(textblock));
704  }
705 
706  // Process the script source //
707 
708 
709  // Only the first segment should be used //
710 #ifdef LEVIATHAN_USING_ANGELSCRIPT
711  auto scrptwrap = RepresentingObject->GetScript();
712  if (scrptwrap) {
713 
714  auto scrptmodule = scrptwrap->GetModuleSafe();
715 
716  if (scrptmodule && scrptmodule->GetScriptSegmentCount() > 0) {
717 
718  // We can try to specialize the code //
719  auto onlysegment = scrptmodule->GetScriptSegment(0);
720 
721  const string& resultingsource = *onlysegment->SourceCode;
722 
723  const string& newsource = ReplaceStringTemplateArguments(resultingsource,
724  instanceargs.Arguments);
725 
726  std::string newname = scrptmodule->GetName();
727 
728  ReplaceStringWithTemplateArguments(newname, instanceargs.Arguments);
729 
730  shared_ptr<ScriptScript> newscript(new
731  ScriptScript(ScriptExecutor::Get()->CreateNewModule(newname,
732  scrptmodule->GetSource())));
733 
734  // Add the new script to the object //
735  shared_ptr<ScriptSourceFileData> newsourcesegment(new
736  ScriptSourceFileData(onlysegment->SourceFile, onlysegment->StartLine, newsource));
737 
738  auto newmod = newscript->GetModuleSafe();
739 
740  newmod->AddScriptSegment(newsourcesegment);
741  newmod->SetBuildState(SCRIPTBUILDSTATE_READYTOBUILD);
742 
743  internalobj->AddScriptScript(newscript);
744  }
745  }
746 #endif // LEVIATHAN_USING_ANGELSCRIPT
747 
748  // Return the object //
749  return internalobj;
750 }
Interface for object file lists to implement.
Definition: ObjectFile.h:21
std::vector< std::unique_ptr< std::string > > Arguments
Template arguments.
Definition: ObjectFile.h:302
hosts one or more VariableBlocks keeping only one name for all of them
Definition: NamedVars.h:27
virtual DLLEXPORT size_t GetLineCount() const =0
Returns the number of text lines.
Non-template class for working with all types of DataBlocks.
Definition: DataBlock.h:425
virtual void Warning(const std::string &Text)=0
Represents a section of script source file.
Definition: ScriptModule.h:71
std::shared_ptr< ObjectFileObject > RepresentingObject
The object from which the instances are created.
Definition: ObjectFile.h:356
Implementation of ObjectFileList.
Definition: ObjectFile.h:49
Implementation of ObjectFileTextBlock.
Definition: ObjectFile.h:99
Interface for object file text blocks to implement.
Definition: ObjectFile.h:71
static DLLEXPORT ScriptExecutor * Get()
#define DATABLOCK_TYPE_WSTRING
Definition: DataBlock.h:25
virtual DLLEXPORT const std::string & GetLine(size_t index) const =0
Gets a line from index ExceptionInvalidArgument when the index is out of bounds. ...
virtual DLLEXPORT void Print(LErrorReporter *errorreport) const noexcept
Definition: Exceptions.cpp:41
bool ConvertAndAssingToVariable(ConvertT &var) const
Definition: DataBlock.h:728
#define DATABLOCK_TYPE_STRING
Definition: DataBlock.h:26
virtual DLLEXPORT DataBlockAll * AllocateNewFromThis() const =0
DataBlockAll * GetBlock()
Definition: DataBlock.h:499
std::vector< std::unique_ptr< std::string > > Parameters
Template parameter definitions.
Definition: ObjectFile.h:353
Class that represents an object created from a template.
Definition: ObjectFile.h:261
std::string ReplaceStringTemplateArguments(const std::string &target, const std::vector< std::unique_ptr< std::string >> &args)
Definition: ObjectFile.cpp:764
virtual DLLEXPORT NamedVars & GetVariables()=0
Gets a reference to the underlying variables.
DataBlock< std::string > StringBlock
Definition: DataBlock.h:386
virtual DLLEXPORT const std::string & GetName() const =0
Gets the name of this list.
DLLEXPORT std::vector< std::shared_ptr< NamedVariableList > > * GetVec()
Definition: NamedVars.cpp:1246
virtual DLLEXPORT const std::string & GetName() const =0
Gets the name of this text block.
void ReplaceStringWithTemplateArguments(std::string &target, const std::vector< std::unique_ptr< std::string >> &args)
Definition: ObjectFile.cpp:752

◆ GetName()

DLLEXPORT const string & Leviathan::ObjectFileTemplateDefinition::GetName ( ) const

Gets the name of this template.

Definition at line 518 of file ObjectFile.cpp.

518  {
519  return Name;
520 }
std::string Name
Name of this template.
Definition: ObjectFile.h:350

◆ ReplaceStringTemplateArguments()

std::string Leviathan::ObjectFileTemplateDefinition::ReplaceStringTemplateArguments ( const std::string &  target,
const std::vector< std::unique_ptr< std::string >> &  args 
)
protected

Definition at line 764 of file ObjectFile.cpp.

765  {
766 
767  LEVIATHAN_ASSERT(Parameters.size() == args.size(), "ReplaceStringTemplateArguments arguments size mismatch");
768 
769  string result;
770 
771  if (Parameters.size() == 0)
772  return target;
773 
774  result = StringOperations::Replace<string>(target, *Parameters[0], *args[0]);
775 
776  // Look for strings matching our parameters //
777  for (size_t i = 1; i < Parameters.size(); i++) {
778 
779  result = StringOperations::Replace<string>(result, *Parameters[i], *args[i]);
780  }
781 
782  return result;
783 }
std::vector< std::unique_ptr< std::string > > Parameters
Template parameter definitions.
Definition: ObjectFile.h:353

◆ ReplaceStringWithTemplateArguments()

void Leviathan::ObjectFileTemplateDefinition::ReplaceStringWithTemplateArguments ( std::string &  target,
const std::vector< std::unique_ptr< std::string >> &  args 
)
protected

Definition at line 752 of file ObjectFile.cpp.

753  {
754 
755  LEVIATHAN_ASSERT(Parameters.size() == args.size(), "ReplaceStringWithTemplateArguments arguments size mismatch");
756 
757  // Look for strings matching our parameters //
758  for (size_t i = 0; i < Parameters.size(); i++) {
759 
760  target = StringOperations::Replace<std::string>(target, *Parameters[i], *args[i]);
761  }
762 }
std::vector< std::unique_ptr< std::string > > Parameters
Template parameter definitions.
Definition: ObjectFile.h:353

◆ Serialize()

DLLEXPORT std::string Leviathan::ObjectFileTemplateDefinition::Serialize ( ) const
Returns
A string representation of this object

Definition at line 532 of file ObjectFile.cpp.

532  {
533 
534  std::string result = "template<";
535 
536  bool first = true;
537 
538  // Parameter names for the template //
539  for (const auto& paramstr : Parameters) {
540 
541  if (!first)
542  result += ", ";
543 
544  result += *paramstr;
545  first = false;
546  }
547 
548  result += "> " + Name + ": \n";
549 
550  // The object associated with the template //
551  result += RepresentingObject->Serialize(4);
552 
553  return result;
554 
555 }
std::shared_ptr< ObjectFileObject > RepresentingObject
The object from which the instances are created.
Definition: ObjectFile.h:356
std::vector< std::unique_ptr< std::string > > Parameters
Template parameter definitions.
Definition: ObjectFile.h:353
std::string Name
Name of this template.
Definition: ObjectFile.h:350

Member Data Documentation

◆ Name

std::string Leviathan::ObjectFileTemplateDefinition::Name
protected

Name of this template.

Definition at line 350 of file ObjectFile.h.

◆ Parameters

std::vector<std::unique_ptr<std::string> > Leviathan::ObjectFileTemplateDefinition::Parameters
protected

Template parameter definitions.

Definition at line 353 of file ObjectFile.h.

◆ RepresentingObject

std::shared_ptr<ObjectFileObject> Leviathan::ObjectFileTemplateDefinition::RepresentingObject
protected

The object from which the instances are created.

Definition at line 356 of file ObjectFile.h.


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