Wolframe, 0.0.3

customDataNormalizer.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_TYPES_CUSTOM_DATA_NORMALIZER_HPP_INCLUDED
35 #define _Wolframe_TYPES_CUSTOM_DATA_NORMALIZER_HPP_INCLUDED
36 #include "types/customDataType.hpp"
38 #include "types/variant.hpp"
39 #include <string>
40 #include <vector>
41 
42 namespace _Wolframe {
43 namespace types {
44 
49 {
50 public:
53  :m_name(o.m_name)
54  ,m_type(o.m_type)
56  {}
58  CustomDataNormalizer( const std::string& name_, const std::vector<types::Variant>& arg, const types::CustomDataType* type_)
59  :m_name(name_)
60  ,m_type(type_)
61  ,m_initializer(type_->hasInitializer()?type_->createInitializer(arg):0)
62  {
63  if (!arg.empty() && !type_->hasInitializer())
64  {
65  throw std::runtime_error("arguments passed to custom data type without initializer defined");
66  }
67  }
68 
71  {
72  if (m_initializer) delete m_initializer;
73  }
74 
76  virtual const char* name() const
77  {
78  return m_name.c_str();
79  }
80 
82  const types::CustomDataType* type() const
83  {
84  return m_type;
85  }
86 
89  {
91  if (i.defined())
92  {
93  rt.data().value.Custom->assign( i);
94  }
95  return rt;
96  }
97 
99  virtual types::NormalizeFunction* copy() const
100  {
101  return new CustomDataNormalizer(*this);
102  }
103 
104 private:
105  std::string m_name;
108 };
109 
110 
115 {
116 public:
118  :m_name(o.m_name)
119  ,m_arg(o.m_arg)
120  {}
121  CustomDataMethodCallNormalizer( const std::string& name_, const std::vector<types::Variant>& arg_)
122  :m_name(name_)
123  ,m_arg(arg_)
124  {}
125 
127  {}
128 
129  virtual const char* name() const
130  {
131  return m_name.c_str();
132  }
133 
135  {
136  const CustomDataValue* cv = i.customref();
137  const CustomDataType* dt = cv->type();
139  if (!method) throw std::logic_error( "internal: calling undefined method");
140  return method( *cv, m_arg);
141  }
142 
144  {
145  return new CustomDataMethodCallNormalizer(*this);
146  }
147 
148 private:
149  std::string m_name;
150  std::vector<types::Variant> m_arg;
151 };
152 
153 }}//namespace
154 #endif
155 
156 
const Data & data() const
Get the internal representation of the data of this.
Definition: variant.hpp:193
virtual ~CustomDataMethodCallNormalizer()
Definition: customDataNormalizer.hpp:126
Initializer for a custom data value.
Definition: customDataType.hpp:94
Normalize function definition.
std::vector< types::Variant > m_arg
Definition: customDataNormalizer.hpp:150
Custom data type interface for variant.
types::Variant execute(const types::Variant &i) const
Implementation of types::NormalizeFunction::execute(const types::Variant&)const.
Definition: customDataNormalizer.hpp:88
types::Variant(* CustomDataValueMethod)(const CustomDataValue &val, const std::vector< types::Variant > &arg)
Definition: customDataType.hpp:144
CustomDataValueMethod getMethod(const std::string &methodname) const
types::CustomDataInitializer * m_initializer
Definition: customDataNormalizer.hpp:107
Custom data value.
Definition: customDataType.hpp:65
const types::CustomDataType * m_type
Definition: customDataNormalizer.hpp:106
const CustomDataType * type() const
Definition: customDataType.hpp:74
CustomDataMethodCallNormalizer(const CustomDataMethodCallNormalizer &o)
Definition: customDataNormalizer.hpp:117
CustomDataValue * Custom
custom data type reference
Definition: variant.hpp:118
Basic normalization function for atomic values (variant type)
Definition: normalizeFunction.hpp:52
Forward declaration.
Definition: variant.hpp:65
bool hasInitializer() const
Definition: customDataType.hpp:180
virtual const char * name() const
Implementation of types::NormalizeFunction::name()const.
Definition: customDataNormalizer.hpp:76
Variant value type.
const CustomDataValue * customref() const
Get the pointer to the custom data object (throws for non custom data type)
Definition: variant.hpp:201
Custom Data Type Definition.
Definition: customDataType.hpp:105
Normalizer function created from a custom data type initializer.
Definition: customDataNormalizer.hpp:47
virtual types::NormalizeFunction * copy() const
Definition: customDataNormalizer.hpp:143
CustomDataNormalizer(const std::string &name_, const std::vector< types::Variant > &arg, const types::CustomDataType *type_)
Constructor.
Definition: customDataNormalizer.hpp:58
CustomDataMethodCallNormalizer(const std::string &name_, const std::vector< types::Variant > &arg_)
Definition: customDataNormalizer.hpp:121
union _Wolframe::types::Variant::Data::@17 value
value of the variant
virtual types::NormalizeFunction * copy() const
Implementation of types::NormalizeFunction::copy()const.
Definition: customDataNormalizer.hpp:99
virtual const char * name() const
Definition: customDataNormalizer.hpp:129
virtual void assign(const Variant &o)=0
Normalizer function created from a custom data method call.
Definition: customDataNormalizer.hpp:113
bool defined() const
Test if this value is defined (not null)
Definition: variant.hpp:238
types::Variant execute(const types::Variant &i) const
Definition: customDataNormalizer.hpp:134
CustomDataNormalizer(const CustomDataNormalizer &o)
Copy constructor.
Definition: customDataNormalizer.hpp:52
const types::CustomDataType * type() const
Get the custom data type.
Definition: customDataNormalizer.hpp:82
std::string m_name
Definition: customDataNormalizer.hpp:105
virtual ~CustomDataNormalizer()
Destructor.
Definition: customDataNormalizer.hpp:70
std::string m_name
Definition: customDataNormalizer.hpp:149