Gorgon Game Engine
Array.h
Go to the documentation of this file.
1 #pragma once
2 
3 
4 #include <vector>
5 
6 #include "../../Any.h"
7 #include "../Reflection.h"
8 #include "../Exceptions.h"
9 
10 
11 namespace Gorgon {
12  namespace Scripting {
13 
14  class Array {
15  public:
17 
18  Array(const Array &arr) : type(arr.type), elements(arr.elements), Elements(elements) {}
19 
20 
21  Data GetItemData(unsigned index) const {
22  if(index>=elements.size()) {
23  throw OutofBoundsException(index, (long)elements.size(), "Array");
24  }
25  return elements[index];
26  }
27 
28  Data GetItemData(unsigned index) {
29  if(index>=elements.size()) {
30  throw OutofBoundsException(index, (long)elements.size(), "Array");
31  }
32 
33  return elements[index].GetReference();
34  }
35 
36  void SetItemData(unsigned index, Data data) {
37  if(index>=elements.size()) {
38  throw OutofBoundsException(index, (long)elements.size(), "Array");
39  }
40  if(type!=&data.GetType()) {
41  data=data.GetType().MorphTo(*type, data);
42  }
43 
44  elements[index]=data;
45  }
46 
47  template<class T_>
48  void PushWithoutCheck(const T_ &elm) {
49  elements.push_back({type, elm});
50  }
51 
52  void PushData(Data elm) {
53  if(elm.GetType()!=type) {
54  elm=elm.GetType().MorphTo(*type, elm);
55  }
56  elements.push_back(elm);
57  }
58 
59  void PushData(Any elm, bool ref=false, bool cnst=false) {
60  elements.push_back({type, elm, ref, cnst});
61  }
62 
64  if(!elements.size()) {
65  throw OutofBoundsException(0, (long)elements.size(), "Array");
66  }
67 
68  Data d=elements.back();
69  elements.pop_back();
70 
71  return d;
72  }
73 
74  void Resize(unsigned size) {
75  elements.resize(size, {type, type->GetDefaultValue()});
76  }
77 
78  unsigned GetSize() const {
79  return (unsigned)elements.size();
80  }
81 
82  const Type &GetType() const {
83  return *type;
84  }
85 
86  const std::vector<Data> &Elements;
87 
88  protected:
89  std::vector<Data> elements;
90 
91  const Type *type;
92  };
93 
94  Type *ArrayType();
95 
96  }
97 }
Gorgon::Scripting::Array::Array
Array(const Type &type)
Definition: Array.h:16
Gorgon::Scripting::Array::GetItemData
Data GetItemData(unsigned index) const
Definition: Array.h:21
Gorgon::Scripting::Array::PopData
Data PopData()
Definition: Array.h:63
Gorgon::Scripting::Array::Elements
const std::vector< Data > & Elements
Definition: Array.h:86
Gorgon::Scripting::ReferenceCounter::Register
void Register(const Data &data)
Registers a new object of reference counting, this will set reference count to one.
Definition: Runtime.h:24
Gorgon::Scripting::Type::AddMembers
virtual void AddMembers(std::initializer_list< StaticMember * > newmembers) override
Adds a list of static members to this type.
Definition: Reflection.h:1228
Gorgon::Any
This class can hold any other information providing type erasure.
Definition: Any.h:32
Gorgon::Scripting::VirtualMachine::References
ReferenceCounter References
This system allows objects of automatic lifetime.
Definition: VirtualMachine.h:222
Gorgon::Scripting::Array::GetType
const Type & GetType() const
Definition: Array.h:82
Gorgon::Scripting::Data
Data describes a piece of data.
Definition: Data.h:22
Gorgon::Scripting::Array::GetSize
unsigned GetSize() const
Definition: Array.h:78
Gorgon::Scripting::Array::Array
Array(const Array &arr)
Definition: Array.h:18
Gorgon::Scripting::VirtualMachine::Get
static VirtualMachine & Get()
Returns the current VM for this thread.
Definition: VirtualMachine.h:109
Gorgon::Scripting::MapFunctionToInstanceMember
InstanceMember * MapFunctionToInstanceMember(F_ reader, const std::string &name, const std::string &help, const Type *membertype, const Type *parenttype)
This function will map a const data returning function to a read-only, non-ref, const instance member...
Definition: Embedding.h:1147
Gorgon::Scripting::Data::GetType
const Type & GetType() const
Returns the type of the data.
Definition: Data.h:173
Gorgon::Scripting::Array::GetItemData
Data GetItemData(unsigned index)
Definition: Array.h:28
Gorgon::Scripting::Type
This class stores information about types.
Definition: Reflection.h:1165
Gorgon
Root namespace for Gorgon Game Engine.
Definition: Any.h:19
Gorgon::Scripting::Array::type
const Type * type
Definition: Array.h:91
Gorgon::Scripting::Type::AddMember
virtual void AddMember(StaticMember &member) override
Adds a static member to this type.
Definition: Reflection.h:1214
Gorgon::Scripting::Array::elements
std::vector< Data > elements
Definition: Array.h:89
Gorgon::Scripting::Array
Definition: Array.h:14
Gorgon::Scripting::Array::PushData
void PushData(Data elm)
Definition: Array.h:52
Gorgon::Scripting::ReferenceTag
@ ReferenceTag
Marks the object as a reference.
Definition: Reflection.h:61
Gorgon::Scripting::Type::GetDefaultValue
Any GetDefaultValue() const
Returns the value of the type.
Definition: Reflection.h:1322
Gorgon::Scripting::ConstTag
@ ConstTag
Marks a parameter or a function constant.
Definition: Reflection.h:95
Gorgon::Scripting::ArrayType
Type * ArrayType()
Definition: Array.cpp:74
Gorgon::Scripting::Array::PushData
void PushData(Any elm, bool ref=false, bool cnst=false)
Definition: Array.h:59
Gorgon::Scripting::Parameter
This class represents a function parameter description.
Definition: Reflection.h:137
Gorgon::Scripting::Array::SetItemData
void SetItemData(unsigned index, Data data)
Definition: Array.h:36
Gorgon::GL::UBOBindingPoint::Type
Type
Definition: Shader.h:14
Gorgon::Scripting::ArrayFunctions
std::vector< StaticMember * > ArrayFunctions()
Definition: Array.cpp:198
Gorgon::Scripting::Array::PushWithoutCheck
void PushWithoutCheck(const T_ &elm)
Definition: Array.h:48
Gorgon::Scripting::Array::Resize
void Resize(unsigned size)
Definition: Array.h:74
Gorgon::Scripting::MappedReferenceType
This class allows embedded types to become scripting types that are passed around as references.
Definition: Embedding.h:1406
Array.h
Gorgon::end
std::vector< T_ >::const_iterator end(enum_type_id< T_ >)
Definition: Enum.h:288
Gorgon::Scripting::MapFunction
Scripting::Function::Overload * MapFunction(F_ fn, const Type *returntype, ParameterList parameters, P_ ...tags)
Definition: Embedding.h:614
Gorgon::Scripting::Types::Function
const Scripting::Type & Function()
Definition: Reflection.h:589
Gorgon::Scripting::BuildArray
Array * BuildArray(const Type *type, std::vector< Data > datav)
Definition: Array.cpp:184
Gorgon::Scripting::Type::MorphTo
Data MorphTo(const Type &type, Data source, bool allowtypecast=true) const
Morphs the given data into the target type.
Definition: Reflection.cpp:123