SparrowSerializer/src/property.cpp

181 lines
9.6 KiB
C++

#include "property.h"
#include "serializable.h"
// INTEGER PROPERTY
template <> std::ostream& Property<int>::saveBinary(std::ostream& os) { return os.write((char*)&value, sizeof(int)); }
template <> std::ostream& Property<int>::saveAscii(std::ostream& os) { return os << value; }
template <> std::istream& Property<int>::loadBinary(std::istream& is) { return is.read((char*)&value, sizeof(int)); }
template <> std::istream& Property<int>::loadAscii(std::istream& is) { return is >> value; }
template <> AbstractProperty::PropertyType Property<int>::getStaticPropertyType() { return AbstractProperty::INTEGER; }
// FLOAT PROPERTY
template <> std::ostream& Property<float>::saveBinary(std::ostream& os) { return os.write((char*)&value, sizeof(float)); }
template <> std::ostream& Property<float>::saveAscii(std::ostream& os) { return os << value; }
template <> std::istream& Property<float>::loadBinary(std::istream& is) { return is.read((char*)&value, sizeof(float)); }
template <> std::istream& Property<float>::loadAscii(std::istream& is) { return is >> value; }
template <> AbstractProperty::PropertyType Property<float>::getStaticPropertyType() { return AbstractProperty::FLOAT; }
// BOOLEAN PROPERTY
template <> std::ostream& Property<bool>::saveBinary(std::ostream& os) { return os.write((char*)&value, sizeof(bool)); }
template <> std::ostream& Property<bool>::saveAscii(std::ostream& os) { return os << value; }
template <> std::istream& Property<bool>::loadBinary(std::istream& is) { return is.read((char*)&value, sizeof(bool)); }
template <> std::istream& Property<bool>::loadAscii(std::istream& is) { return is >> value; }
template <> AbstractProperty::PropertyType Property<bool>::getStaticPropertyType() { return AbstractProperty::BOOLEAN; }
// UNSIGNED INTEGER PROPERTY
template <> std::ostream& Property<unsigned int>::saveBinary(std::ostream& os) { return os.write((char*)&value, sizeof(unsigned int)); }
template <> std::ostream& Property<unsigned int>::saveAscii(std::ostream& os) { return os << value; }
template <> std::istream& Property<unsigned int>::loadBinary(std::istream& is) { return is.read((char*)&value, sizeof(unsigned int)); }
template <> std::istream& Property<unsigned int>::loadAscii(std::istream& is) { return is >> value; }
template <> AbstractProperty::PropertyType Property<unsigned int>::getStaticPropertyType() { return AbstractProperty::UNSIGNED_INTEGER; }
// BYTE PROPERTY
template <> std::ostream& Property<char>::saveBinary(std::ostream& os) { return os.write((char*)&value, sizeof(char)); }
template <> std::ostream& Property<char>::saveAscii(std::ostream& os) { return os << value; }
template <> std::istream& Property<char>::loadBinary(std::istream& is) { return is.read((char*)&value, sizeof(char)); }
template <> std::istream& Property<char>::loadAscii(std::istream& is) { return is >> value; }
template <> AbstractProperty::PropertyType Property<char>::getStaticPropertyType() { return AbstractProperty::BYTE; }
// SHORT PROPERTY
template <> std::ostream& Property<short>::saveBinary(std::ostream& os) { return os.write((char*)&value, sizeof(short)); }
template <> std::ostream& Property<short>::saveAscii(std::ostream& os) { return os << value; }
template <> std::istream& Property<short>::loadBinary(std::istream& is) { return is.read((char*)&value, sizeof(short)); }
template <> std::istream& Property<short>::loadAscii(std::istream& is) { return is >> value; }
template <> AbstractProperty::PropertyType Property<short>::getStaticPropertyType() { return AbstractProperty::SHORT; }
// LONG PROPERTY
template <> std::ostream& Property<long>::saveBinary(std::ostream& os) { return os.write((char*)&value, sizeof(long)); }
template <> std::ostream& Property<long>::saveAscii(std::ostream& os) { return os << value; }
template <> std::istream& Property<long>::loadBinary(std::istream& is) { return is.read((char*)&value, sizeof(long)); }
template <> std::istream& Property<long>::loadAscii(std::istream& is) { return is >> value; }
template <> AbstractProperty::PropertyType Property<long>::getStaticPropertyType() { return AbstractProperty::LONG; }
// DOUBLE PROPERTY
template <> std::ostream& Property<double>::saveBinary(std::ostream& os) { return os.write((char*)&value, sizeof(double)); }
template <> std::ostream& Property<double>::saveAscii(std::ostream& os) { return os << value; }
template <> std::istream& Property<double>::loadBinary(std::istream& is) { return is.read((char*)&value, sizeof(double)); }
template <> std::istream& Property<double>::loadAscii(std::istream& is) { return is >> value; }
template <> AbstractProperty::PropertyType Property<double>::getStaticPropertyType() { return AbstractProperty::DOUBLE; }
// VEC2 PROPERTY
template <> std::ostream& Property<glm::vec2>::saveBinary(std::ostream& os) { return os.write((char*)glm::value_ptr(value), sizeof(float)*value.length()); }
template <> std::ostream& Property<glm::vec2>::saveAscii(std::ostream& os) { os << "["; for(int i=0; i<value.length(); ++i) os << (glm::value_ptr(value)[i]) << (i == value.length()-1 ? "]" : ", "); return os; }
template <> std::istream& Property<glm::vec2>::loadBinary(std::istream& is) { return is.read((char*)glm::value_ptr(value), sizeof(float)*value.length()); }
template <> std::istream& Property<glm::vec2>::loadAscii(std::istream& is) { is.ignore(1); for(int i=0; i<value.length(); ++i) is >> (glm::value_ptr(value)[i]), is.ignore(1); return is; }
template <> AbstractProperty::PropertyType Property<glm::vec2>::getStaticPropertyType() { return AbstractProperty::VEC2; }
// VEC3 PROPERTY
template <> std::ostream& Property<glm::vec3>::saveBinary(std::ostream& os) { return os.write((char*)glm::value_ptr(value), sizeof(float)*value.length()); }
template <> std::ostream& Property<glm::vec3>::saveAscii(std::ostream& os) { os << "["; for(int i=0; i<value.length(); ++i) os << (glm::value_ptr(value)[i]) << (i == value.length()-1 ? "]" : ", "); return os; }
template <> std::istream& Property<glm::vec3>::loadBinary(std::istream& is) { return is.read((char*)glm::value_ptr(value), sizeof(float)*value.length()); }
template <> std::istream& Property<glm::vec3>::loadAscii(std::istream& is) { is.ignore(1); for(int i=0; i<value.length(); ++i) is >> (glm::value_ptr(value)[i]), is.ignore(1); return is; }
template <> AbstractProperty::PropertyType Property<glm::vec3>::getStaticPropertyType() { return AbstractProperty::VEC3; }
// MAT4 PROPERTY
template <> std::ostream& Property<glm::mat4>::saveBinary(std::ostream& os) { return os.write((char*)glm::value_ptr(value), sizeof(float)*value.length()); }
template <> std::ostream& Property<glm::mat4>::saveAscii(std::ostream& os) { os << "["; for(int i=0; i<value.length(); ++i) os << (glm::value_ptr(value)[i]) << (i == value.length()-1 ? "]" : ", "); return os; }
template <> std::istream& Property<glm::mat4>::loadBinary(std::istream& is) { return is.read((char*)glm::value_ptr(value), sizeof(float)*value.length()); }
template <> std::istream& Property<glm::mat4>::loadAscii(std::istream& is) { is.ignore(1); for(int i=0; i<value.length(); ++i) is >> (glm::value_ptr(value)[i]), is.ignore(1); return is; }
template <> AbstractProperty::PropertyType Property<glm::mat4>::getStaticPropertyType() { return AbstractProperty::MAT4; }
// STRING PROPERTY
template <> std::ostream& Property<std::string>::saveBinary(std::ostream& os)
{
static const char endChar = '\0';
os.write(value.data(), sizeof(char)*value.size());
return os.write(&endChar, sizeof(char));;
}
template <> std::ostream& Property<std::string>::saveAscii(std::ostream& os)
{
os << "\"";
for(char c : value)
{
switch(c)
{
case '\t' :
os << "\\t";
break;
case '\\' :
os << "\\\\";
break;
case '\n' :
os << "\\n";
break;
case '\r' :
os << "\\r";
break;
case '"' :
os << "\\\"";
break;
default:
os << c;
}
}
return os << "\"";
}
template <> std::istream& Property<std::string>::loadBinary(std::istream& is)
{
value = "";
for(char c = is.get(); c != '\0'; c = is.get())
value.push_back(c);
return is;
}
template <> std::istream& Property<std::string>::loadAscii(std::istream& is)
{
value = "";
is.ignore(1); // "
for(char c = is.get(); c != '"'; c = is.get())
{
if(c == '\\')
{
c = is.get();
switch(c)
{
case 't' :
value.push_back('\t');
break;
case '\\' :
value.push_back('\\');
break;
case 'n' :
value.push_back('\n');
break;
case 'r' :
value.push_back('\r');
break;
case '"' :
value.push_back('"');
c = 'c';
break;
default:
value.push_back(c);
}
}
else
value.push_back(c);
}
return is;
}
template <> AbstractProperty::PropertyType Property<std::string>::getStaticPropertyType() { return AbstractProperty::STRING; }
// ARRAY PROPERTY SPECIAL BOOL HANDLING
template <> std::ostream& ArrayProperty<bool>::saveBinary(std::ostream& os)
{
unsigned int size = value.size();
os.write((char*)&size, sizeof(unsigned int));
for(bool it : value)
Property<bool>(it).saveBinary(os);
return os;
}
template <> std::ostream& ArrayProperty<bool>::saveAscii(std::ostream& os)
{
os << "{ " << value.size() << ", [" << std::endl;
for(bool it : value)
{
Property<bool>(it).saveAscii(os);
os << "," << std::endl;
}
return os << "] }";
}