/usr/include/simgear/nasal/cppbind/NasalHash.hxx is in libsimgear-dev 3.4.0-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | ///@file
//
// Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
#ifndef SG_NASAL_HASH_HXX_
#define SG_NASAL_HASH_HXX_
#include "from_nasal.hxx"
#include "to_nasal.hxx"
#include <simgear/structure/map.hxx>
#include <boost/iterator/iterator_facade.hpp>
namespace nasal
{
/**
* Wrapper class for Nasal hashes.
*/
class Hash
{
public:
template<bool> class Entry;
template<bool> class Iterator;
typedef Entry<false> reference;
typedef Entry<true> const_reference;
typedef Iterator<false> iterator;
typedef Iterator<true> const_iterator;
/**
* Create a new Nasal Hash
*
* @param c Nasal context for creating the hash
*/
explicit Hash(naContext c);
/**
* Initialize from an existing Nasal Hash
*
* @param hash Existing Nasal Hash
* @param c Nasal context for creating new Nasal objects
*/
Hash(naRef hash, naContext c);
iterator begin();
iterator end();
const_iterator begin() const;
const_iterator end() const;
/**
* Set member
*
* @param name Member name
* @param ref Reference to Nasal object (naRef)
*/
void set(const std::string& name, naRef ref);
/**
* Set member to anything convertible using to_nasal
*
* @param name Member name
* @param val Value (has to be convertible with to_nasal)
*/
template<class T>
void set(const std::string& name, const T& val)
{
set(name, to_nasal(_context, val));
}
/**
* Get member
*
* @param key Member key
*/
naRef get(naRef key) const;
/**
* Get member
*
* @param name Member name
*/
naRef get(const std::string& name) const;
/**
* Get member converted to given type
*
* @tparam T Type to convert to (using from_nasal)
* @param name Member name
*/
template<class T, class Key>
T get(const Key& name) const
{
BOOST_STATIC_ASSERT(( boost::is_convertible<Key, naRef>::value
|| boost::is_convertible<Key, std::string>::value
));
return from_nasal<T>(_context, get(name));
}
/**
* Get member converted to callable object
*
* @tparam Sig Function signature
* @param name Member name
*/
template<class Sig, class Key>
typename boost::enable_if< boost::is_function<Sig>,
boost::function<Sig>
>::type
get(const Key& name) const
{
BOOST_STATIC_ASSERT(( boost::is_convertible<Key, naRef>::value
|| boost::is_convertible<Key, std::string>::value
));
return from_nasal_helper(_context, get(name), static_cast<Sig*>(0));
}
/**
* Returns the number of entries in the hash
*/
int size() const;
/**
* Get a list of all keys
*/
std::vector<std::string> keys() const;
/**
* Create a new child hash (module)
*
* @param name Name of the new hash inside this hash
*/
Hash createHash(const std::string& name);
/**
* Set a new Nasal context. Eg. in FlightGear the context changes every
* frame, so if using the same Hash instance for multiple frames you have
* to update the context before using the Hash object.
*/
void setContext(naContext context);
/**
* Get Nasal representation of Hash
*/
naRef get_naRef() const;
/**
* Get Nasal vector of keys
*/
naRef get_naRefKeys() const;
/// Hash entry
template<bool is_const>
class Entry
{
public:
typedef typename boost::mpl::if_c<
is_const,
Hash const*,
Hash*
>::type HashPtr;
Entry(HashPtr hash, naRef key):
_hash(hash),
_key(key)
{
assert(hash);
assert(naIsScalar(key));
}
std::string getKey() const
{
if( !_hash || naIsNil(_key) )
return std::string();
return from_nasal<std::string>(_hash->_context, _key);
}
template<class T>
T getValue() const
{
if( !_hash || naIsNil(_key) )
return T();
return _hash->template get<T>(_key);
}
private:
HashPtr _hash;
naRef _key;
};
/// Hash iterator
template<bool is_const>
class Iterator:
public boost::iterator_facade<
Iterator<is_const>,
Entry<is_const>,
boost::bidirectional_traversal_tag,
Entry<is_const>
>
{
public:
typedef typename Entry<is_const>::HashPtr HashPtr;
typedef Entry<is_const> value_type;
Iterator():
_hash(NULL),
_index(0)
{}
Iterator(HashPtr hash, int index):
_hash(hash),
_index(index)
{}
/**
* Convert from iterator to const_iterator or copy within same type
*/
template<bool is_other_const>
Iterator( Iterator<is_other_const> const& other,
typename boost::enable_if_c< is_const || !is_other_const,
void*
>::type = NULL ):
_hash(other._hash),
_index(other._index)
{}
private:
friend class boost::iterator_core_access;
template <bool> friend class Iterator;
HashPtr _hash;
int _index;
template<bool is_other_const>
bool equal(Iterator<is_other_const> const& other) const
{
return _hash == other._hash
&& _index == other._index;
}
void increment() { ++_index; }
void decrement() { --_index; }
value_type dereference() const
{
return value_type(_hash, naVec_get(_hash->get_naRefKeys(), _index));
}
};
protected:
naRef _hash;
naContext _context;
mutable naRef _keys; //< Store vector of keys (for iterators)
};
} // namespace nasal
template<class Value>
simgear::Map<std::string, Value>
from_nasal_helper( naContext c,
naRef ref,
const simgear::Map<std::string, Value>* )
{
nasal::Hash hash = from_nasal_helper(c, ref, static_cast<nasal::Hash*>(0));
simgear::Map<std::string, Value> map;
for(nasal::Hash::const_iterator it = hash.begin(); it != hash.end(); ++it)
map[ it->getKey() ] = it->getValue<Value>();
return map;
}
#endif /* SG_NASAL_HASH_HXX_ */
|