C:/Projets/hexamonkey/core/module.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 MODULE_H
00019 #define MODULE_H
00020 
00021 #include <list>
00022 #include <map>
00023 #include <set>
00024 #include <algorithm>
00025 #include <memory>
00026 
00027 #include "core/objecttype.h"
00028 #include "core/objecttypetemplate.h"
00029 #include "core/formatdetector/standardformatdetector.h"
00030 #include "core/variable/variable.h"
00031 #include "core/interpreter/scope/scopeptr.h"
00032 
00033 class Object;
00034 class Parser;
00035 class Scope;
00036 
00064 class Module
00065 {
00066 public:
00067     Module();
00068 
00075     void import(const Module& module);
00076 
00083     bool isExtension(const ObjectType& child, const ObjectType& parent) const;
00084 
00090     void setExtension(const ObjectTypeTemplate& childTemplate, const std::function<ObjectType(const ObjectType &)> &parentFunction);
00091 
00095     void setExtension(const ObjectTypeTemplate& childTemplate, const ObjectType& parent);
00096 
00100     void setExtension(const ObjectTypeTemplate& childTemplate, const ObjectType& parent, const std::map<int, int> &parameterMapping);
00101 
00107     ObjectType getFather(const ObjectType& child) const;
00108 
00114     ObjectType specify(const ObjectType& parent) const;
00115 
00119     void setSpecification(const ObjectType& parent, const ObjectType& child);
00120 
00125     Object* handle(const ObjectType& type, File& file, Object* parent = nullptr) const;
00126 
00130     bool  canHandle(const ObjectType& type) const;
00131 
00137     const Module* handler(const ObjectType &type) const;
00138 
00144     int64_t getFixedSize(const ObjectType& type) const;
00145 
00149     const ObjectTypeTemplate& getTemplate(const std::string& name) const;
00150 
00154     bool hasTemplate(const std::string& name) const;
00155 
00159     bool canHandleFunction(const std::string &name) const;
00160 
00166     const Module* functionHandler(const std::string &name) const;
00167 
00171     Variable executeFunction(const std::string &name, const ScopePtr &params) const;
00172 
00176     const std::vector<std::string>& getFunctionParameterNames(const std::string& name) const;
00177 
00181     const std::vector<bool>& getFunctionParameterModifiables(const std::string& name) const;
00182 
00186     const std::vector<Variant>& getFunctionParameterDefaults(const std::string& name) const;
00187 
00192     bool isLoaded() const;
00193 
00194 protected:
00199     virtual void addFormatDetection(StandardFormatDetector::Adder& formatAdder);
00200 
00205     virtual void requestImportations(std::vector<std::string>& modulesRequested);
00206 
00211     virtual bool doLoad();
00212 
00218     virtual Parser* getParser(const ObjectType &type, Object& object, const Module& fromModule) const;
00219 
00223     virtual bool hasParser(const ObjectType &type) const;
00224 
00232     virtual int64_t doGetFixedSize(const ObjectType& type, const Module& module) const;
00233 
00234 
00235     virtual bool doCanHandleFunction(const std::string& name) const;
00236     virtual Variable doExecuteFunction(const std::string& name, const ScopePtr &params, const Module &fromModule) const;
00237     virtual const std::vector<std::string>& doGetFunctionParameterNames(const std::string& name) const;
00238     virtual const std::vector<bool>& doGetFunctionParameterModifiables(const std::string& name) const;
00239     virtual const std::vector<Variant>& doGetFunctionParameterDefaults(const std::string& name) const;
00240 
00249     void addTemplate(const ObjectTypeTemplate& typeTemplate);
00250 
00254     const ObjectTypeTemplate& newTemplate(const std::string& name, const std::vector<std::string>& parameters = std::vector<std::string>());
00255 
00256 private:
00257     friend class ModuleLoader;
00258     template<class T>
00259     struct UnrefCompare : public std::binary_function<T, T, bool>
00260     {
00261         bool operator()(T x, T y) const
00262         {
00263             return *x < *y;
00264         }
00265     };
00266 
00267     typedef std::map<const ObjectType*, ObjectType, UnrefCompare<const ObjectType*> > SpecificationMap;
00268 
00269     typedef std::map<const ObjectTypeTemplate*, std::function<ObjectType(const ObjectType&)>, UnrefCompare<const ObjectTypeTemplate*> > ExtensionMap;
00270 
00271     bool load();
00272 
00273     ObjectType specifyLocally(const ObjectType& parent) const;
00274     void addParsers(Object& data, const ObjectType &type, const Module& fromModule, const ObjectType &lastType = ObjectType()) const;
00275 
00276     Object* handle(const ObjectType& type, File& file, Object *parent, const Module& fromModule) const;
00277 
00278     Variable executeFunction(const std::string& name, const ScopePtr &params, const Module& fromModule) const;
00279 
00280     bool _loaded;
00281 
00282     std::list<const Module*> _importedModules;
00283 
00284     ExtensionMap _extensions;
00285 
00286     SpecificationMap _automaticSpecifications;
00287 
00288     std::map<std::string, const ObjectTypeTemplate*> _templates;
00289     std::list<std::unique_ptr<ObjectTypeTemplate> > _ownedTemplates;
00290 };
00291 
00292 #endif // MODULE_H