From e01bc95615b24e8e122942f3701b6b4197ec16b7 Mon Sep 17 00:00:00 2001 From: Justyna Ilczuk Date: Sun, 30 Dec 2012 22:37:10 +0100 Subject: [PATCH] More on context manager. --- Sencha-lang/Context.cpp | 80 ++++++++++++++++++++++++++++++++++ Sencha-lang/Context.h | 43 ++++++++++++++++++ Sencha-lang/ContextManager.cpp | 61 +++++++------------------- Sencha-lang/ContextManager.h | 37 ++++------------ 4 files changed, 146 insertions(+), 75 deletions(-) create mode 100644 Sencha-lang/Context.cpp create mode 100644 Sencha-lang/Context.h diff --git a/Sencha-lang/Context.cpp b/Sencha-lang/Context.cpp new file mode 100644 index 0000000..e9b16c3 --- /dev/null +++ b/Sencha-lang/Context.cpp @@ -0,0 +1,80 @@ +/* + * Context.cpp + * + * Created on: Dec 30, 2012 + * Author: att + */ + +#include "Context.h" + +Context::Context(std::string name) { + // TODO Auto-generated constructor stub + this->index = 0; + this->name = name; +} + +Context::~Context() { + // TODO Auto-generated destructor stub +} + + +void Context::register_function(std::string name, PointerToNativeFunction f) +{ + registered_functions[name] = f; +} + +SenchaObject Context::execute_native_function(std::string name, std::vector arguments) +{ + SenchaObject result; + if(registered_functions.count(name) == 1) + result = registered_functions[name](arguments); + return result; +} + +ObjectIndex Context::add_to_store(SenchaObject & object) +{ + index++; + object_store[index] = object; + + return index; +} + +SenchaObject Context::get_from_store(ObjectIndex index) +{ + return object_store[index]; +} + +void Context::add(std::string name, SenchaObject object) +{ + interpreter_context[name] = add_to_store(object); +} + +void Context::set(std::string name, SenchaObject object) +{ + if(interpreter_context[name] != 0) + { + object_store[interpreter_context[name]] = object; + } + else + { + add(name, object); + + } +} + +SenchaObject Context::get(std::string name) +{ + return get_from_store(interpreter_context[name]); +} + +std::string Context::debug() { + std::string debug_note = ""; + + for( auto iter = this->interpreter_context.begin(); iter != this->interpreter_context.end(); iter++) + { + debug_note += "Context: " + to_string((*iter).second) + ": " + (*iter).first + " " + object_store[(*iter).second].repr() + "\n"; + } + return debug_note; + + +} diff --git a/Sencha-lang/Context.h b/Sencha-lang/Context.h new file mode 100644 index 0000000..53e7888 --- /dev/null +++ b/Sencha-lang/Context.h @@ -0,0 +1,43 @@ +/* + * Context.h + * + * Created on: Dec 30, 2012 + * Author: att + */ + +#ifndef CONTEXT_H_ +#define CONTEXT_H_ +#include "Utils/to_string.h" + +class Context { +public: + Context(std::string name); + unsigned int index; + + std::string name; + std::map object_store; + ObjectIndex add_to_store(SenchaObject & object); + SenchaObject get_from_store(ObjectIndex index); + + + typedef SenchaObject (*PointerToNativeFunction)(std::vector); + + std::map registered_functions; + + void register_function(std::string name, PointerToNativeFunction f); + + SenchaObject execute_native_function(std::string name, std::vector arguments); + + //Overload it to use contexts + + typedef std::map ExecutionContext; + ExecutionContext interpreter_context; + + std::string debug() ; + void add(std::string name, SenchaObject object); + void set(std::string name, SenchaObject object); + SenchaObject get(std::string name); + virtual ~Context(); +}; + +#endif /* CONTEXT_H_ */ diff --git a/Sencha-lang/ContextManager.cpp b/Sencha-lang/ContextManager.cpp index 3a753d9..56ae10f 100644 --- a/Sencha-lang/ContextManager.cpp +++ b/Sencha-lang/ContextManager.cpp @@ -8,69 +8,38 @@ #include "ContextManager.h" ContextManager::ContextManager() { + contexts["global"] = new Context("global"); index = 0; } ContextManager::~ContextManager() { } -void ContextManager::register_function(std::string name, PointerToNativeFunction f) -{ - registered_functions[name] = f; -} - -SenchaObject ContextManager::execute_native_function(std::string name, std::vector arguments) -{ - SenchaObject result; - if(registered_functions.count(name) == 1) - result = registered_functions[name](arguments); - return result; -} - -ObjectIndex ContextManager::add_to_store(SenchaObject & object) +Context * ContextManager::create_new_context() { + Context * context = new Context("Zdzislaw" + to_string(index)); index++; - object_store[index] = object; - - return index; + contexts[context->name] = context; + return context; } -SenchaObject ContextManager::get_from_store(ObjectIndex index) +Context * ContextManager::get_context(std::string name) { - return object_store[index]; + return contexts[name]; } -void ContextManager::add(std::string name, SenchaObject object) +Context * ContextManager::context(std::string name) { - interpreter_context[name] = add_to_store(object); + if(contexts.count(name) != 0) return contexts[name]; + else return nullptr; } -void ContextManager::set(std::string name, SenchaObject object) +void ContextManager::destroy_context(std::string name) { - if(interpreter_context[name] != 0) + auto iter = contexts.find(name); + if(iter != contexts.end()) { - object_store[interpreter_context[name]] = object; - } - else - { - add(name, object); - + delete (*iter); + contexts.erase(iter); } } - -SenchaObject ContextManager::get(std::string name) -{ - return get_from_store(interpreter_context[name]); -} - -std::string ContextManager::debug() { - std::string debug_note = ""; - - for( auto iter = this->interpreter_context.begin(); iter != this->interpreter_context.end(); iter++) - { - debug_note += "Context: " + to_string((*iter).second) + ": " + (*iter).first + " " + object_store[(*iter).second].repr() + "\n"; - } - return debug_note; - - - } diff --git a/Sencha-lang/ContextManager.h b/Sencha-lang/ContextManager.h index 81bcba2..f376da5 100644 --- a/Sencha-lang/ContextManager.h +++ b/Sencha-lang/ContextManager.h @@ -13,7 +13,8 @@ #include #include "Elements/SenchaObject.h" #include "AST/ASTExpression.h" - +#include "Context.h" +#include "Utils/to_string.h" typedef unsigned long ObjectIndex; typedef unsigned long FunctionIndex; @@ -21,34 +22,12 @@ typedef unsigned long FunctionIndex; class ContextManager { public: ContextManager(); - typedef unsigned long ObjectIndex; - ObjectIndex index; - - std::map object_store; - ObjectIndex add_to_store(SenchaObject & object); - SenchaObject get_from_store(ObjectIndex index); - - typedef SenchaObject (*PointerToNativeFunction)(std::vector); - - std::map registered_functions; - - void register_function(std::string name, PointerToNativeFunction f); - - SenchaObject execute_native_function(std::string name, std::vector arguments); - - //Overload it to use contexts - void add(std::string name, SenchaObject object); - void set(std::string name, SenchaObject object); - SenchaObject get(std::string name); - typedef std::map ExecutionContext; - ExecutionContext interpreter_context; - - std::string debug() ; - - - - ExecutionContext global_context; - std::map function_contexts; + std::map contexts; + Context * create_new_context(); + void destroy_context(std::string name); + Context * get_context(std::string name); + Context * context(std::string name); + unsigned int index; virtual ~ContextManager(); };