Osmium
0.1
|
00001 #ifndef OSMIUM_OSM_OBJECT_HPP 00002 #define OSMIUM_OSM_OBJECT_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 00029 #include <cstdlib> 00030 #include <stdexcept> 00031 #include <assert.h> 00032 #include <time.h> 00033 #include <boost/tr1/memory.hpp> 00034 using std::tr1::shared_ptr; 00035 00036 #ifdef OSMIUM_WITH_SHPLIB 00037 # include <shapefil.h> 00038 #endif // OSMIUM_WITH_SHPLIB 00039 00040 #include <osmium/osm/types.hpp> 00041 #include <osmium/osm/tag_list.hpp> 00042 #include <osmium/utils/timestamp.hpp> 00043 00044 namespace Osmium { 00045 00046 namespace OSM { 00047 00051 class Object { 00052 00053 public: 00054 00055 static const int max_characters_username = 255; 00056 static const int max_utf16_length_username = 2 * (max_characters_username + 1); 00057 00058 static const int max_length_username = 255 * 4 + 1; 00059 00060 osm_object_id_t id() const { 00061 return m_id; 00062 } 00063 00064 Object& id(osm_object_id_t id) { 00065 m_id = id; 00066 return *this; 00067 } 00068 00069 Object& id(const char* id) { 00070 m_id = atol(id); 00071 return *this; 00072 } 00073 00074 osm_version_t version() const { 00075 return m_version; 00076 } 00077 00078 Object& version(osm_version_t version) { 00079 m_version = version; 00080 return *this; 00081 } 00082 00083 Object& version(const char* version) { 00084 m_version = atoi(version); 00085 return *this; 00086 } 00087 00088 osm_changeset_id_t changeset() const { 00089 return m_changeset; 00090 } 00091 00092 Object& changeset(osm_changeset_id_t changeset) { 00093 m_changeset = changeset; 00094 return *this; 00095 } 00096 00097 Object& changeset(const char* changeset) { 00098 m_changeset = atol(changeset); 00099 return *this; 00100 } 00101 00102 osm_user_id_t uid() const { 00103 return m_uid; 00104 } 00105 00106 Object& uid(osm_user_id_t uid) { 00107 m_uid = uid; 00108 return *this; 00109 } 00110 00111 Object& uid(const char* uid) { 00112 m_uid = atol(uid); 00113 return *this; 00114 } 00115 00116 time_t timestamp() const { 00117 return m_timestamp; 00118 } 00119 00120 time_t endtime() const { 00121 return m_endtime; 00122 } 00123 00128 std::string timestamp_as_string() const { 00129 return Osmium::Utils::Timestamp::to_iso(m_timestamp); 00130 } 00131 00136 std::string endtime_as_string() const { 00137 return Osmium::Utils::Timestamp::to_iso(m_endtime); 00138 } 00139 00145 Object& timestamp(time_t timestamp) { 00146 m_timestamp = timestamp; 00147 return *this; 00148 } 00149 00156 Object& endtime(time_t timestamp) { 00157 m_endtime = timestamp; 00158 return *this; 00159 } 00160 00169 Object& timestamp(const char* timestamp) { 00170 m_timestamp = Osmium::Utils::Timestamp::parse_iso(timestamp); 00171 return *this; 00172 } 00173 00178 const char* user() const { 00179 return m_user; 00180 } 00181 00187 Object& user(const char* user) { 00188 if (! memccpy(m_user, user, 0, max_length_username)) { 00189 m_user[0] = '\0'; 00190 throw std::length_error("user name too long"); 00191 } 00192 return *this; 00193 } 00194 00200 bool visible() const { 00201 return m_visible; 00202 } 00203 00209 Object& visible(bool visible) { 00210 m_visible = visible; 00211 return *this; 00212 } 00213 00219 Object& visible(const char* visible) { 00220 if (!strcmp(visible, "false")) { 00221 m_visible = false; 00222 } 00223 return *this; 00224 } 00225 00226 virtual osm_object_type_t get_type() const = 0; 00227 00233 void set_attribute(const char* attr, const char* value) { 00234 if (!strcmp(attr, "id")) { 00235 id(value); 00236 } else if (!strcmp(attr, "version")) { 00237 version(value); 00238 } else if (!strcmp(attr, "changeset")) { 00239 changeset(value); 00240 } else if (!strcmp(attr, "timestamp")) { 00241 timestamp(value); 00242 } else if (!strcmp(attr, "uid")) { 00243 uid(value); 00244 } else if (!strcmp(attr, "user")) { 00245 user(value); 00246 } else if (!strcmp(attr, "visible")) { 00247 visible(value); 00248 } 00249 } 00250 00251 const TagList& tags() const { 00252 return m_tags; 00253 } 00254 00255 TagList& tags() { 00256 return m_tags; 00257 } 00258 00259 void tags(TagList& tags) { 00260 m_tags = tags; 00261 } 00262 00263 #ifdef OSMIUM_WITH_JAVASCRIPT 00264 v8::Persistent<v8::Object> js_object_instance; 00265 00266 v8::Persistent<v8::Object> get_instance() const { 00267 return js_object_instance; 00268 } 00269 00270 v8::Handle<v8::Value> js_id() const { 00271 return v8::Number::New(id()); 00272 } 00273 00274 v8::Handle<v8::Value> js_version() const { 00275 return v8::Integer::New(version()); 00276 } 00277 00278 v8::Handle<v8::Value> js_timestamp_as_string() const { 00279 return v8::String::New(timestamp_as_string().c_str()); 00280 } 00281 00282 v8::Handle<v8::Value> js_uid() const { 00283 return v8::Integer::New(uid()); 00284 } 00285 00286 v8::Handle<v8::Value> js_user() const { 00287 return Osmium::utf8_to_v8_String<max_utf16_length_username>(user()); 00288 } 00289 00290 v8::Handle<v8::Value> js_changeset() const { 00291 return v8::Number::New(changeset()); 00292 } 00293 00294 v8::Handle<v8::Value> js_visible() const { 00295 return v8::Boolean::New(visible()); 00296 } 00297 00298 v8::Handle<v8::Value> js_tags() const { 00299 return tags().js_instance(); 00300 } 00301 00302 struct JavascriptTemplate : public Osmium::Javascript::Template { 00303 00304 JavascriptTemplate() : Osmium::Javascript::Template() { 00305 js_template->SetAccessor(v8::String::NewSymbol("id"), accessor_getter<Object, &Object::js_id>); 00306 js_template->SetAccessor(v8::String::NewSymbol("version"), accessor_getter<Object, &Object::js_version>); 00307 js_template->SetAccessor(v8::String::NewSymbol("timestamp"), accessor_getter<Object, &Object::js_timestamp_as_string>); 00308 js_template->SetAccessor(v8::String::NewSymbol("uid"), accessor_getter<Object, &Object::js_uid>); 00309 js_template->SetAccessor(v8::String::NewSymbol("user"), accessor_getter<Object, &Object::js_user>); 00310 js_template->SetAccessor(v8::String::NewSymbol("changeset"), accessor_getter<Object, &Object::js_changeset>); 00311 js_template->SetAccessor(v8::String::NewSymbol("tags"), accessor_getter<Object, &Object::js_tags>); 00312 js_template->SetAccessor(v8::String::NewSymbol("visible"), accessor_getter<Object, &Object::js_visible>); 00313 } 00314 00315 }; 00316 #endif // OSMIUM_WITH_JAVASCRIPT 00317 00318 protected: 00319 00320 Object() : 00321 m_id(0), 00322 m_version(0), 00323 m_changeset(0), 00324 m_timestamp(0), 00325 m_endtime(0), 00326 m_uid(-1), // to be compatible with Osmosis we use -1 for unknown user id 00327 m_visible(true), 00328 m_tags() { 00329 m_user[0] = '\0'; 00330 } 00331 00332 Object(const Object &o) { 00333 m_id = o.m_id; 00334 m_version = o.m_version; 00335 m_uid = o.m_uid; 00336 m_changeset = o.m_changeset; 00337 m_timestamp = o.m_timestamp; 00338 m_endtime = o.m_endtime; 00339 m_tags = o.m_tags; 00340 m_visible = o.m_visible; 00341 strncpy(m_user, o.m_user, max_length_username); 00342 } 00343 00344 virtual ~Object() { 00345 #ifdef OSMIUM_WITH_JAVASCRIPT 00346 js_object_instance.Dispose(); 00347 #endif // OSMIUM_WITH_JAVASCRIPT 00348 } 00349 00350 private: 00351 00352 osm_object_id_t m_id; 00353 osm_version_t m_version; 00354 osm_changeset_id_t m_changeset; 00355 time_t m_timestamp; 00356 time_t m_endtime; 00357 osm_user_id_t m_uid; 00358 char m_user[max_length_username]; 00359 bool m_visible; 00360 00361 TagList m_tags; 00362 00363 }; // class Object 00364 00365 } // namespace OSM 00366 00367 } // namespace Osmium 00368 00369 #endif // OSMIUM_OSM_OBJECT_HPP