]> git.mxchange.org Git - simgear.git/blob - simgear/nasal/cppbind/NasalHash.hxx
First working version of DOM like Canvas event handling
[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 namespace nasal
26 {
27
28   /**
29    * A Nasal Hash
30    */
31   class Hash
32   {
33     public:
34
35       /**
36        * Create a new Nasal Hash
37        *
38        * @param c   Nasal context for creating the hash
39        */
40       Hash(naContext c);
41
42       /**
43        * Initialize from an existing Nasal Hash
44        *
45        * @param hash  Existing Nasal Hash
46        * @param c     Nasal context for creating new Nasal objects
47        */
48       Hash(naRef hash, naContext c);
49
50       /**
51        * Set member
52        *
53        * @param name    Member name
54        * @param ref     Reference to Nasal object (naRef)
55        */
56       void set(const std::string& name, naRef ref);
57
58       /**
59        * Set member to anything convertible using to_nasal
60        *
61        * @param name    Member name
62        * @param val     Value (has to be convertible with to_nasal)
63        */
64       template<class T>
65       void set(const std::string& name, const T& val)
66       {
67         set(name, to_nasal(_context, val));
68       }
69
70       /**
71        * Get member
72        *
73        * @param name    Member name
74        */
75       naRef get(const std::string& name);
76
77       /**
78        * Get member converted to given type
79        *
80        * @tparam T      Type to convert to (using from_nasal)
81        * @param name    Member name
82        */
83       template<class T>
84       T get(const std::string& name)
85       {
86         return from_nasal<T>(_context, get(name));
87       }
88
89       /**
90        * Create a new child hash (module)
91        *
92        * @param name  Name of the new hash inside this hash
93        */
94       Hash createHash(const std::string& name);
95
96       /**
97        * Set a new Nasal context. Eg. in FlightGear the context changes every
98        * frame, so if using the same Hash instance for multiple frames you have
99        * to update the context before using the Hash object.
100        */
101       void setContext(naContext context);
102
103       /**
104        * Get Nasal representation of Hash
105        */
106       const naRef get_naRef() const;
107
108     protected:
109
110       naRef _hash;
111       naContext _context;
112
113   };
114
115 } // namespace nasal
116
117 #endif /* SG_NASAL_HASH_HXX_ */