]> Dogcows Code - chaz/yoink/blobdiff - src/Moof/Deserializer.cc
considerable refactoring
[chaz/yoink] / src / Moof / Deserializer.cc
index 8bf230fe68627054e225fd54aa8b48ad9f15a1aa..c92ef47ef6fb31081c406576d9967980dc0d38ef 100644 (file)
 #include <yajl/yajl_parse.h>
 
 #include "Deserializer.hh"
+#include "Log.hh"
 #include "Serializable.hh"
 
 
 namespace Mf {
 
 
-class Deserializer::DeserializerImpl
+class Deserializer::Impl
 {
 public:
-       DeserializerImpl(const std::string& filePath, bool comments = false,
+       Impl(const std::string& filePath, bool comments = false,
                        bool check = false)
        {
                std::ifstream* input = new std::ifstream(filePath.c_str());
                init(*input, true, comments, check);
        }
 
-       DeserializerImpl(std::istream& input, bool comments = false,
+       Impl(std::istream& input, bool comments = false,
                        bool check = false)
        {
                init(input, false, comments, check);
        }
 
-       ~DeserializerImpl()
+       ~Impl()
        {
                while (!parsed.empty())
                {
@@ -69,51 +70,50 @@ public:
                yajl_free(hand);
        }
 
-       void throwError()
+       void raise()
        {
                unsigned char* errorStr = yajl_get_error(hand, 0, 0, 0);
-               Deserializer::Exception exception((char*)errorStr);
+               logError("parser error: %s", errorStr);
                yajl_free_error(hand, errorStr);
-               throw exception;
+
+               throw Exception(Exception::PARSING_FAILED);
        }
 
 
        static int parsedNull(void* ctx)
        {
-               ((DeserializerImpl*)ctx)->parsed.push(new SerializableNull);
+               ((Impl*)ctx)->parsed.push(new SerializableNull);
                return 1;
        }
 
        static int parsedBoolean(void* ctx, int value)
        {
-               ((DeserializerImpl*)ctx)->parsed.push(new SerializableBoolean(value));
+               ((Impl*)ctx)->parsed.push(new SerializableBasic<Serializable::Boolean>(value));
                return 1;
        }
 
        static int parsedInteger(void* ctx, long value)
        {
-               ((DeserializerImpl*)ctx)->parsed.push(new SerializableInteger(value));
+               ((Impl*)ctx)->parsed.push(new SerializableBasic<Serializable::Integer>(value));
                return 1;
        }
 
        static int parsedFloat(void* ctx, double value)
        {
-               ((DeserializerImpl*)ctx)->parsed.push(new SerializableReal(value));
+               ((Impl*)ctx)->parsed.push(new SerializableBasic<Serializable::Float>(value));
                return 1;
        }
 
        static int parsedString(void* ctx, const unsigned char* value,
                        unsigned length)
        {
-               SerializableString* parsed =
-                       new SerializableString(std::string((char*)value, length));
-               ((DeserializerImpl*)ctx)->parsed.push(parsed);
+               ((Impl*)ctx)->parsed.push(new SerializableBasic<Serializable::String>(std::string((char*)value, length)));
                return 1;
        }
 
        static int parsedBeginMap(void* ctx)
        {
-               ((DeserializerImpl*)ctx)->parsed.push(new SerializableMap);
+               ((Impl*)ctx)->parsed.push(new SerializableBasic<Serializable::Map>);
                return 1;
        }
 
@@ -126,19 +126,21 @@ public:
 
        static int parsedEndMap(void* ctx)
        {
-               ((DeserializerImpl*)ctx)->parsed.push(0);
+               // null means the end of a structure
+               ((Impl*)ctx)->parsed.push(0);
                return 1;
        }
 
        static int parsedBeginArray(void* ctx)
        {
-               ((DeserializerImpl*)ctx)->parsed.push(new SerializableArray);
+               ((Impl*)ctx)->parsed.push(new SerializableBasic<Serializable::Array>);
                return 1;
        }
 
        static int parsedEndArray(void* ctx)
        {
-               ((DeserializerImpl*)ctx)->parsed.push(0);
+               // null means the end of a structure
+               ((Impl*)ctx)->parsed.push(0);
                return 1;
        }
 
@@ -166,7 +168,7 @@ public:
                        if (stat != yajl_status_ok &&
                                        stat != yajl_status_insufficient_data)
                        {
-                               throwError();
+                               raise();
                        }
                }
        }
@@ -186,17 +188,17 @@ private:
                // internal data structures but rather keeps a pointer to this
                static const yajl_callbacks callbacks =
                {
-                       DeserializerImpl::parsedNull,
-                       DeserializerImpl::parsedBoolean,
-                       DeserializerImpl::parsedInteger,
-                       DeserializerImpl::parsedFloat,
+                       Impl::parsedNull,
+                       Impl::parsedBoolean,
+                       Impl::parsedInteger,
+                       Impl::parsedFloat,
                        0,
-                       DeserializerImpl::parsedString,
-                       DeserializerImpl::parsedBeginMap,
-                       DeserializerImpl::parsedMapKey,
-                       DeserializerImpl::parsedEndMap,
-                       DeserializerImpl::parsedBeginArray,
-                       DeserializerImpl::parsedEndArray
+                       Impl::parsedString,
+                       Impl::parsedBeginMap,
+                       Impl::parsedMapKey,
+                       Impl::parsedEndMap,
+                       Impl::parsedBeginArray,
+                       Impl::parsedEndArray
                };
 
                in = &input;
@@ -211,21 +213,21 @@ private:
 Deserializer::Deserializer(const std::string& filePath, bool comments,
                bool check) :
        // pass through
-       impl_(new Deserializer::DeserializerImpl(filePath, comments, check)) {}
+       impl_(new Deserializer::Impl(filePath, comments, check)) {}
 
 Deserializer::Deserializer(std::istream& input, bool comments, bool check) :
        // pass through
-       impl_(new Deserializer::DeserializerImpl(input, comments, check)) {}
+       impl_(new Deserializer::Impl(input, comments, check)) {}
 
 
-SerializablePtr Deserializer::deserialize()
+SerializableP Deserializer::deserialize()
 {
        Serializable* ptr = pullNext();
        if (ptr)
        {
                ptr->deserialize(*this);
        }
-       return SerializablePtr(ptr);
+       return SerializableP(ptr);
 }
 
 
This page took 0.025843 seconds and 4 git commands to generate.