/[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 3159 by schoenebeck, Mon May 8 21:15:16 2017 UTC revision 3169 by schoenebeck, Wed May 10 21:17:10 2017 UTC
# Line 38  namespace Serialization { Line 38  namespace Serialization {
38      // *      // *
39    
40      static UID _createNullUID() {      static UID _createNullUID() {
41          return (UID) { NULL, 0 };          const UID uid = { NULL, 0 };
42            return uid;
43      }      }
44    
45      const UID NO_UID = _createNullUID();      const UID NO_UID = _createNullUID();
# Line 401  namespace Serialization { Line 402  namespace Serialization {
402          return s;          return s;
403      }      }
404    
405        template<typename T>
406        static T _primitiveObjectValueToNumber(const Object& obj) {
407            T value = 0;
408            const DataType& type = obj.type();
409            const ID& id = obj.uid().id;
410            void* ptr = obj.m_data.empty() ? (void*)id : (void*)&obj.m_data[0];
411            if (!obj.m_data.empty())
412                assert(type.size() == obj.m_data.size());
413            if (type.isPrimitive() && !type.isPointer()) {
414                if (type.isInteger() || type.isEnum()) {
415                    if (type.isSigned()) {
416                        if (type.size() == 1)
417                            value = (T)*(int8_t*)ptr;
418                        else if (type.size() == 2)
419                            value = (T)*(int16_t*)ptr;
420                        else if (type.size() == 4)
421                            value = (T)*(int32_t*)ptr;
422                        else if (type.size() == 8)
423                            value = (T)*(int64_t*)ptr;
424                        else
425                            assert(false /* unknown signed int type size */);
426                    } else {
427                        if (type.size() == 1)
428                            value = (T)*(uint8_t*)ptr;
429                        else if (type.size() == 2)
430                            value = (T)*(uint16_t*)ptr;
431                        else if (type.size() == 4)
432                            value = (T)*(uint32_t*)ptr;
433                        else if (type.size() == 8)
434                            value = (T)*(uint64_t*)ptr;
435                        else
436                            assert(false /* unknown unsigned int type size */);
437                    }
438                } else if (type.isReal()) {
439                    if (type.size() == sizeof(float))
440                        value = (T)*(float*)ptr;
441                    else if (type.size() == sizeof(double))
442                        value = (T)*(double*)ptr;
443                    else
444                        assert(false /* unknown floating point type */);
445                } else if (type.isBool()) {
446                    value = (T)*(bool*)ptr;
447                } else {
448                    assert(false /* unknown primitive type */);
449                }
450            }
451            return value;
452        }
453    
454      static String _encodePrimitiveValue(const Object& obj) {      static String _encodePrimitiveValue(const Object& obj) {
455          return _encodeBlob( _primitiveObjectValueToString(obj) );          return _encodeBlob( _primitiveObjectValueToString(obj) );
456      }      }
# Line 460  namespace Serialization { Line 510  namespace Serialization {
510      };      };
511    
512      static _Blob _decodeBlob(const char* p, const char* end, bool bThrow = true) {      static _Blob _decodeBlob(const char* p, const char* end, bool bThrow = true) {
513          if (!bThrow && p >= end)          if (!bThrow && p >= end) {
514              return (_Blob) { p, end };              const _Blob blob =  { p, end };
515                return blob;
516            }
517          size_t sz = 0;          size_t sz = 0;
518          for (; true; ++p) {          for (; true; ++p) {
519              if (p >= end)              if (p >= end)
# Line 476  namespace Serialization { Line 528  namespace Serialization {
528          ++p;          ++p;
529          if (p + sz > end)          if (p + sz > end)
530              throw Exception("Decode Error: Premature end of blob");              throw Exception("Decode Error: Premature end of blob");
531          return (_Blob) { p, p + sz };          const _Blob blob = { p, p + sz };
532            return blob;
533      }      }
534    
535      template<typename T_int>      template<typename T_int>
# Line 580  namespace Serialization { Line 633  namespace Serialization {
633          const ID id = (ID) _popIntBlob<size_t>(p, end);          const ID id = (ID) _popIntBlob<size_t>(p, end);
634          const size_t size = _popIntBlob<size_t>(p, end);          const size_t size = _popIntBlob<size_t>(p, end);
635    
636          return (UID) { id, size };          const UID uid = { id, size };
637            return uid;
638      }      }
639    
640      static UIDChain _popUIDChainBlob(const char*& p, const char* end) {      static UIDChain _popUIDChainBlob(const char*& p, const char* end) {
# Line 989  namespace Serialization { Line 1043  namespace Serialization {
1043          return _primitiveObjectValueToString(*pObject);          return _primitiveObjectValueToString(*pObject);
1044      }      }
1045    
1046        int64_t Archive::valueAsInt(const Object& object) {
1047            if (!object)
1048                throw Exception("Invalid object");
1049            if (!object.type().isInteger() && !object.type().isEnum())
1050                throw Exception("Object is neither an integer nor an enum");
1051            const Object* pObject = &object;
1052            if (object.type().isPointer()) {
1053                const Object& obj = objectByUID(object.uid(1));
1054                if (!obj) return 0;
1055                pObject = &obj;
1056            }
1057            return _primitiveObjectValueToNumber<int64_t>(*pObject);
1058        }
1059    
1060        double Archive::valueAsReal(const Object& object) {
1061            if (!object)
1062                throw Exception("Invalid object");
1063            if (!object.type().isReal())
1064                throw Exception("Object is not an real type");
1065            const Object* pObject = &object;
1066            if (object.type().isPointer()) {
1067                const Object& obj = objectByUID(object.uid(1));
1068                if (!obj) return 0;
1069                pObject = &obj;
1070            }
1071            return _primitiveObjectValueToNumber<double>(*pObject);
1072        }
1073    
1074        bool Archive::valueAsBool(const Object& object) {
1075            if (!object)
1076                throw Exception("Invalid object");
1077            if (!object.type().isBool())
1078                throw Exception("Object is not a bool");
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<bool>(*pObject);
1086        }
1087    
1088      // *************** Archive::Syncer ***************      // *************** Archive::Syncer ***************
1089      // *      // *
1090    

Legend:
Removed from v.3159  
changed lines
  Added in v.3169

  ViewVC Help
Powered by ViewVC