Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-22 10:59:27

0001 #ifndef GRAPHBUILDER_H_62B23520_7C8E_11DE_8A39_0800200C9A66
0002 #define GRAPHBUILDER_H_62B23520_7C8E_11DE_8A39_0800200C9A66
0003 
0004 #if defined(_MSC_VER) ||                                            \
0005     (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \
0006      (__GNUC__ >= 4))  // GCC supports "pragma once" correctly since 3.4
0007 #pragma once
0008 #endif
0009 
0010 #include "yaml-cpp/mark.h"
0011 #include <string>
0012 
0013 namespace YAML {
0014 class Parser;
0015 
0016 // GraphBuilderInterface
0017 // . Abstraction of node creation
0018 // . pParentNode is always nullptr or the return value of one of the NewXXX()
0019 //   functions.
0020 class GraphBuilderInterface {
0021  public:
0022   virtual ~GraphBuilderInterface() = 0;
0023 
0024   // Create and return a new node with a null value.
0025   virtual void *NewNull(const Mark &mark, void *pParentNode) = 0;
0026 
0027   // Create and return a new node with the given tag and value.
0028   virtual void *NewScalar(const Mark &mark, const std::string &tag,
0029                           void *pParentNode, const std::string &value) = 0;
0030 
0031   // Create and return a new sequence node
0032   virtual void *NewSequence(const Mark &mark, const std::string &tag,
0033                             void *pParentNode) = 0;
0034 
0035   // Add pNode to pSequence.  pNode was created with one of the NewXxx()
0036   // functions and pSequence with NewSequence().
0037   virtual void AppendToSequence(void *pSequence, void *pNode) = 0;
0038 
0039   // Note that no moew entries will be added to pSequence
0040   virtual void SequenceComplete(void *pSequence) { (void)pSequence; }
0041 
0042   // Create and return a new map node
0043   virtual void *NewMap(const Mark &mark, const std::string &tag,
0044                        void *pParentNode) = 0;
0045 
0046   // Add the pKeyNode => pValueNode mapping to pMap.  pKeyNode and pValueNode
0047   // were created with one of the NewXxx() methods and pMap with NewMap().
0048   virtual void AssignInMap(void *pMap, void *pKeyNode, void *pValueNode) = 0;
0049 
0050   // Note that no more assignments will be made in pMap
0051   virtual void MapComplete(void *pMap) { (void)pMap; }
0052 
0053   // Return the node that should be used in place of an alias referencing
0054   // pNode (pNode by default)
0055   virtual void *AnchorReference(const Mark &mark, void *pNode) {
0056     (void)mark;
0057     return pNode;
0058   }
0059 };
0060 
0061 // Typesafe wrapper for GraphBuilderInterface.  Assumes that Impl defines
0062 // Node, Sequence, and Map types.  Sequence and Map must derive from Node
0063 // (unless Node is defined as void).  Impl must also implement function with
0064 // all of the same names as the virtual functions in GraphBuilderInterface
0065 // -- including the ones with default implementations -- but with the
0066 // prototypes changed to accept an explicit Node*, Sequence*, or Map* where
0067 // appropriate.
0068 template <class Impl>
0069 class GraphBuilder : public GraphBuilderInterface {
0070  public:
0071   typedef typename Impl::Node Node;
0072   typedef typename Impl::Sequence Sequence;
0073   typedef typename Impl::Map Map;
0074 
0075   GraphBuilder(Impl &impl) : m_impl(impl) {
0076     Map *pMap = nullptr;
0077     Sequence *pSeq = nullptr;
0078     Node *pNode = nullptr;
0079 
0080     // Type consistency checks
0081     pNode = pMap;
0082     pNode = pSeq;
0083   }
0084 
0085   GraphBuilderInterface &AsBuilderInterface() { return *this; }
0086 
0087   virtual void *NewNull(const Mark &mark, void *pParentNode) {
0088     return CheckType<Node>(m_impl.NewNull(mark, AsNode(pParentNode)));
0089   }
0090 
0091   virtual void *NewScalar(const Mark &mark, const std::string &tag,
0092                           void *pParentNode, const std::string &value) {
0093     return CheckType<Node>(
0094         m_impl.NewScalar(mark, tag, AsNode(pParentNode), value));
0095   }
0096 
0097   virtual void *NewSequence(const Mark &mark, const std::string &tag,
0098                             void *pParentNode) {
0099     return CheckType<Sequence>(
0100         m_impl.NewSequence(mark, tag, AsNode(pParentNode)));
0101   }
0102   virtual void AppendToSequence(void *pSequence, void *pNode) {
0103     m_impl.AppendToSequence(AsSequence(pSequence), AsNode(pNode));
0104   }
0105   virtual void SequenceComplete(void *pSequence) {
0106     m_impl.SequenceComplete(AsSequence(pSequence));
0107   }
0108 
0109   virtual void *NewMap(const Mark &mark, const std::string &tag,
0110                        void *pParentNode) {
0111     return CheckType<Map>(m_impl.NewMap(mark, tag, AsNode(pParentNode)));
0112   }
0113   virtual void AssignInMap(void *pMap, void *pKeyNode, void *pValueNode) {
0114     m_impl.AssignInMap(AsMap(pMap), AsNode(pKeyNode), AsNode(pValueNode));
0115   }
0116   virtual void MapComplete(void *pMap) { m_impl.MapComplete(AsMap(pMap)); }
0117 
0118   virtual void *AnchorReference(const Mark &mark, void *pNode) {
0119     return CheckType<Node>(m_impl.AnchorReference(mark, AsNode(pNode)));
0120   }
0121 
0122  private:
0123   Impl &m_impl;
0124 
0125   // Static check for pointer to T
0126   template <class T, class U>
0127   static T *CheckType(U *p) {
0128     return p;
0129   }
0130 
0131   static Node *AsNode(void *pNode) { return static_cast<Node *>(pNode); }
0132   static Sequence *AsSequence(void *pSeq) {
0133     return static_cast<Sequence *>(pSeq);
0134   }
0135   static Map *AsMap(void *pMap) { return static_cast<Map *>(pMap); }
0136 };
0137 
0138 void *BuildGraphOfNextDocument(Parser &parser,
0139                                GraphBuilderInterface &graphBuilder);
0140 
0141 template <class Impl>
0142 typename Impl::Node *BuildGraphOfNextDocument(Parser &parser, Impl &impl) {
0143   GraphBuilder<Impl> graphBuilder(impl);
0144   return static_cast<typename Impl::Node *>(
0145       BuildGraphOfNextDocument(parser, graphBuilder));
0146 }
0147 }
0148 
0149 #endif  // GRAPHBUILDER_H_62B23520_7C8E_11DE_8A39_0800200C9A66