C:/Projets/hexamonkey/core/mapmodule.h
Go to the documentation of this file.
00001 //This file is part of the HexaMonkey project, a multimedia analyser
00002 //Copyright (C) 2013  Sevan Drapeau-Martin, Nicolas Fleury
00003 
00004 //This program is free software; you can redistribute it and/or
00005 //modify it under the terms of the GNU General Public License
00006 //as published by the Free Software Foundation; either version 2
00007 //of the License, or (at your option) any later version.
00008 
00009 //This program is distributed in the hope that it will be useful,
00010 //but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 //GNU General Public License for more details.
00013 
00014 //You should have received a copy of the GNU General Public License
00015 //along with this program; if not, write to the Free Software
00016 //Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00017 
00018 #ifndef MAPMODULE_H
00019 #define MAPMODULE_H
00020 
00021 #include <unordered_map>
00022 #include <string>
00023 #include <functional>
00024 
00025 #include "core/module.h"
00026 
00027 #define parserLambda (const ObjectType &type, Object &object, const Module &module) ->Parser*
00028 #define fixedSizeLambda (const ObjectType &type, const Module &module) ->int64_t
00029 #define functionLambda (Scope& scope, const Module &module) ->Variable
00030 
00034 class MapModule : public Module
00035 {
00036 protected:
00037     typedef std::function<Parser* (const ObjectType &, Object &, const Module &)> ParserGenerator;
00038     typedef std::function<int64_t (const ObjectType &, const Module &)> FixedSizeGenerator;
00039     typedef std::function<Variable (Scope&, const Module &)> Functor;
00040 
00041     virtual ~MapModule(){}
00042 
00046     void addParser(const std::string& name, const ParserGenerator& parserGenerator);
00047 
00051     void addParser(const std::string& name);
00052 
00057     void setFixedSize(const std::string& name, const FixedSizeGenerator& fixedSizeFunction);
00058 
00063     void setFixedSize(const std::string& name, int64_t fixedSize);
00064 
00073     void setFixedSizeFromArg(const std::string& name, int arg);
00074 
00078     void addFunction(const std::string& name,
00079                      const std::vector<std::string>& parameterNames,
00080                      const std::vector<bool>& parameterModifiables,
00081                      const std::vector<Variant>& parameterDefaults,
00082                      const Functor& functor);
00083 
00084 private:
00085     bool hasParser(const ObjectType &type) const override;
00086     Parser* getParser(const ObjectType &type, Object &object, const Module &fromModule) const override;
00087 
00088     int64_t doGetFixedSize(const ObjectType &type, const Module &module) const override;
00089 
00090     bool doCanHandleFunction(const std::string& name) const override;
00091     Variable doExecuteFunction(const std::string& name, const ScopePtr &params, const Module &fromModule) const override;
00092     const std::vector<std::string>& doGetFunctionParameterNames(const std::string& name) const override;
00093     const std::vector<bool>& doGetFunctionParameterModifiables(const std::string& name) const override;
00094     const std::vector<Variant>& doGetFunctionParameterDefaults(const std::string& name) const override;
00095 
00096     static const ParserGenerator& nullGenerator;
00097     std::unordered_map<std::string, ParserGenerator> _map;
00098     std::unordered_map<std::string, FixedSizeGenerator> _sizes;
00099 
00100     typedef std::tuple<std::vector<std::string>, std::vector<bool>, std::vector<Variant>, Functor> FunctionDescriptor;
00101     std::unordered_map<std::string,  FunctionDescriptor>  _functions;
00102 };
00103 
00104 #endif // MAPMODULE_H