Osmium  0.1
include/osmium/osm/relation_member_list.hpp
Go to the documentation of this file.
00001 #ifndef OSMIUM_OSM_RELATION_MEMBER_LIST_HPP
00002 #define OSMIUM_OSM_RELATION_MEMBER_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 
00027 #include <osmium/osm/relation_member.hpp>
00028 
00029 namespace Osmium {
00030 
00031     namespace OSM {
00032 
00033         class RelationMemberList {
00034 
00035         public:
00036 
00037             RelationMemberList() : m_list() {
00038             }
00039 
00040             osm_sequence_id_t size() const {
00041                 return m_list.size();
00042             }
00043 
00044             void clear() {
00045                 m_list.clear();
00046             }
00047 
00048             RelationMember& operator[](int i) {
00049                 return m_list[i];
00050             }
00051 
00052             const RelationMember& operator[](int i) const {
00053                 return m_list[i];
00054             }
00055 
00056             typedef std::vector<RelationMember>::iterator iterator;
00057             typedef std::vector<RelationMember>::const_iterator const_iterator;
00058 
00059             iterator begin() {
00060                 return m_list.begin();
00061             }
00062 
00063             const_iterator begin() const {
00064                 return m_list.begin();
00065             }
00066 
00067             iterator end() {
00068                 return m_list.end();
00069             }
00070 
00071             const_iterator end() const {
00072                 return m_list.end();
00073             }
00074 
00075             void add_member(const char type, osm_object_id_t ref, const char *role) {
00076                 /* first we resize the vector... */
00077                 m_list.resize(m_list.size()+1);
00078                 /* ...and get an address for the new element... */
00079                 RelationMember *m = &m_list[m_list.size()-1];
00080                 /* ...so that we can directly write into the memory and avoid
00081                 a second copy */
00082                 m->type(type);
00083                 m->ref(ref);
00084                 m->role(role);
00085             }
00086 
00087 #ifdef OSMIUM_WITH_JAVASCRIPT
00088             v8::Local<v8::Object> js_instance() const {
00089                 return JavascriptTemplate::get<JavascriptTemplate>().create_instance((void *)this);
00090             }
00091 
00092             v8::Handle<v8::Value> js_get_member(uint32_t index) {
00093                 return m_list[index].js_instance();
00094             }
00095 
00096             v8::Handle<v8::Array> js_enumerate_members() const {
00097                 v8::HandleScope scope;
00098                 v8::Local<v8::Array> array = v8::Array::New(m_list.size());
00099 
00100                 for (unsigned int i=0; i < m_list.size(); i++) {
00101                     array->Set(i, v8::Integer::New(i));
00102                 }
00103 
00104                 return scope.Close(array);
00105             }
00106 
00107             v8::Handle<v8::Value> js_length() const {
00108                 return v8::Number::New(m_list.size());
00109             }
00110 
00111             struct JavascriptTemplate : public Osmium::Javascript::Template {
00112 
00113                 JavascriptTemplate() : Osmium::Javascript::Template() {
00114                     js_template->SetAccessor(v8::String::NewSymbol("length"), accessor_getter<RelationMemberList, &RelationMemberList::js_length>);
00115                     js_template->SetIndexedPropertyHandler(
00116                         indexed_property_getter<RelationMemberList, &RelationMemberList::js_get_member>,
00117                         0,
00118                         0,
00119                         0,
00120                         property_enumerator<RelationMemberList, &RelationMemberList::js_enumerate_members>
00121                     );
00122                 }
00123 
00124             };
00125 #endif // OSMIUM_WITH_JAVASCRIPT
00126 
00127         private:
00128 
00129             std::vector<RelationMember> m_list;
00130 
00131         }; // class RelationMemberList
00132 
00133     } // namespace OSM
00134 
00135 } // namespace Osmium
00136 
00137 #endif // OSMIUM_OSM_RELATION_MEMBER_LIST_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines