/[svn]/libgig/trunk/src/Serialization.cpp
ViewVC logotype

Diff of /libgig/trunk/src/Serialization.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 3168 by schoenebeck, Tue May 9 19:12:32 2017 UTC revision 3182 by schoenebeck, Sun May 14 20:40:02 2017 UTC
# Line 27  Line 27 
27  #include <assert.h>  #include <assert.h>
28  #include <string.h> // for memcpy()  #include <string.h> // for memcpy()
29  #include <stdlib.h> // for atof()  #include <stdlib.h> // for atof()
30    #include <cxxabi.h>
31    
32  #include "helper.h"  #include "helper.h"
33    
# Line 127  namespace Serialization { Line 128  namespace Serialization {
128      }      }
129    
130      String DataType::asLongDescr() const {      String DataType::asLongDescr() const {
         //TODO: Demangling of C++ raw type names  
131          String s = m_baseTypeName;          String s = m_baseTypeName;
132          if (!m_customTypeName.empty())          if (!m_customTypeName.empty())
133              s += " " + m_customTypeName;              s += " " + customTypeName(true);
134          if (isPointer())          if (isPointer())
135              s += " pointer";              s += " pointer";
136          return s;          return s;
137      }      }
138    
139        String DataType::customTypeName(bool demangle) const {
140            if (!demangle) return m_customTypeName;
141            int status;
142            const char* result =
143                abi::__cxa_demangle(m_customTypeName.c_str(), 0, 0, &status);
144            return (status == 0) ? result : m_customTypeName;
145        }
146    
147      // *************** Member ***************      // *************** Member ***************
148      // *      // *
149    
# Line 232  namespace Serialization { Line 240  namespace Serialization {
240              return other.minVersion() <= this->version();              return other.minVersion() <= this->version();
241      }      }
242    
243        void Object::setVersion(Version v) {
244            m_version = v;
245        }
246    
247        void Object::setMinVersion(Version v) {
248            m_minVersion = v;
249        }
250    
251      Member Object::memberNamed(String name) const {      Member Object::memberNamed(String name) const {
252          for (int i = 0; i < m_members.size(); ++i)          for (int i = 0; i < m_members.size(); ++i)
253              if (m_members[i].name() == name)              if (m_members[i].name() == name)
# Line 402  namespace Serialization { Line 418  namespace Serialization {
418          return s;          return s;
419      }      }
420    
421        template<typename T>
422        static T _primitiveObjectValueToNumber(const Object& obj) {
423            T value = 0;
424            const DataType& type = obj.type();
425            const ID& id = obj.uid().id;
426            void* ptr = obj.m_data.empty() ? (void*)id : (void*)&obj.m_data[0];
427            if (!obj.m_data.empty())
428                assert(type.size() == obj.m_data.size());
429            if (type.isPrimitive() && !type.isPointer()) {
430                if (type.isInteger() || type.isEnum()) {
431                    if (type.isSigned()) {
432                        if (type.size() == 1)
433                            value = (T)*(int8_t*)ptr;
434                        else if (type.size() == 2)
435                            value = (T)*(int16_t*)ptr;
436                        else if (type.size() == 4)
437                            value = (T)*(int32_t*)ptr;
438                        else if (type.size() == 8)
439                            value = (T)*(int64_t*)ptr;
440                        else
441                            assert(false /* unknown signed int type size */);
442                    } else {
443                        if (type.size() == 1)
444                            value = (T)*(uint8_t*)ptr;
445                        else if (type.size() == 2)
446                            value = (T)*(uint16_t*)ptr;
447                        else if (type.size() == 4)
448                            value = (T)*(uint32_t*)ptr;
449                        else if (type.size() == 8)
450                            value = (T)*(uint64_t*)ptr;
451                        else
452                            assert(false /* unknown unsigned int type size */);
453                    }
454                } else if (type.isReal()) {
455                    if (type.size() == sizeof(float))
456                        value = (T)*(float*)ptr;
457                    else if (type.size() == sizeof(double))
458                        value = (T)*(double*)ptr;
459                    else
460                        assert(false /* unknown floating point type */);
461                } else if (type.isBool()) {
462                    value = (T)*(bool*)ptr;
463                } else {
464                    assert(false /* unknown primitive type */);
465                }
466            }
467            return value;
468        }
469    
470      static String _encodePrimitiveValue(const Object& obj) {      static String _encodePrimitiveValue(const Object& obj) {
471          return _encodeBlob( _primitiveObjectValueToString(obj) );          return _encodeBlob( _primitiveObjectValueToString(obj) );
472      }      }
# Line 855  namespace Serialization { Line 920  namespace Serialization {
920          return m_allObjects[uid];          return m_allObjects[uid];
921      }      }
922    
923        void Archive::setVersion(Object& object, Version v) {
924            if (!object) return;
925            object.setVersion(v);
926            m_isModified = true;
927        }
928    
929        void Archive::setMinVersion(Object& object, Version v) {
930            if (!object) return;
931            object.setMinVersion(v);
932            m_isModified = true;
933        }
934    
935      void Archive::setEnumValue(Object& object, uint64_t value) {      void Archive::setEnumValue(Object& object, uint64_t value) {
936          if (!object) return;          if (!object) return;
937          if (!object.type().isEnum())          if (!object.type().isEnum())
# Line 994  namespace Serialization { Line 1071  namespace Serialization {
1071          return _primitiveObjectValueToString(*pObject);          return _primitiveObjectValueToString(*pObject);
1072      }      }
1073    
1074        int64_t Archive::valueAsInt(const Object& object) {
1075            if (!object)
1076                throw Exception("Invalid object");
1077            if (!object.type().isInteger() && !object.type().isEnum())
1078                throw Exception("Object is neither an integer nor an enum");
1079            const Object* pObject = &object;
1080            if (object.type().isPointer()) {
1081                const Object& obj = objectByUID(object.uid(1));
1082                if (!obj) return 0;
1083                pObject = &obj;
1084            }
1085            return _primitiveObjectValueToNumber<int64_t>(*pObject);
1086        }
1087    
1088        double Archive::valueAsReal(const Object& object) {
1089            if (!object)
1090                throw Exception("Invalid object");
1091            if (!object.type().isReal())
1092                throw Exception("Object is not an real type");
1093            const Object* pObject = &object;
1094            if (object.type().isPointer()) {
1095                const Object& obj = objectByUID(object.uid(1));
1096                if (!obj) return 0;
1097                pObject = &obj;
1098            }
1099            return _primitiveObjectValueToNumber<double>(*pObject);
1100        }
1101    
1102        bool Archive::valueAsBool(const Object& object) {
1103            if (!object)
1104                throw Exception("Invalid object");
1105            if (!object.type().isBool())
1106                throw Exception("Object is not a bool");
1107            const Object* pObject = &object;
1108            if (object.type().isPointer()) {
1109                const Object& obj = objectByUID(object.uid(1));
1110                if (!obj) return 0;
1111                pObject = &obj;
1112            }
1113            return _primitiveObjectValueToNumber<bool>(*pObject);
1114        }
1115    
1116      // *************** Archive::Syncer ***************      // *************** Archive::Syncer ***************
1117      // *      // *
1118    

Legend:
Removed from v.3168  
changed lines
  Added in v.3182

  ViewVC Help
Powered by ViewVC