Enhancement - Hooks

- Added Lua copyright notice to the version command.
- Added Lua copyright details to API.cpp, which is the only file that
  interacts with Lua.
- Added hook-type detection.
- Added a hook-type calling mechanism in Hooks.
- Implemented the post-start and pre-exit event triggers.
- Context::initialize now calls Hooks::initialize, which in turn calls
  API::initialize.  We have liftoff!
This commit is contained in:
Paul Beckingham 2010-01-17 14:20:29 -05:00
parent c66d6b0500
commit 57e94585e8
5 changed files with 141 additions and 6 deletions

View file

@ -23,6 +23,28 @@
// 02110-1301
// USA
//
// -------------
//
// Copyright © 19942008 Lua.org, PUC-Rio.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
////////////////////////////////////////////////////////////////////////////////
#include "API.h"
@ -389,7 +411,7 @@ void API::initialize ()
{
// Initialize Lua.
L = lua_open ();
luaL_openlibs (L);
luaL_openlibs (L); // TODO Error handling
// Register all the API functions in Lua global space.
lua_pushcfunction (L, api_task_version); lua_setglobal (L, "task_version");

View file

@ -83,6 +83,11 @@ void Context::initialize (int argc, char** argv)
}
initialize ();
// Hook system init, plus post-start event occurring at the first possible
// moment after hook initialization.
hooks.initialize ();
hooks.trigger ("post-start");
}
////////////////////////////////////////////////////////////////////////////////
@ -180,6 +185,7 @@ int Context::run ()
else
std::cout << *f << std::endl;
hooks.trigger ("pre-exit");
return rc;
}

View file

@ -25,6 +25,7 @@
//
////////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include "Hooks.h"
////////////////////////////////////////////////////////////////////////////////
@ -38,3 +39,90 @@ Hooks::~Hooks ()
}
////////////////////////////////////////////////////////////////////////////////
void Hooks::initialize ()
{
api.initialize ();
}
////////////////////////////////////////////////////////////////////////////////
bool Hooks::trigger (const std::string& event)
{
// TODO Look up scripts/functions hooking this event.
// TODO Load the scripts if necessary.
// TODO Call each function.
std::string type;
if (eventType (event, type))
{
if (type == "program") return triggerProgramEvent (event);
else if (type == "list") return triggerListEvent (event);
else if (type == "task") return triggerTaskEvent (event);
else if (type == "field") return triggerFieldEvent (event);
}
else
throw std::string ("Unrecognized hook event '") + event + "'";
return false;
}
////////////////////////////////////////////////////////////////////////////////
bool Hooks::eventType (const std::string& event, std::string& type)
{
if (event == "post-start" ||
event == "pre-exit")
{
type = "program";
return true;
}
else if (event == "?")
{
type = "list";
return true;
}
else if (event == "?")
{
type = "task";
return true;
}
else if (event == "?")
{
type = "field";
return true;
}
return false;
}
////////////////////////////////////////////////////////////////////////////////
bool Hooks::triggerProgramEvent (const std::string& event)
{
std::cout << "Hooks::triggerProgramEvent " << event << std::endl;
// TODO Is this event hooked?
// TODO Is the associated script loaded?
// TODO Call the function
return false;
}
////////////////////////////////////////////////////////////////////////////////
bool Hooks::triggerListEvent (const std::string& event)
{
std::cout << "Hooks::triggerListEvent " << event << std::endl;
return false;
}
////////////////////////////////////////////////////////////////////////////////
bool Hooks::triggerTaskEvent (const std::string& event)
{
std::cout << "Hooks::triggerTaskEvent " << event << std::endl;
return false;
}
////////////////////////////////////////////////////////////////////////////////
bool Hooks::triggerFieldEvent (const std::string& event)
{
std::cout << "Hooks::triggerFieldEvent " << event << std::endl;
return false;
}
////////////////////////////////////////////////////////////////////////////////

View file

@ -27,16 +27,31 @@
#ifndef INCLUDED_HOOKS
#define INCLUDED_HOOKS
#include <vector>
#include <string>
#include "API.h"
class Hooks
{
public:
Hooks (); // Default constructor
~Hooks (); // Destructor
Hooks (const Hooks&); // Deliberately unimplemented
Hooks& operator= (const Hooks&); // Deliberately unimplemented
Hooks (const Hooks&);
Hooks& operator= (const Hooks&);
void initialize ();
bool trigger (const std::string&);
bool eventType (const std::string&, std::string&);
private:
bool triggerProgramEvent (const std::string&);
bool triggerListEvent (const std::string&);
bool triggerTaskEvent (const std::string&);
bool triggerFieldEvent (const std::string&);
private:
API api;
std::vector <std::string> scripts;
};
#endif

View file

@ -490,8 +490,12 @@ int handleVersion (std::string &outs)
#endif
<< std::endl
<< "Copyright (C) 2006 - 2010, P. Beckingham, F. Hernandez."
<< "Copyright (C) 2006 - 2010 P. Beckingham, F. Hernandez."
<< std::endl
#ifdef HAVE_LIBLUA
<< "Portions of this software Copyright (C) 1994 2008 Lua.org, PUC-Rio."
<< std::endl
#endif
<< disclaimer.render ()
<< link.render ()
<< std::endl;