Wolframe, 0.0.3

structDescription.hpp
Go to the documentation of this file.
1 /************************************************************************
2 Copyright (C) 2011 - 2014 Project Wolframe.
3 All rights reserved.
4 
5 This file is part of Project Wolframe.
6 
7 Commercial Usage
8 Licensees holding valid Project Wolframe Commercial licenses may
9 use this file in accordance with the Project Wolframe
10 Commercial License Agreement provided with the Software or,
11 alternatively, in accordance with the terms contained
12 in a written agreement between the licensee and Project Wolframe.
13 
14 GNU General Public License Usage
15 Alternatively, you can redistribute this file and/or modify it
16 under the terms of the GNU General Public License as published by
17 the Free Software Foundation, either version 3 of the License, or
18 (at your option) any later version.
19 
20 Wolframe is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
24 
25 You should have received a copy of the GNU General Public License
26 along with Wolframe. If not, see <http://www.gnu.org/licenses/>.
27 
28 If you have questions regarding the use of this file, please contact
29 Project Wolframe.
30 
31 ************************************************************************/
35 #ifndef _Wolframe_SERIALIZE_STRUCT_DESCRIPTION_HPP_INCLUDED
36 #define _Wolframe_SERIALIZE_STRUCT_DESCRIPTION_HPP_INCLUDED
42 #include <exception>
43 #include <stdexcept>
44 #include <sstream>
45 
46 namespace _Wolframe {
47 namespace serialize {
48 
52 template <class Structure>
54 {
57  :StructDescriptionBase( &constructor, &destructor, getTypename<Structure>(), 0, sizeof(Structure), IntrusiveProperty<Structure>::type(), &IntrusiveParser<Structure>::parse, &IntrusiveSerializer<Structure>::fetch, NoRequirement){}
58 
63  template <typename Element>
64  StructDescription& operator()( const char* tag, Element Structure::*eptr)
65  {
69  std::size_t pp = (std::size_t)&(((Structure*)0)->*eptr);
70 
71  StructDescriptionBase e( getTypename<Element>(), pp, sizeof(Element), type_, parse_, fetch_, NoRequirement);
72  if (find( tag) != end())
73  {
74  std::ostringstream err;
75  err << "duplicate definition of " << tag << " in structure";
76  throw std::runtime_error( err.str().c_str());
77  }
78  define( tag, e);
79  return *this;
80  }
81 
84  {
85  if (last().optional()) throw std::logic_error( "ambiguous mandatory/optional declaration");
87  return *this;
88  }
89 
92  {
93  if (last().mandatory()) throw std::logic_error( "ambiguous mandatory/optional declaration");
95  return *this;
96  }
97 
100  {
102  return *this;
103  }
104 
106  static bool constructor( void* obj)
107  {
109  struct StructureShell :public Structure
110  {
111  void* operator new( std::size_t num_bytes, void* obj) throw (std::bad_alloc)
112  {
113  if (sizeof(StructureShell) != num_bytes) throw std::bad_alloc();
114  return obj;
115  }
116  };
117  try
118  {
119  new (obj) StructureShell();
120  return true;
121  }
122  catch (const std::exception&)
123  {
124  return false;
125  }
126  }
127 
129  static void destructor( void* obj)
130  {
131  ((Structure*)obj)->~Structure();
132  }
133 
135  template <class Element>
136  static const char* getTypename()
137  {
138  const char* typ = 0;
139  try
140  {
141  typ = typeid(Element).name();
142  }
143  catch (std::bad_typeid)
144  {}
145  return typ;
146  }
147 };
148 
149 }}// end namespace
150 #endif
151 
Fetch fetch() const
Non intrusive serializer call (fetch the next element) function of this sructure. ...
Definition: structDescriptionBase.hpp:72
static StructDescriptionBase::ElementType type()
Definition: intrusiveProperty.hpp:54
Defines the intrusive implementation of the serialization of structures.
ElementType type() const
Get the element kind (type of access) of this structure or atomic element.
Definition: structDescriptionBase.hpp:123
ElementType
Type of a structure element.
Definition: structDescriptionBase.hpp:56
no explicit condition
Definition: structDescriptionBase.hpp:78
static const char * getTypename()
Get the name of the type 'Element' if available.
Definition: structDescription.hpp:136
StructDescriptionBase & last()
Definition: structDescriptionBase.hpp:186
defines the type traits for the intrusive part of serialization/deserialization
Base class for structure description used for introspection in serialization/deserialization.
Definition: structDescriptionBase.hpp:51
forward declaration
Definition: intrusiveParser.hpp:55
Map::const_iterator end() const
Get the end iterator on the elements of this structure.
Definition: structDescriptionBase.hpp:138
Maps the traits based on structure element type properties to an enumeration value.
Definition: intrusiveProperty.hpp:44
StructDescription & operator()(const char *tag, Element Structure::*eptr)
Operator to build the structure description element by element.
Definition: structDescription.hpp:64
static void destructor(void *obj)
Destroy an object of this kind allocated with 'constructor(void*)' at the location 'obj'...
Definition: structDescription.hpp:129
StructDescription & optional()
Define the last structure element defined to be optional always (independent of validation mode) ...
Definition: structDescription.hpp:91
Defines the intrusive implementation of the deserialization of objects interfaced as TypedInputFilter...
void requirement(ElementRequirement requirement_)
Set element occurrence requirement.
Definition: structDescriptionBase.hpp:183
forward declaration
Definition: intrusiveSerializer.hpp:52
bool(* Parse)(langbind::TypedInputFilter &flt, Context &ctx, ParseStateStack &stk)
Definition: structDescriptionBase.hpp:64
static bool constructor(void *obj)
Construct an object of this kind at an allocated location (with base pointer 'obj') ...
Definition: structDescription.hpp:106
Parse parse() const
Non intrusive parser call function of this sructure.
Definition: structDescriptionBase.hpp:70
Map::const_iterator find(const std::string &name) const
Find an element in this structure by name.
void defineEndOfAttributes()
Define the number of attributes of a struct.
Definition: structDescriptionBase.hpp:158
Intrusive description of a filter/form map.
Definition: structDescription.hpp:53
StructDescription & mandatory()
Define the last structure element defined to be mandatory always (independent of validation mode) ...
Definition: structDescription.hpp:83
Defines the intrusive implementation of some parsing element properties.
element is always mandatory, even if structure is filled and validated in "relaxed" mode ...
Definition: structDescriptionBase.hpp:79
StructDescription()
Constructor.
Definition: structDescription.hpp:56
void define(const std::string &name, const StructDescriptionBase &dd)
Define an element of this structure.
Definition: structDescriptionBase.hpp:142
StructDescription & operator--(int)
Define the elements defined until now to be attributes and the subsequent elements defined to be cont...
Definition: structDescription.hpp:99
Defines the non intrusive base class of serialization/deserialization of objects interfaced as TypedI...
element is always optional, even if structure is filled and validated in "strict" mode ...
Definition: structDescriptionBase.hpp:80
bool(* Fetch)(Context &ctx, SerializeStateStack &stk)
Definition: structDescriptionBase.hpp:65