Wolframe, 0.0.3

structDescriptionBase.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 ************************************************************************/
34 #ifndef _Wolframe_SERIALIZE_STRUCT_DESCRIPTION_BASE_HPP_INCLUDED
35 #define _Wolframe_SERIALIZE_STRUCT_DESCRIPTION_BASE_HPP_INCLUDED
36 #include "filter/typedfilter.hpp"
37 #include "types/variant.hpp"
38 #include "serialize/mapContext.hpp"
41 #include <cstddef>
42 #include <string>
43 #include <vector>
44 #include <limits>
45 
46 namespace _Wolframe {
47 namespace serialize {
48 
52 {
53 public:
57  {
61  };
62 
63  typedef std::vector<std::pair<std::string,StructDescriptionBase> > Map;
64  typedef bool (*Parse)( langbind::TypedInputFilter& flt, Context& ctx, ParseStateStack& stk);
65  typedef bool (*Fetch)( Context& ctx, SerializeStateStack& stk);
66  typedef bool (*Constructor)( void* obj);
67  typedef void (*Destructor)( void* obj);
68 
70  Parse parse() const {return m_parse;}
72  Fetch fetch() const {return m_fetch;}
73 
77  {
81  };
83  StructDescriptionBase( Constructor c, Destructor d, const char* tn, std::size_t os, std::size_t sz, ElementType t, Parse pa, Fetch pr, ElementRequirement req_);
85  StructDescriptionBase( const char* tn, std::size_t os, std::size_t sz, ElementType t, Parse pa, Fetch pr, ElementRequirement req_);
90 
92  bool parse( void* obj, langbind::TypedInputFilter& in, Context& ctx, ParseStateStack& stk) const;
93 
96  bool setAtomicValue( void* obj, std::size_t idx, const std::string& value) const;
97 
99  bool init( void* obj) const
100  {
101  return (m_constructor)?m_constructor( obj):true;
102  }
103 
105  void done( void* obj) const
106  {
107  if (m_destructor) m_destructor( obj);
108  }
109 
111  std::size_t size() const
112  {
113  return m_size;
114  }
115 
117  std::size_t ofs() const
118  {
119  return m_ofs;
120  }
121 
124  {
125  return m_type;
126  }
127 
129  Map::const_iterator find( const std::string& name) const;
131  Map::const_iterator find_cis( const std::string& name) const;
133  std::string names( const char* sep) const;
134 
136  Map::const_iterator begin() const {return m_elem.begin();}
138  Map::const_iterator end() const {return m_elem.end();}
139 
142  void define( const std::string& name, const StructDescriptionBase& dd)
143  {
144  m_elem.push_back( Map::value_type(name,dd));
145  }
146 
150  std::size_t nof_attributes() const
151  {
152  return m_nof_attributes;
153  }
154 
159  {
160  m_nof_attributes = m_elem.size();
161  }
162 
165  std::size_t nof_elements() const
166  {
167  return m_elem.size();
168  }
169 
171  const char* typeName() const
172  {
173  return m_typename;
174  }
175 
178  bool mandatory() const {return m_requirement == Mandatory;}
181  bool optional() const {return m_requirement == Optional;}
183  void requirement( ElementRequirement requirement_){m_requirement = requirement_;}
184 
185 protected:
187  {
188  if (m_elem.empty()) throw std::logic_error( "access of last element in empty structure");
189  return m_elem.back().second;
190  }
191 
192 private:
195  const char* m_typename;
196  std::size_t m_ofs;
197  std::size_t m_size;
198  std::size_t m_nof_attributes;
204 };
205 
209 {
211 };
212 
213 }}//namespace
214 #endif
215 
State stack for an initializer of a structure.
Definition: parseStack.hpp:80
Fetch fetch() const
Non intrusive serializer call (fetch the next element) function of this sructure. ...
Definition: structDescriptionBase.hpp:72
void(* Destructor)(void *obj)
Definition: structDescriptionBase.hpp:67
std::string names(const char *sep) const
Return the element names of this structure as string for error messages.
Atomic element type.
Definition: structDescriptionBase.hpp:58
ElementType type() const
Get the element kind (type of access) of this structure or atomic element.
Definition: structDescriptionBase.hpp:123
std::size_t m_nof_attributes
Definition: structDescriptionBase.hpp:198
ElementType
Type of a structure element.
Definition: structDescriptionBase.hpp:56
no explicit condition
Definition: structDescriptionBase.hpp:78
StructDescriptionBase & last()
Definition: structDescriptionBase.hpp:186
Base class for structure description used for introspection in serialization/deserialization.
Definition: structDescriptionBase.hpp:51
Map::const_iterator begin() const
Get the start iterator on the elements of this structure.
Definition: structDescriptionBase.hpp:136
ElementRequirement
Constraints definition of a structure element.
Definition: structDescriptionBase.hpp:76
std::size_t size() const
Get the allocation size of this structure in bytes.
Definition: structDescriptionBase.hpp:111
StructDescriptionBase()
Default constructor.
const char * typeName() const
Get the type name of this structure.
Definition: structDescriptionBase.hpp:171
Map::const_iterator end() const
Get the end iterator on the elements of this structure.
Definition: structDescriptionBase.hpp:138
static const serialize::StructDescriptionBase * getStructDescription()
std::size_t m_ofs
Definition: structDescriptionBase.hpp:196
Parse m_parse
Definition: structDescriptionBase.hpp:201
bool init(void *obj) const
Initialize the structure referenced with 'obj'.
Definition: structDescriptionBase.hpp:99
void requirement(ElementRequirement requirement_)
Set element occurrence requirement.
Definition: structDescriptionBase.hpp:183
bool setAtomicValue(void *obj, std::size_t idx, const std::string &value) const
Initializes an atomic element in a structure.
bool(* Parse)(langbind::TypedInputFilter &flt, Context &ctx, ParseStateStack &stk)
Definition: structDescriptionBase.hpp:64
Defines the parsing stack for serialization of objects.
ElementRequirement m_requirement
Definition: structDescriptionBase.hpp:203
Parse parse() const
Non intrusive parser call function of this sructure.
Definition: structDescriptionBase.hpp:70
Variant value type.
const char * m_typename
Definition: structDescriptionBase.hpp:195
std::size_t m_size
Definition: structDescriptionBase.hpp:197
Map::const_iterator find(const std::string &name) const
Find an element in this structure by name.
ElementType m_type
Definition: structDescriptionBase.hpp:200
std::vector< std::pair< std::string, StructDescriptionBase > > Map
Definition: structDescriptionBase.hpp:63
void defineEndOfAttributes()
Define the number of attributes of a struct.
Definition: structDescriptionBase.hpp:158
std::size_t ofs() const
Get the offset of this structure inside the holding parent structure.
Definition: structDescriptionBase.hpp:117
Array of structures or atomic values.
Definition: structDescriptionBase.hpp:60
bool(* Constructor)(void *obj)
Definition: structDescriptionBase.hpp:66
Defines the data structure holding the global serialization state variables (without the stack) ...
Defines the parsing stack for deserialization.
element is always mandatory, even if structure is filled and validated in "relaxed" mode ...
Definition: structDescriptionBase.hpp:79
Map::const_iterator find_cis(const std::string &name) const
Case insensitive find an element in this structure by name.
bool mandatory() const
Find out if the element in the structure is mandatory.
Definition: structDescriptionBase.hpp:178
std::vector< SerializeState > SerializeStateStack
State stack for an iterator on a structure (serializer)
Definition: serializeStack.hpp:79
Constructor m_constructor
Definition: structDescriptionBase.hpp:193
void define(const std::string &name, const StructDescriptionBase &dd)
Define an element of this structure.
Definition: structDescriptionBase.hpp:142
Structure with fixed number of named elements.
Definition: structDescriptionBase.hpp:59
Destructor m_destructor
Definition: structDescriptionBase.hpp:194
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
Fetch m_fetch
Definition: structDescriptionBase.hpp:202
Typed interface for input/output filter.
bool optional() const
Find out if the element in the structure is optional.
Definition: structDescriptionBase.hpp:181
Global state variables of a running serialization/deserialization procedure (without the stack) ...
Definition: mapContext.hpp:46
std::size_t nof_elements() const
Get the number of elements in the structure or array.
Definition: structDescriptionBase.hpp:165
Map m_elem
Definition: structDescriptionBase.hpp:199
Input filter with atomic values having a type.
Definition: typedfilter.hpp:48
std::size_t nof_attributes() const
Get the number of attributes of a struct.
Definition: structDescriptionBase.hpp:150
Class representing an empty structure.
Definition: structDescriptionBase.hpp:208
void done(void *obj) const
Destroy the structure referenced with 'obj'.
Definition: structDescriptionBase.hpp:105