Osmium
0.1
|
00001 #ifndef OSMIUM_OSM_RELATION_MEMBER_HPP 00002 #define OSMIUM_OSM_RELATION_MEMBER_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 <cstring> 00026 #include <stdexcept> 00027 00028 #include <osmium/osm/types.hpp> 00029 00030 namespace Osmium { 00031 00032 namespace OSM { 00033 00034 class RelationMember { 00035 00036 public: 00037 00038 static const int max_characters_role = 255; 00039 00040 static const int max_utf16_length_role = 2 * (max_characters_role + 1); 00041 00042 static const int max_length_role = 255 * 4 + 1; /* 255 UTF-8 characters + null byte */ 00043 00044 osm_object_id_t ref() const { 00045 return m_ref; 00046 } 00047 00048 RelationMember& ref(osm_object_id_t ref) { 00049 m_ref = ref; 00050 return *this; 00051 } 00052 00053 char type() const { 00054 return m_type; 00055 } 00056 00057 const char *type_name() const { 00058 switch (type()) { 00059 case 'n': 00060 return "node"; 00061 case 'w': 00062 return "way"; 00063 case 'r': 00064 return "relation"; 00065 default: 00066 return "unknown"; 00067 } 00068 } 00069 00070 RelationMember& type(char type) { 00071 m_type = type; 00072 return *this; 00073 } 00074 00075 const char *role() const { 00076 return m_role; 00077 } 00078 00079 RelationMember& role(const char* role) { 00080 if (! memccpy(m_role, role, 0, max_length_role)) { 00081 throw std::length_error("role too long"); 00082 } 00083 return *this; 00084 } 00085 00086 #ifdef OSMIUM_WITH_JAVASCRIPT 00087 v8::Local<v8::Object> js_instance() const { 00088 return JavascriptTemplate::get<JavascriptTemplate>().create_instance((void*)this); 00089 } 00090 00091 v8::Handle<v8::Value> js_ref() const { 00092 return v8::Number::New(ref()); 00093 } 00094 00095 v8::Handle<v8::Value> js_type() const { 00096 char t[2]; 00097 t[0] = type(); 00098 t[1] = 0; 00099 return v8::String::NewSymbol(t); 00100 } 00101 00102 v8::Handle<v8::Value> js_role() const { 00103 return Osmium::utf8_to_v8_String<max_utf16_length_role>(role()); 00104 } 00105 00106 struct JavascriptTemplate : public Osmium::Javascript::Template { 00107 00108 JavascriptTemplate() : Osmium::Javascript::Template() { 00109 js_template->SetAccessor(v8::String::NewSymbol("type"), accessor_getter<Osmium::OSM::RelationMember, &Osmium::OSM::RelationMember::js_type>); 00110 js_template->SetAccessor(v8::String::NewSymbol("ref"), accessor_getter<Osmium::OSM::RelationMember, &Osmium::OSM::RelationMember::js_ref>); 00111 js_template->SetAccessor(v8::String::NewSymbol("role"), accessor_getter<Osmium::OSM::RelationMember, &Osmium::OSM::RelationMember::js_role>); 00112 } 00113 00114 }; 00115 #endif // OSMIUM_WITH_JAVASCRIPT 00116 00117 private: 00118 00119 osm_object_id_t m_ref; 00120 char m_type; 00121 char m_role[max_length_role]; 00122 00123 }; // class RelationMember 00124 00125 } // namespace OSM 00126 00127 } // namespace Osmium 00128 00129 #endif // OSMIUM_OSM_RELATION_MEMBER_HPP