More on context manager.
parent
f23c85949f
commit
e01bc95615
|
@ -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<ASTExpression *> 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;
|
||||
|
||||
|
||||
}
|
|
@ -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<ObjectIndex, SenchaObject> object_store;
|
||||
ObjectIndex add_to_store(SenchaObject & object);
|
||||
SenchaObject get_from_store(ObjectIndex index);
|
||||
|
||||
|
||||
typedef SenchaObject (*PointerToNativeFunction)(std::vector<ASTExpression *>);
|
||||
|
||||
std::map<std::string, PointerToNativeFunction> registered_functions;
|
||||
|
||||
void register_function(std::string name, PointerToNativeFunction f);
|
||||
|
||||
SenchaObject execute_native_function(std::string name, std::vector<ASTExpression *> arguments);
|
||||
|
||||
//Overload it to use contexts
|
||||
|
||||
typedef std::map<std::string, ObjectIndex> 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_ */
|
|
@ -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<ASTExpression *> 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;
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -13,7 +13,8 @@
|
|||
#include <iostream>
|
||||
#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<ObjectIndex, SenchaObject> object_store;
|
||||
ObjectIndex add_to_store(SenchaObject & object);
|
||||
SenchaObject get_from_store(ObjectIndex index);
|
||||
|
||||
typedef SenchaObject (*PointerToNativeFunction)(std::vector<ASTExpression *>);
|
||||
|
||||
std::map<std::string, PointerToNativeFunction> registered_functions;
|
||||
|
||||
void register_function(std::string name, PointerToNativeFunction f);
|
||||
|
||||
SenchaObject execute_native_function(std::string name, std::vector<ASTExpression *> 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<std::string, ObjectIndex> ExecutionContext;
|
||||
ExecutionContext interpreter_context;
|
||||
|
||||
std::string debug() ;
|
||||
|
||||
|
||||
|
||||
ExecutionContext global_context;
|
||||
std::map<FunctionIndex , ExecutionContext *> function_contexts;
|
||||
std::map<std::string, Context *> 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();
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue