aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/xml/init/ptr.h
blob: 59eb1057f7e2a9089563410ba1394360f39b9c45 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#pragma once

#include <util/generic/ptr.h>
#include <libxml/tree.h>
#include <libxml/parser.h>
#include <libxml/xpath.h>
#include <libxml/xpathInternals.h>
#include <libxml/xmlsave.h>
#include <libxml/uri.h>
#include <libxml/xmlschemas.h>

template <class T, void (*DestroyFun)(T*)>
struct TFunctionDestroy {
    static inline void Destroy(T* t) noexcept {
        if (t)
            DestroyFun(t);
    }
};

namespace NXml {
#define DEF_HOLDER(type, free) typedef THolder<type, TFunctionDestroy<type, free>> T##type##Holder
#define DEF_PTR(type, free) typedef TAutoPtr<type, TFunctionDestroy<type, free>> T##type##Ptr

    // define xmlDocPtr -> TxmlDocHolder TxmlDocPtr
    DEF_HOLDER(xmlDoc, xmlFreeDoc);
    DEF_PTR(xmlDoc, xmlFreeDoc);

    //    xmlXPathContextPtr xpathCtx;
    DEF_HOLDER(xmlXPathContext, xmlXPathFreeContext);
    DEF_PTR(xmlXPathContext, xmlXPathFreeContext);

    //    xmlXPathObjectPtr xpathObj;
    DEF_HOLDER(xmlXPathObject, xmlXPathFreeObject);
    DEF_PTR(xmlXPathObject, xmlXPathFreeObject);

    //    xmlNodeSetPtr nodes
    DEF_HOLDER(xmlNodeSet, xmlXPathFreeNodeSet);
    DEF_PTR(xmlNodeSet, xmlXPathFreeNodeSet);

    //    xmlSchemaParserCtxtPtr ctxt;
    DEF_HOLDER(xmlSchemaParserCtxt, xmlSchemaFreeParserCtxt);
    DEF_PTR(xmlSchemaParserCtxt, xmlSchemaFreeParserCtxt);

    //    xmlSchemaPtr schema;
    DEF_HOLDER(xmlSchema, xmlSchemaFree);
    DEF_PTR(xmlSchema, xmlSchemaFree);

    //    xmlSchemaValidCtxt ctxt;
    DEF_HOLDER(xmlSchemaValidCtxt, xmlSchemaFreeValidCtxt);
    DEF_PTR(xmlSchemaValidCtxt, xmlSchemaFreeValidCtxt);

    // xmlSaveCtxtPtr
    inline void xmlFreeSave(xmlSaveCtxt* c) {
        // returns int
        xmlSaveClose(c);
    }
    DEF_HOLDER(xmlSaveCtxt, xmlFreeSave);
    DEF_PTR(xmlSaveCtxt, xmlFreeSave);

    DEF_PTR(xmlURI, xmlFreeURI);
    DEF_PTR(xmlNode, xmlFreeNode);
    DEF_PTR(xmlParserCtxt, xmlFreeParserCtxt);
    DEF_PTR(xmlParserInputBuffer, xmlFreeParserInputBuffer);
    DEF_PTR(xmlDtd, xmlFreeDtd); 
    DEF_PTR(xmlValidCtxt, xmlFreeValidCtxt); 

#undef DEF_HOLDER
#undef DEF_PTR
}