aboutsummaryrefslogtreecommitdiff
path: root/xml/qname
blob: 85d642476c80eb87028b5a5145aa9e80c0eaaa3e (plain)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// file      : xml/qname -*- C++ -*-
// copyright : Copyright (c) 2013-2014 Code Synthesis Tools CC
// license   : MIT; see accompanying LICENSE file

#ifndef XML_QNAME
#define XML_QNAME

#include <xml/details/pre.hxx>

#include <string>
#include <iosfwd>

#include <xml/forward>

#include <xml/details/export.hxx>

namespace xml
{
  // Note that the optional prefix is just a "syntactic sugar". In
  // particular, it is ignored by the comparison operators and the
  // std::ostream insertion operator.
  //
  class LIBSTUDXML_EXPORT qname
  {
  public:
    qname () {}
    qname (const std::string& name): name_ (name) {}
    qname (const std::string& ns, const std::string& name)
      : ns_ (ns), name_ (name) {}
    qname (const std::string& ns,
           const std::string& name,
           const std::string& prefix)
      : ns_ (ns), name_ (name), prefix_ (prefix) {}

    const std::string& namespace_ () const {return ns_;}
    const std::string& name () const {return name_;}
    const std::string& prefix () const {return prefix_;}

    std::string& namespace_ () {return ns_;}
    std::string& name () {return name_;}
    std::string& prefix () {return prefix_;}

    bool
    empty () const {return name_.empty () && ns_.empty ();}

    // String representation in the [<namespace>#]<name> form.
    //
    std::string
    string () const;

    // Note that comparison operators ignore prefixes.
    //
  public:
    friend bool
    operator< (const qname& x, const qname& y)
    {
      return x.ns_ < y.ns_ || (x.ns_ == y.ns_ && x.name_ < y.name_);
    }

    friend bool
    operator== (const qname& x, const qname& y)
    {
      return x.ns_ == y.ns_ && x.name_ == y.name_;
    }

    friend bool
    operator!= (const qname& x, const qname& y)
    {
      return !(x == y);
    }

  private:
    std::string ns_;
    std::string name_;
    std::string prefix_;
  };

  // Print the string representation ([<namespace>#]<name>).
  //
  LIBSTUDXML_EXPORT
  std::ostream&
  operator<< (std::ostream&, const qname&);
}

#include <xml/details/post.hxx>

#endif // XML_QNAME