Osmium
0.1
|
00001 #ifndef OSMIUM_OSM_TAG_LIST_HPP 00002 #define OSMIUM_OSM_TAG_LIST_HPP 00003 00004 /* 00005 00006 Copyright 2011 Jochen Topf <jochen@topf.org> and others (see README). 00007 00008 This file is part of Osmium (https://github.com/joto/osmium). 00009 00010 Osmium is free software: you can redistribute it and/or modify it under the 00011 terms of the GNU Lesser General Public License or (at your option) the GNU 00012 General Public License as published by the Free Software Foundation, either 00013 version 3 of the Licenses, or (at your option) any later version. 00014 00015 Osmium is distributed in the hope that it will be useful, but WITHOUT ANY 00016 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 00017 PARTICULAR PURPOSE. See the GNU Lesser General Public License and the GNU 00018 General Public License for more details. 00019 00020 You should have received a copy of the Licenses along with Osmium. If not, see 00021 <http://www.gnu.org/licenses/>. 00022 00023 */ 00024 00025 #include <vector> 00026 #include <cstring> 00027 #include <stdexcept> 00028 00029 #include <osmium/osm/tag.hpp> 00030 00031 namespace Osmium { 00032 00033 namespace OSM { 00034 00041 class TagList { 00042 00043 public: 00044 00045 TagList() : m_tags() { 00046 } 00047 00049 int size() const { 00050 return m_tags.size(); 00051 } 00052 00053 bool empty() const { 00054 return m_tags.empty(); 00055 } 00056 00058 void clear() { 00059 m_tags.clear(); 00060 } 00061 00062 Tag& operator[](int i) { 00063 return m_tags[i]; 00064 } 00065 00066 const Tag& operator[](int i) const { 00067 return m_tags[i]; 00068 } 00069 00070 typedef std::vector<Tag>::iterator iterator; 00071 typedef std::vector<Tag>::const_iterator const_iterator; 00072 00073 iterator begin() { 00074 return m_tags.begin(); 00075 } 00076 00077 const_iterator begin() const { 00078 return m_tags.begin(); 00079 } 00080 00081 iterator end() { 00082 return m_tags.end(); 00083 } 00084 00085 const_iterator end() const { 00086 return m_tags.end(); 00087 } 00088 00090 void add(const char* key, const char* value) { 00091 m_tags.push_back(Tag(key, value)); 00092 } 00093 00094 const char* get_tag_by_key(const char* key) const { 00095 for (const_iterator it = begin(); it != end(); ++it) { 00096 if (!strcmp(it->key(), key)) { 00097 return it->value(); 00098 } 00099 } 00100 return 0; 00101 } 00102 00103 const char* get_tag_key(unsigned int n) const { 00104 if (n < m_tags.size()) { 00105 return m_tags[n].key(); 00106 } 00107 throw std::range_error("no tag with this index"); 00108 } 00109 00110 const char* get_tag_value(unsigned int n) const { 00111 if (n < m_tags.size()) { 00112 return m_tags[n].value(); 00113 } 00114 throw std::range_error("no tag with this index"); 00115 } 00116 00117 #ifdef OSMIUM_WITH_JAVASCRIPT 00118 v8::Local<v8::Object> js_instance() const { 00119 return JavascriptTemplate::get<JavascriptTemplate>().create_instance((void*)this); 00120 } 00121 00122 v8::Handle<v8::Value> js_get_tag_value_by_key(v8::Local<v8::String> property) const { 00123 const char* key = Osmium::v8_String_to_utf8<Osmium::OSM::Tag::max_utf16_length_key>(property); 00124 const char* value = get_tag_by_key(key); 00125 if (value) { 00126 return Osmium::utf8_to_v8_String<Osmium::OSM::Tag::max_utf16_length_value>(value); 00127 } 00128 return v8::Undefined(); 00129 } 00130 00131 v8::Handle<v8::Array> js_enumerate_tag_keys() const { 00132 v8::HandleScope scope; 00133 v8::Local<v8::Array> array = v8::Array::New(m_tags.size()); 00134 00135 const_iterator end = this->end(); 00136 int i = 0; 00137 for (const_iterator it = begin(); it != end; ++it) { 00138 array->Set(i++, Osmium::utf8_to_v8_String<Osmium::OSM::Tag::max_utf16_length_key>(it->key())); 00139 } 00140 00141 return scope.Close(array); 00142 } 00143 00144 struct JavascriptTemplate : public Osmium::Javascript::Template { 00145 00146 JavascriptTemplate() : Osmium::Javascript::Template() { 00147 js_template->SetNamedPropertyHandler( 00148 named_property_getter<TagList, &TagList::js_get_tag_value_by_key>, 00149 0, 00150 0, 00151 0, 00152 property_enumerator<TagList, &TagList::js_enumerate_tag_keys> 00153 ); 00154 } 00155 00156 }; 00157 #endif // OSMIUM_WITH_JAVASCRIPT 00158 00159 private: 00160 00161 std::vector<Tag> m_tags; 00162 00163 }; // class TagList 00164 00165 } // namespace OSM 00166 00167 } // namespace Osmium 00168 00169 #endif // OSMIUM_OSM_TAG_LIST_HPP