IWORKValueContext.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /*
00003  * This file is part of the libetonyek project.
00004  *
00005  * This Source Code Form is subject to the terms of the Mozilla Public
00006  * License, v. 2.0. If a copy of the MPL was not distributed with this
00007  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
00008  */
00009 
00010 #ifndef IWORKVALUECONTEXT_H_INCLUDED
00011 #define IWORKVALUECONTEXT_H_INCLUDED
00012 
00013 #include <boost/optional.hpp>
00014 
00015 #include "IWORKXMLContextBase.h"
00016 
00017 namespace libetonyek
00018 {
00019 
00020 template<typename Type, class NestedParser, unsigned Id>
00021 class IWORKValueContext : public IWORKXMLElementContextBase
00022 {
00023 public:
00024   IWORKValueContext(IWORKXMLParserState &state, boost::optional<Type> &value)
00025     : IWORKXMLElementContextBase(state)
00026     , m_value()
00027     , m_optional(value)
00028     , m_direct(0)
00029     , m_isSet(0)
00030   {
00031   }
00032 
00033   IWORKValueContext(IWORKXMLParserState &state, Type &value, bool *isSet = 0)
00034     : IWORKXMLElementContextBase(state)
00035     , m_value()
00036     , m_optional(m_value)
00037     , m_direct(&value)
00038     , m_isSet(isSet)
00039   {
00040   }
00041 
00042 protected:
00043   void set(const Type &value)
00044   {
00045     m_value = value;
00046   }
00047 
00048   virtual IWORKXMLContextPtr_t element(const int name)
00049   {
00050     if (name == Id)
00051       return makeContext<NestedParser>(getState(), m_value);
00052     return IWORKXMLContextPtr_t();
00053   }
00054 
00055   virtual void endOfElement()
00056   {
00057     if (m_value)
00058     {
00059       if (m_direct)
00060       {
00061         *m_direct = get(m_value);
00062         if (m_isSet)
00063           *m_isSet |= true;
00064       }
00065       else
00066       {
00067         m_optional = m_value;
00068       }
00069     }
00070   }
00071 
00072 private:
00073   boost::optional<Type> m_value;
00074   boost::optional<Type> &m_optional;
00075   Type *const m_direct;
00076   bool *const m_isSet;
00077 };
00078 
00079 }
00080 
00081 #endif // IWORKVALUECONTEXT_H_INCLUDED
00082 
00083 /* vim:set shiftwidth=2 softtabstop=2 expandtab: */