strusAnalyzer  0.17
functionView.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 Patrick P. Frey
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
11 #ifndef _STRUS_ANALYZER_FUNCTION_VIEW_HPP_INCLUDED
12 #define _STRUS_ANALYZER_FUNCTION_VIEW_HPP_INCLUDED
13 #include "strus/base/enable_if.hpp"
14 #include "strus/base/type_traits.hpp"
15 #include "strus/numericVariant.hpp"
16 #include <string>
17 #include <vector>
18 #include <map>
19 #include <iostream>
20 #include <sstream>
21 
23 namespace strus {
25 namespace analyzer {
26 
30 {
31 public:
32  typedef std::pair<std::string,std::string> NamedParameter;
33 
38  :m_name(o.m_name),m_parameter(o.m_parameter){}
42  FunctionView( const std::string& name_, const std::vector<NamedParameter>& params_)
43  :m_name(name_),m_parameter(params_){}
47  explicit FunctionView( const std::string& name_)
48  :m_name(name_),m_parameter(){}
49 
51  template<typename type>
52  struct is_atomic
53  {
54  typename strus::enable_if<
55  strus::is_arithmetic<type>::value
56  || strus::is_same<bool,type>::value
57  || strus::is_same<std::string,type>::value
58  || strus::is_same<char*,type>::value
59  ,bool> value_type;
60  enum {value=sizeof(value_type)};
61  };
62 
66  template <typename value_type>
67  typename strus::enable_if<is_atomic<value_type>::value,FunctionView&>::type operator()( const char* name_, const value_type& value_)
68  {
69  std::ostringstream out;
70  out << value_;
71  m_parameter.push_back( NamedParameter( name_, out.str()));
72  return *this;
73  }
74 
78  template <typename value_type>
79  typename strus::enable_if<is_atomic<value_type>::value,FunctionView&>::type operator()( const char* name_, const std::map<std::string,value_type>& value_)
80  {
81  typename std::map<std::string,value_type>::const_iterator vi = value_.begin(), ve = value_.end();
82  for (; vi != ve; ++vi)
83  {
84  std::ostringstream out;
85  out << vi->second;
86  m_parameter.push_back( NamedParameter( std::string(name_)+"::"+vi->first, out.str()));
87  }
88  return *this;
89  }
90 
94  template <typename value_type>
95  typename strus::enable_if<is_atomic<value_type>::value,FunctionView&>::type operator()( const char* name_, const std::vector<value_type>& value_)
96  {
97  typename std::vector<value_type>::const_iterator vi = value_.begin(), ve = value_.end();
98  for (; vi != ve; ++vi)
99  {
100  std::ostringstream out;
101  out << *vi;
102  m_parameter.push_back( NamedParameter( std::string("[]")+name_, out.str()));
103  }
104  return *this;
105  }
106 
110  template <typename value_type>
111  typename strus::enable_if<strus::is_same<NumericVariant,value_type>::value,FunctionView&>::type operator()( const char* name_, const value_type& value_)
112  {
113  std::ostringstream out;
114  out << value_.tostring().c_str();
115  m_parameter.push_back( NamedParameter( name_, out.str()));
116  return *this;
117  }
118 
121  const std::string& name() const {return m_name;}
122 
126  const std::vector<NamedParameter>& parameter() const {return m_parameter;}
127 
128 private:
129  std::string m_name;
130  std::vector<NamedParameter> m_parameter;
131 };
132 
133 }}//namespace
134 #endif
135 
std::pair< std::string, std::string > NamedParameter
Definition: functionView.hpp:32
strus::enable_if< is_atomic< value_type >::value, FunctionView & >::type operator()(const char *name_, const std::map< std::string, value_type > &value_)
Operator to build parameter list (for map of string to atomic value type)
Definition: functionView.hpp:79
strus::enable_if< is_atomic< value_type >::value, FunctionView & >::type operator()(const char *name_, const value_type &value_)
Operator to build parameter list (for atomic value type)
Definition: functionView.hpp:67
FunctionView(const std::string &name_)
Constructor.
Definition: functionView.hpp:47
Structure describing the internal representation of a normalizer/tokenizer/aggregator function in the...
Definition: functionView.hpp:29
FunctionView(const FunctionView &o)
Copy constructor.
Definition: functionView.hpp:37
strus::enable_if< is_atomic< value_type >::value, FunctionView & >::type operator()(const char *name_, const std::vector< value_type > &value_)
Operator to build parameter list (for map of string to atomic value type)
Definition: functionView.hpp:95
strus::enable_if< strus::is_arithmetic< type >::value||strus::is_same< bool, type >::value||strus::is_same< std::string, type >::value||strus::is_same< char *, type >::value,bool > value_type
Definition: functionView.hpp:59
strus::enable_if< strus::is_same< NumericVariant, value_type >::value, FunctionView & >::type operator()(const char *name_, const value_type &value_)
Operator to build parameter list (for value type numeric variant)
Definition: functionView.hpp:111
FunctionView(const std::string &name_, const std::vector< NamedParameter > &params_)
Constructor.
Definition: functionView.hpp:42
const std::string & name() const
Get the name of the function.
Definition: functionView.hpp:121
FunctionView()
Default constructor.
Definition: functionView.hpp:35
const std::vector< NamedParameter > & parameter() const
Get the internal representation of the named parameters of the function.
Definition: functionView.hpp:126
Conditional for atomic type.
Definition: functionView.hpp:52