mirror of
https://github.com/OpenNebula/one.git
synced 2024-12-22 13:33:52 +03:00
feature #206: New interace for callbacks
This commit is contained in:
parent
eacaa30e12
commit
4aaac043d0
85
include/Callbackable.h
Normal file
85
include/Callbackable.h
Normal file
@ -0,0 +1,85 @@
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org) */
|
||||
/* */
|
||||
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
|
||||
/* not use this file except in compliance with the License. You may obtain */
|
||||
/* a copy of the License at */
|
||||
/* */
|
||||
/* http://www.apache.org/licenses/LICENSE-2.0 */
|
||||
/* */
|
||||
/* Unless required by applicable law or agreed to in writing, software */
|
||||
/* distributed under the License is distributed on an "AS IS" BASIS, */
|
||||
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
|
||||
/* See the License for the specific language governing permissions and */
|
||||
/* limitations under the License. */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
#ifndef CALLBACKABLE_H_
|
||||
#define CALLBACKABLE_H_
|
||||
|
||||
using namespace std;
|
||||
|
||||
class SqlDB;
|
||||
|
||||
/**
|
||||
* ObjectSQL class. Provides a SQL backend interface, it should be implemented
|
||||
* by persistent objects.
|
||||
*/
|
||||
class Callbackable
|
||||
{
|
||||
public:
|
||||
|
||||
Callbackable():cb(0),arg(0){};
|
||||
|
||||
virtual ~Callbackable(){};
|
||||
|
||||
/**
|
||||
* Datatype for call back pointers
|
||||
*/
|
||||
typedef int (Callbackable::*CallBack)(void *, int, char ** ,char **);
|
||||
|
||||
/**
|
||||
* Set the callback function and custom arguments to be executed by the
|
||||
* next SQL command
|
||||
* @param ptr to the callback function
|
||||
* @param arg custom arguments for the callback function
|
||||
*/
|
||||
void set_callback(CallBack _cb, void * _arg)
|
||||
{
|
||||
cb = _cb;
|
||||
arg = _arg;
|
||||
};
|
||||
|
||||
/**
|
||||
* Test if the CallBack is set for the object.
|
||||
* @return true if the callback is set
|
||||
*/
|
||||
bool isCallBackSet()
|
||||
{
|
||||
return (cb != 0);
|
||||
};
|
||||
|
||||
/**
|
||||
* Set the callback function and custom arguments to be executed by the
|
||||
* next SQL command
|
||||
* @param ptr to the callback function
|
||||
* @param arg custom arguments for the callback function
|
||||
*/
|
||||
int do_callback(int num, char **values, char **names)
|
||||
{
|
||||
return (this->*cb)(arg, num, values, names);
|
||||
};
|
||||
|
||||
private:
|
||||
/**
|
||||
* SQL callback to be executed for each row result of an SQL statement
|
||||
*/
|
||||
CallBack cb;
|
||||
|
||||
/**
|
||||
* Custom arguments for the callback
|
||||
*/
|
||||
void * arg;
|
||||
};
|
||||
|
||||
#endif /*CALLBACKABLE_H_*/
|
@ -17,17 +17,16 @@
|
||||
#ifndef OBJECT_SQL_H_
|
||||
#define OBJECT_SQL_H_
|
||||
|
||||
#include "Callbackable.h"
|
||||
#include "SqlDB.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
class SqlDB;
|
||||
|
||||
/**
|
||||
* ObjectSQL class. Provides a SQL backend interface, it should be implemented
|
||||
* by persistent objects.
|
||||
*/
|
||||
class ObjectSQL
|
||||
class ObjectSQL : public Callbackable
|
||||
{
|
||||
public:
|
||||
|
||||
@ -35,43 +34,6 @@ public:
|
||||
|
||||
virtual ~ObjectSQL(){};
|
||||
|
||||
/**
|
||||
* Datatype for call back pointers
|
||||
*/
|
||||
typedef int (ObjectSQL::*CallBackPtr)(void *, int, char ** ,char **);
|
||||
|
||||
/**
|
||||
* Set the callback function and custom arguments to be executed by the
|
||||
* next SQL command
|
||||
* @param ptr to the callback function
|
||||
* @param arg custom arguments for the callback function
|
||||
*/
|
||||
void set_callback(CallBackPtr ptr, void * arg)
|
||||
{
|
||||
callback.cb = ptr;
|
||||
callback.arg = arg;
|
||||
};
|
||||
|
||||
/**
|
||||
* Test if the CallBack is set for the object.
|
||||
* @return true if the callback is set
|
||||
*/
|
||||
bool isCallBackSet()
|
||||
{
|
||||
return (callback.cb != 0);
|
||||
};
|
||||
|
||||
/**
|
||||
* Set the callback function and custom arguments to be executed by the
|
||||
* next SQL command
|
||||
* @param ptr to the callback function
|
||||
* @param arg custom arguments for the callback function
|
||||
*/
|
||||
int do_callback(int num, char **values, char **names)
|
||||
{
|
||||
return (this->*(callback.cb))(callback.arg, num, values, names);
|
||||
};
|
||||
|
||||
protected:
|
||||
/**
|
||||
* Reads the ObjectSQL (identified with its OID) from the database.
|
||||
@ -104,22 +66,6 @@ protected:
|
||||
*/
|
||||
virtual int drop(
|
||||
SqlDB * db) = 0;
|
||||
|
||||
private:
|
||||
/**
|
||||
* A Callback defined by the function to be executed, a generic argument
|
||||
* and the columns returned by the query
|
||||
*/
|
||||
struct CallBackRegister
|
||||
{
|
||||
CallBackPtr cb;
|
||||
void * arg;
|
||||
};
|
||||
|
||||
/**
|
||||
* SQL callback to be executed for each row result of an SQL statement
|
||||
*/
|
||||
CallBackRegister callback;
|
||||
};
|
||||
|
||||
#endif /*OBJECT_SQL_H_*/
|
||||
#endif /*OBJECT_SQL_H_*/
|
@ -18,12 +18,10 @@
|
||||
#define SQL_DB_H_
|
||||
|
||||
#include <sstream>
|
||||
#include "ObjectSQL.h"
|
||||
#include "Callbackable.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
class ObjectSQL;
|
||||
|
||||
/**
|
||||
* SqlDB class.Provides an abstract interface to implement a SQL backend
|
||||
*/
|
||||
@ -42,7 +40,7 @@ public:
|
||||
* @param arg to pass to the callback function
|
||||
* @return 0 on success
|
||||
*/
|
||||
virtual int exec(ostringstream& cmd, ObjectSQL* obj=0) = 0;
|
||||
virtual int exec(ostringstream& cmd, Callbackable* obj=0) = 0;
|
||||
|
||||
/**
|
||||
* This function returns a legal SQL string that can be used in an SQL
|
||||
|
@ -88,7 +88,7 @@ public:
|
||||
* @param arg to pass to the callback function
|
||||
* @return 0 on success
|
||||
*/
|
||||
int exec(ostringstream& cmd, ObjectSQL* obj=0)
|
||||
int exec(ostringstream& cmd, Callbackable* obj=0)
|
||||
{
|
||||
int rc;
|
||||
|
||||
@ -167,7 +167,7 @@ public:
|
||||
* @param arg to pass to the callback function
|
||||
* @return 0 on success
|
||||
*/
|
||||
int exec(const char * cmd_c_str, ObjectSQL* obj=0)
|
||||
int exec(const char * cmd_c_str, Callbackable* obj=0)
|
||||
{
|
||||
string cmd_str = cmd_c_str;
|
||||
ostringstream cmd;
|
||||
|
Loading…
Reference in New Issue
Block a user