RAUL  0.8.0
Symbol.hpp
1 /* This file is part of Raul.
2  * Copyright (C) 2008-2009 David Robillard <http://drobilla.net>
3  *
4  * Raul is free software; you can redistribute it and/or modify it under the
5  * terms of the GNU General Public License as published by the Free Software
6  * Foundation; either version 2 of the License, or (at your option) any later
7  * version.
8  *
9  * Raul is distributed in the hope that it will be useful, but WITHOUT ANY
10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16  */
17 
18 #ifndef RAUL_SYMBOL_HPP
19 #define RAUL_SYMBOL_HPP
20 
21 #include <cassert>
22 #include <cctype>
23 #include <cstring>
24 #include <iostream>
25 #include <string>
26 
27 #include <glib.h>
28 
29 namespace Raul {
30 
31 
43 class Symbol {
44 public:
50  Symbol(const std::basic_string<char>& symbol)
51  : _str(g_intern_string(symbol.c_str()))
52  {
53  assert(is_valid(symbol));
54  }
55 
56 
62  Symbol(const char* csymbol)
63  : _str(g_intern_string(csymbol))
64  {
65  assert(is_valid(csymbol));
66  }
67 
68  inline const char* c_str() const { return _str; }
69 
70  inline bool operator==(const Symbol& other) const {
71  return _str == other._str;
72  }
73 
74  inline bool operator!=(const Symbol& other) const {
75  return _str != other._str;
76  }
77 
78  inline bool operator<(const Symbol& other) const {
79  return strcmp(_str, other._str) < 0;
80  }
81 
82  static bool is_valid(const std::basic_string<char>& symbol);
83 
84  static std::string symbolify(const std::basic_string<char>& str);
85 
86 private:
87  const char* _str;
88 };
89 
90 
91 } // namespace Raul
92 
93 static inline std::ostream& operator<<(std::ostream& os, const Raul::Symbol& symbol)
94 {
95  return (os << symbol.c_str());
96 }
97 
98 #endif // RAUL_SYMBOL_HPP