Created
February 3, 2013 20:05
-
-
Save Groogy/4703427 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Tyr/Configuration.hpp> | |
#include <cassert> | |
#include <cstring> | |
tyr::Configuration tyr::Configuration::getDefaultConfiguration() | |
{ | |
std::vector<tyr::Configuration> dimensionConfig; | |
dimensionConfig.push_back(tyr::Configuration(1024)); | |
dimensionConfig.push_back(tyr::Configuration(768)); | |
std::map<std::string, tyr::Configuration> windowConfig; | |
windowConfig["dimension"] = tyr::Configuration(dimensionConfig); | |
windowConfig["fullscreen"] = tyr::Configuration(false); | |
windowConfig["vsync"] = tyr::Configuration(false); | |
windowConfig["title"] = tyr::Configuration("Tyr"); | |
std::map<std::string, tyr::Configuration> rootConfig; | |
rootConfig["Window"] = tyr::Configuration(windowConfig); | |
return tyr::Configuration(rootConfig); | |
} | |
tyr::Configuration::Configuration() | |
: myType(Type::Null) | |
, myData() | |
{ | |
} | |
tyr::Configuration::Configuration(const Collection& children) | |
: myType(Type::Branch) | |
, myData(children) | |
{ | |
} | |
tyr::Configuration::Configuration(const Array& array) | |
: myType(Type::Array) | |
, myData(array) | |
{ | |
} | |
tyr::Configuration::Configuration(int value) | |
: myType(Type::Integer) | |
, myData(value) | |
{ | |
} | |
tyr::Configuration::Configuration(float value) | |
: myType(Type::Float) | |
, myData(value) | |
{ | |
} | |
tyr::Configuration::Configuration(bool value) | |
: myType(Type::Boolean) | |
, myData(value) | |
{ | |
} | |
tyr::Configuration::Configuration(String value) | |
: myType(Type::String) | |
, myData(value) | |
{ | |
} | |
tyr::Configuration::Configuration(const char* value) | |
: myType(Type::String) | |
, myData(std::string(value)) | |
{ | |
} | |
tyr::Configuration::Configuration(const Configuration& source) | |
: myType(source.myType) | |
, myData() | |
{ | |
switch(myType) | |
{ | |
case Type::String: | |
new (&myData.str) String(source.myData.str); | |
break; | |
case Type::Branch: | |
new (&myData.children) Collection(source.myData.children); | |
break; | |
case Type::Array: | |
new (&myData.array) Array(source.myData.array); | |
break; | |
case Type::Null: | |
break; | |
default: | |
std::memcpy(&myData, &source.myData, sizeof(DataUnion)); | |
} | |
} | |
tyr::Configuration::~Configuration() | |
{ | |
switch(myType) | |
{ | |
case Type::String: | |
myData.str.~String(); | |
break; | |
case Type::Branch: | |
myData.children.~Collection(); | |
break; | |
case Type::Array: | |
myData.array.~Array(); | |
break; | |
default: | |
break; | |
} | |
} | |
tyr::Configuration& tyr::Configuration::operator[](const std::string& name) | |
{ | |
assert(myType == Type::Branch); | |
auto iterator = myData.children.find(name); | |
assert(iterator != myData.children.end()); | |
return iterator->second; | |
} | |
const tyr::Configuration& tyr::Configuration::operator[](const std::string& name) const | |
{ | |
assert(myType == Type::Branch); | |
auto iterator = myData.children.find(name); | |
assert(iterator != myData.children.end()); | |
return iterator->second; | |
} | |
tyr::Configuration& tyr::Configuration::operator[](unsigned int index) | |
{ | |
assert(myType == Type::Array); | |
assert(index < myData.array.size()); | |
return myData.array[index]; | |
} | |
const tyr::Configuration& tyr::Configuration::operator[](unsigned int index) const | |
{ | |
assert(myType == Type::Array); | |
assert(index < myData.array.size()); | |
return myData.array[index]; | |
} | |
void tyr::Configuration::operator=(const tyr::Configuration& source) | |
{ | |
switch(myType) | |
{ | |
case Type::String: | |
myData.str.~String(); | |
break; | |
case Type::Branch: | |
myData.children.~Collection(); | |
break; | |
case Type::Array: | |
myData.array.~Array(); | |
break; | |
default: | |
break; | |
} | |
myType = source.myType; | |
switch(myType) | |
{ | |
case Type::String: | |
new (&myData.str) String(source.myData.str); | |
break; | |
case Type::Branch: | |
new (&myData.children) Collection(source.myData.children); | |
break; | |
case Type::Array: | |
new (&myData.array) Array(source.myData.array); | |
break; | |
case Type::Null: | |
break; | |
default: | |
std::memcpy(&myData, &source.myData, sizeof(DataUnion)); | |
} | |
} | |
tyr::Configuration::operator int() const | |
{ | |
assert(myType == Type::Integer); | |
return myData.integer; | |
} | |
tyr::Configuration::operator unsigned int() const | |
{ | |
assert(myType == Type::Integer); | |
return static_cast<unsigned int>(myData.integer); | |
} | |
tyr::Configuration::operator float() const | |
{ | |
assert(myType == Type::Float); | |
return myData.number; | |
} | |
tyr::Configuration::operator bool() const | |
{ | |
assert(myType == Type::Boolean); | |
return myData.boolean; | |
} | |
tyr::Configuration::operator String() const | |
{ | |
assert(myType == Type::String); | |
return myData.str; | |
} | |
tyr::Configuration::DataUnion::DataUnion() | |
{ | |
} | |
tyr::Configuration::DataUnion::DataUnion(const Collection& collection) | |
: children(collection) | |
{ | |
} | |
tyr::Configuration::DataUnion::DataUnion(const Array& array) | |
: array(array) | |
{ | |
} | |
tyr::Configuration::DataUnion::DataUnion(int value) | |
: integer(value) | |
{ | |
} | |
tyr::Configuration::DataUnion::DataUnion(float value) | |
: number(value) | |
{ | |
} | |
tyr::Configuration::DataUnion::DataUnion(bool value) | |
: boolean(value) | |
{ | |
} | |
tyr::Configuration::DataUnion::DataUnion(String value) | |
: str(value) | |
{ | |
} | |
tyr::Configuration::DataUnion::~DataUnion() | |
{ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment