]> git.mxchange.org Git - simgear.git/blob - simgear/props/PropertyBasedElement.cxx
cppbind: expose SGRect as [x, y, w, h]
[simgear.git] / simgear / props / PropertyBasedElement.cxx
1 // Base class for elements of property controlled subsystems
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 #include "PropertyBasedElement.hxx"
20 #include <boost/algorithm/string/predicate.hpp>
21
22 namespace simgear
23 {
24
25   //----------------------------------------------------------------------------
26   PropertyBasedElement::PropertyBasedElement(SGPropertyNode* node):
27     _node(node)
28   {
29     _node->addChangeListener(this);
30   }
31
32   //----------------------------------------------------------------------------
33   PropertyBasedElement::~PropertyBasedElement()
34   {
35     onDestroy();
36     removeListener();
37   }
38
39   //----------------------------------------------------------------------------
40   void PropertyBasedElement::removeListener()
41   {
42     _node->removeChangeListener(this);
43   }
44
45   //----------------------------------------------------------------------------
46   void PropertyBasedElement::destroy()
47   {
48     if( !_node )
49       return;
50
51     // TODO check if really not in use anymore
52     if( _node->getParent() )
53       _node->getParent()
54            ->removeChild(_node->getName(), _node->getIndex());
55   }
56
57   //----------------------------------------------------------------------------
58   SGConstPropertyNode_ptr PropertyBasedElement::getProps() const
59   {
60     return _node;
61   }
62
63   //----------------------------------------------------------------------------
64   SGPropertyNode_ptr PropertyBasedElement::getProps()
65   {
66     return _node;
67   }
68
69   //----------------------------------------------------------------------------
70   bool PropertyBasedElement::hasDataProp(const std::string& name) const
71   {
72     return getDataProp<SGPropertyNode*>(name) != NULL;
73   }
74
75   //----------------------------------------------------------------------------
76   void PropertyBasedElement::removeDataProp(const std::string& name)
77   {
78     SGPropertyNode* node = getDataProp<SGPropertyNode*>(name);
79     if( node )
80       _node->removeChild(node);
81   }
82
83   //----------------------------------------------------------------------------
84   static const std::string DATA_PREFIX("data-");
85
86   //----------------------------------------------------------------------------
87   std::string PropertyBasedElement::dataPropToAttrName(const std::string& name)
88   {
89     // http://www.w3.org/TR/html5/dom.html#attr-data-*
90     //
91     // 3. Insert the string data- at the front of name.
92
93     std::string attr_name;
94     for( std::string::const_iterator cur = name.begin();
95                                      cur != name.end();
96                                    ++cur )
97     {
98       // If name contains a "-" (U+002D) character followed by a lowercase ASCII
99       // letter, throw a SyntaxError exception and abort these steps.
100       if( *cur == '-' )
101       {
102         std::string::const_iterator next = cur + 1;
103         if( next != name.end() && islower(*next) )
104           return std::string();
105       }
106
107       // For each uppercase ASCII letter in name, insert a "-" (U+002D)
108       // character before the character and replace the character with the same
109       // character converted to ASCII lowercase.
110       if( isupper(*cur) )
111       {
112         attr_name.push_back('-');
113         attr_name.push_back( tolower(*cur) );
114       }
115       else
116         attr_name.push_back( *cur );
117     }
118     return DATA_PREFIX + attr_name;
119   }
120
121   //----------------------------------------------------------------------------
122   std::string PropertyBasedElement::attrToDataPropName(const std::string& name)
123   {
124     // http://www.w3.org/TR/html5/dom.html#attr-data-*
125     //
126     // For each "-" (U+002D) character in the name that is followed by a
127     // lowercase ASCII letter, remove the "-" (U+002D) character and replace the
128     // character that followed it by the same character converted to ASCII
129     // uppercase.
130
131     if( !boost::starts_with(name, DATA_PREFIX) )
132       return std::string();
133
134     std::string data_name;
135     for( std::string::const_iterator cur = name.begin() + DATA_PREFIX.length();
136                                      cur != name.end();
137                                    ++cur )
138     {
139       std::string::const_iterator next = cur + 1;
140       if( *cur == '-' && next != name.end() && islower(*next) )
141       {
142         data_name.push_back( toupper(*next) );
143         cur = next;
144       }
145       else
146         data_name.push_back(*cur);
147     }
148     return data_name;
149   }
150
151 } // namespace simgear