qname 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // file : xml/qname -*- C++ -*-
  2. // copyright : Copyright (c) 2013-2014 Code Synthesis Tools CC
  3. // license : MIT; see accompanying LICENSE file
  4. #ifndef XML_QNAME
  5. #define XML_QNAME
  6. #include <xml/details/pre.hxx>
  7. #include <string>
  8. #include <iosfwd>
  9. #include <xml/forward>
  10. #include <xml/details/export.hxx>
  11. namespace xml
  12. {
  13. // Note that the optional prefix is just a "syntactic sugar". In
  14. // particular, it is ignored by the comparison operators and the
  15. // std::ostream insertion operator.
  16. //
  17. class LIBSTUDXML_EXPORT qname
  18. {
  19. public:
  20. qname () {}
  21. qname (const std::string& name): name_ (name) {}
  22. qname (const std::string& ns, const std::string& name)
  23. : ns_ (ns), name_ (name) {}
  24. qname (const std::string& ns,
  25. const std::string& name,
  26. const std::string& prefix)
  27. : ns_ (ns), name_ (name), prefix_ (prefix) {}
  28. const std::string& namespace_ () const {return ns_;}
  29. const std::string& name () const {return name_;}
  30. const std::string& prefix () const {return prefix_;}
  31. std::string& namespace_ () {return ns_;}
  32. std::string& name () {return name_;}
  33. std::string& prefix () {return prefix_;}
  34. bool
  35. empty () const {return name_.empty () && ns_.empty ();}
  36. // String representation in the [<namespace>#]<name> form.
  37. //
  38. std::string
  39. string () const;
  40. // Note that comparison operators ignore prefixes.
  41. //
  42. public:
  43. friend bool
  44. operator< (const qname& x, const qname& y)
  45. {
  46. return x.ns_ < y.ns_ || (x.ns_ == y.ns_ && x.name_ < y.name_);
  47. }
  48. friend bool
  49. operator== (const qname& x, const qname& y)
  50. {
  51. return x.ns_ == y.ns_ && x.name_ == y.name_;
  52. }
  53. friend bool
  54. operator!= (const qname& x, const qname& y)
  55. {
  56. return !(x == y);
  57. }
  58. private:
  59. std::string ns_;
  60. std::string name_;
  61. std::string prefix_;
  62. };
  63. // Print the string representation ([<namespace>#]<name>).
  64. //
  65. LIBSTUDXML_EXPORT
  66. std::ostream&
  67. operator<< (std::ostream&, const qname&);
  68. }
  69. #include <xml/details/post.hxx>
  70. #endif // XML_QNAME