]> git.mxchange.org Git - simgear.git/blob - simgear/nasal/cppbind/NasalHash.hxx
cppbind.Ghost: more member overloads.
[simgear.git] / simgear / nasal / cppbind / NasalHash.hxx
1 ///@file Wrapper class for Nasal hashes
2 //
3 // Copyright (C) 2012  Thomas Geymayer <tomgey@gmail.com>
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Library General Public
7 // License as published by the Free Software Foundation; either
8 // version 2 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // Library General Public License for more details.
14 //
15 // You should have received a copy of the GNU Library General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
18
19 #ifndef SG_NASAL_HASH_HXX_
20 #define SG_NASAL_HASH_HXX_
21
22 #include "from_nasal.hxx"
23 #include "to_nasal.hxx"
24
25 #include <simgear/structure/map.hxx>
26
27 namespace nasal
28 {
29
30   /**
31    * A Nasal Hash
32    */
33   class Hash
34   {
35     public:
36
37       /**
38        * Create a new Nasal Hash
39        *
40        * @param c   Nasal context for creating the hash
41        */
42       Hash(naContext c);
43
44       /**
45        * Initialize from an existing Nasal Hash
46        *
47        * @param hash  Existing Nasal Hash
48        * @param c     Nasal context for creating new Nasal objects
49        */
50       Hash(naRef hash, naContext c);
51
52       /**
53        * Set member
54        *
55        * @param name    Member name
56        * @param ref     Reference to Nasal object (naRef)
57        */
58       void set(const std::string& name, naRef ref);
59
60       /**
61        * Set member to anything convertible using to_nasal
62        *
63        * @param name    Member name
64        * @param val     Value (has to be convertible with to_nasal)
65        */
66       template<class T>
67       void set(const std::string& name, const T& val)
68       {
69         set(name, to_nasal(_context, val));
70       }
71
72       /**
73        * Get member
74        *
75        * @param name    Member name
76        */
77       naRef get(const std::string& name);
78
79       /**
80        * Get member converted to given type
81        *
82        * @tparam T      Type to convert to (using from_nasal)
83        * @param name    Member name
84        */
85       template<class T>
86       T get(const std::string& name)
87       {
88         return from_nasal<T>(_context, get(name));
89       }
90
91       /**
92        * Get member converted to callable object
93        *
94        * @tparam Sig    Function signature
95        * @param name    Member name
96        */
97       template<class Sig>
98       typename boost::enable_if< boost::is_function<Sig>,
99                                  boost::function<Sig>
100                                >::type
101       get(const std::string& name)
102       {
103         return from_nasal_helper(_context, get(name), static_cast<Sig*>(0));
104       }
105
106       /**
107        * Get a list of all keys
108        */
109       std::vector<std::string> keys() const;
110
111       /**
112        * Create a new child hash (module)
113        *
114        * @param name  Name of the new hash inside this hash
115        */
116       Hash createHash(const std::string& name);
117
118       /**
119        * Set a new Nasal context. Eg. in FlightGear the context changes every
120        * frame, so if using the same Hash instance for multiple frames you have
121        * to update the context before using the Hash object.
122        */
123       void setContext(naContext context);
124
125       /**
126        * Get Nasal representation of Hash
127        */
128       naRef get_naRef() const;
129
130     protected:
131
132       naRef _hash;
133       naContext _context;
134
135   };
136
137 } // namespace nasal
138
139 template<class Value>
140 simgear::Map<std::string, Value>
141 from_nasal_helper( naContext c,
142                    naRef ref,
143                    const simgear::Map<std::string, Value>* )
144 {
145
146   nasal::Hash hash = from_nasal_helper(c, ref, static_cast<nasal::Hash*>(0));
147   std::vector<std::string> const& keys = hash.keys();
148
149   simgear::Map<std::string, Value> map;
150   for(size_t i = 0; i < keys.size(); ++i)
151     map[ keys[i] ] = hash.get<Value>(keys[i]);
152
153   return map;
154 }
155
156 #endif /* SG_NASAL_HASH_HXX_ */