Osmium  0.1
include/osmium/osm/way_node_list.hpp
Go to the documentation of this file.
00001 #ifndef OSMIUM_OSM_WAY_NODE_LIST_HPP
00002 #define OSMIUM_OSM_WAY_NODE_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/way_node.hpp>
00028 
00029 namespace Osmium {
00030 
00031     namespace OSM {
00032 
00033         class WayNodeList {
00034 
00035         public:
00036 
00042             static const int default_size = 500;
00043 
00044             WayNodeList(unsigned int size=default_size) : m_list() {
00045                 m_list.reserve(size);
00046             }
00047 
00048             osm_sequence_id_t size() const {
00049                 return m_list.size();
00050             }
00051 
00052             void clear() {
00053                 m_list.clear();
00054             }
00055 
00056             typedef std::vector<WayNode>::iterator iterator;
00057             typedef std::vector<WayNode>::const_iterator const_iterator;
00058             typedef std::vector<WayNode>::reverse_iterator reverse_iterator;
00059             typedef std::vector<WayNode>::const_reverse_iterator const_reverse_iterator;
00060 
00061             iterator begin() {
00062                 return m_list.begin();
00063             }
00064 
00065             const_iterator begin() const {
00066                 return m_list.begin();
00067             }
00068 
00069             iterator end() {
00070                 return m_list.end();
00071             }
00072 
00073             const_iterator end() const {
00074                 return m_list.end();
00075             }
00076 
00077             reverse_iterator rbegin() {
00078                 return m_list.rbegin();
00079             }
00080 
00081             const_reverse_iterator rbegin() const {
00082                 return m_list.rbegin();
00083             }
00084 
00085             reverse_iterator rend() {
00086                 return m_list.rend();
00087             }
00088 
00089             const_reverse_iterator rend() const {
00090                 return m_list.rend();
00091             }
00092 
00093             WayNode& operator[](int i) {
00094                 return m_list[i];
00095             }
00096 
00097             const WayNode& operator[](int i) const {
00098                 return m_list[i];
00099             }
00100 
00101             const WayNode& front() const {
00102                 return m_list.front();
00103             }
00104 
00105             const WayNode& back() const {
00106                 return m_list.back();
00107             }
00108 
00109             bool is_closed() const {
00110                 return m_list.front().ref() == m_list.back().ref();
00111             }
00112 
00113             bool has_position() const {
00114                 if (m_list.empty()) {
00115                     return false;
00116                 } else {
00117                     return m_list.back().has_position();
00118                 }
00119             }
00120 
00121             WayNodeList& add(const WayNode& way_node) {
00122                 m_list.push_back(way_node);
00123                 return *this;
00124             }
00125 
00126             WayNodeList& add(osm_object_id_t ref) {
00127                 m_list.push_back(WayNode(ref));
00128                 return *this;
00129             }
00130 
00131 #ifdef OSMIUM_WITH_JAVASCRIPT
00132             v8::Local<v8::Object> js_instance() const {
00133                 return JavascriptTemplate::get<JavascriptTemplate>().create_instance((void *)this);
00134             }
00135 
00136             v8::Handle<v8::Value> js_length() const {
00137                 return v8::Number::New(m_list.size());
00138             }
00139 
00140             v8::Handle<v8::Value> js_get_node_id(uint32_t index) const {
00141                 return v8::Number::New(m_list[index].ref());
00142             }
00143 
00144             v8::Handle<v8::Array> js_enumerate_nodes() const {
00145                 v8::HandleScope scope;
00146                 v8::Local<v8::Array> array = v8::Array::New(m_list.size());
00147 
00148                 for (unsigned int i=0; i < m_list.size(); ++i) {
00149                     array->Set(i, v8::Integer::New(i));
00150                 }
00151 
00152                 return scope.Close(array);
00153             }
00154 
00155             struct JavascriptTemplate : public Osmium::Javascript::Template {
00156 
00157                 JavascriptTemplate() : Osmium::Javascript::Template() {
00158                     js_template->SetAccessor(v8::String::NewSymbol("length"), accessor_getter<WayNodeList, &WayNodeList::js_length>);
00159                     js_template->SetIndexedPropertyHandler(
00160                         indexed_property_getter<WayNodeList, &WayNodeList::js_get_node_id>,
00161                         0,
00162                         0,
00163                         0,
00164                         property_enumerator<WayNodeList, &WayNodeList::js_enumerate_nodes>
00165                     );
00166                 }
00167 
00168             };
00169 #endif // OSMIUM_WITH_JAVASCRIPT
00170 
00171         private:
00172 
00173             std::vector<WayNode> m_list;
00174 
00175 #ifdef OSMIUM_WITH_JAVASCRIPT
00176             v8::Local<v8::Object> m_js_instance;
00177 #endif // OSMIUM_WITH_JAVASCRIPT
00178 
00179         }; // class WayNodeList
00180 
00181     } // namespace OSM
00182 
00183 } // namespace Osmium
00184 
00185 #endif // OSMIUM_OSM_WAY_NODE_LIST_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines