PropertiesToString.h
Go to the documentation of this file.
00001 /* 00002 * Copyright 2006-2009 Savarese Software Research Corporation 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * https://www.savarese.com/software/ApacheLicense-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00022 #ifndef __SSRC_WSPR_UTILITY_PROPERTIES_TO_STRING_H 00023 #define __SSRC_WSPR_UTILITY_PROPERTIES_TO_STRING_H 00024 00025 #include <iosfwd> 00026 00027 #include <ssrc/wispers/utility/Properties.h> 00028 #include <ssrc/wispers/utility/ToString.h> 00029 00030 __BEGIN_NS_SSRC_WSPR_UTILITY 00031 00032 void escape_lua(string & result, 00033 const char *text, const unsigned int text_size); 00034 00035 inline void escape_lua(string & result, const string & text) { 00036 escape_lua(result, text.c_str(), text.size()); 00037 } 00038 00039 inline string escape_lua(const string & text) { 00040 string result; 00041 escape_lua(result, text); 00042 return result; 00043 } 00044 00045 // Converts Properties to a string representation that can be read by a 00046 // Lua interpreter. Probably should be moved to the lua package because 00047 // we could also have JSON and other string representations. 00048 class PropertiesToString : public boost::static_visitor<void> { 00049 public: 00050 enum ToFormat { ToDefault = 0, ToLua = 0, ToJSON = 1 }; 00051 00052 private: 00053 const char * const * const _token; 00054 std::ostream & _out; 00055 00056 public: 00057 00058 explicit PropertiesToString(std::ostream & out, 00059 const ToFormat to_format = ToDefault); 00060 00061 void operator()(const primitive_property_vector & v) const; 00062 void operator()(const property_vector & v) const; 00063 00064 void operator()(const bool v) const { 00065 _out << (v ? "true" : "false"); 00066 } 00067 00068 // Convert 64-bit integers to string representation because Lua/JavaScript 00069 // usually support only 32-bit integers. 00070 void operator()(const std::int64_t v) const { 00071 _out << "\"" << v << "\""; 00072 } 00073 00074 void operator()(const std::uint64_t v) const { 00075 _out << "\"" << v << "\""; 00076 } 00077 00078 // Convert 32-bit unsigned integers to equivalent 2's complement 00079 // signed integer, allowing the original value to be recovered via 00080 // a cast when read back. 00081 void operator()(const std::uint32_t v) const { 00082 _out << static_cast<std::int32_t>(v); 00083 } 00084 00085 void operator()(const string & s) const { 00086 _out << "\"" << escape_lua(s) << "\""; 00087 } 00088 00089 void operator()(const Properties & properties) const { 00090 if(properties.is_leaf()) { 00091 const property_type *v = properties.value(); 00092 if(v != 0) { 00093 boost::apply_visitor(*this, *v); 00094 } 00095 } else { 00096 _out << "{"; 00097 properties.visit(*this); 00098 _out << "}"; 00099 } 00100 } 00101 00102 template<typename T> 00103 void operator()(const T & v) const { 00104 _out << v; 00105 } 00106 00107 void enter(const std::string & key, const Properties *props, 00108 const bool is_last_child) const; 00109 00110 void leave(const std::string & key, const Properties *props, 00111 const bool is_last_child) const 00112 { 00113 if(props->is_leaf()) { 00114 if(!is_last_child) { 00115 _out << ","; 00116 } 00117 } else { 00118 if(is_last_child) { 00119 _out << "}"; 00120 } else { 00121 _out << "},"; 00122 } 00123 } 00124 } 00125 }; 00126 00127 inline 00128 std::ostream & operator<<(std::ostream & out, const Properties & properties) { 00129 PropertiesToString tostring(out); 00130 tostring(properties); 00131 return out; 00132 } 00133 00134 __END_NS_SSRC_WSPR_UTILITY 00135 00136 #endif
Copyright © 2006-2011 Savarese Software Research Corporation. All rights reserved.