JSON-59
Roker 5 years ago
parent 8ff15cdb99
commit 7fe0225ed8

@ -1,9 +1,13 @@
#include "function_map.hh"
#include "c_string.hh"
template<>
js::Value Type2String<std::string>::get() { return "String"; }
template<>
js::Value Type2String<c_string>::get() { return "String"; }
template<>
js::Value Type2String<const char*>::get() { return "String"; }

@ -1,14 +1,183 @@
#ifndef FUNCTION_MAP_HH
#define FUNCTION_MAP_HH
#include "json_spirit/json_spirit_value.h"
#include "json_spirit/json_spirit_writer.h"
#include "inout.hh"
#include "c_string.hh"
#include "context.hh"
#include <type_traits>
#include "json_spirit/json_spirit_writer.h"
// Just for debugging:
#include <iostream>
#include <pEp/message_api.h>
namespace js = json_spirit;
template<class T, bool NeedInput> struct In;
template<class T, bool NeedInput> struct Out;
// "params" and "position" might be used to fetch additional parameters from the array.
template<class T>
T from_json(const js::Value& v);
template<class T>
js::Value to_json(const T& t);
// helper classes to specify in- and out-parameters
template<class T, bool NeedInput=true>
struct In
{
typedef T c_type; // the according type in C function parameter
enum { is_output = false, need_input = NeedInput };
explicit In(const T& t) : value(t) {}
~In();
In(const In<T,NeedInput>& other) = delete;
In(In<T,NeedInput>&& victim) = delete;
In<T,NeedInput>& operator=(const In<T,NeedInput>&) = delete;
// default implementation:
In(const js::Value& v, Context*)
: In( from_json<T>(v) )
{ }
js::Value to_json() const
{
return ::to_json<T>(value);
}
c_type get_value() const { return value; }
T value;
};
// to call functions that operate directly on the JSON data type
template<class T, bool NeedInput=true>
struct InRaw
{
typedef js::Value c_type; // do not unwrap JSON data type
enum { is_output = false, need_input = NeedInput };
explicit InRaw(const js::Value& t) : value(t) {}
~InRaw() = default;
InRaw(const InRaw<T,NeedInput>& other) = delete;
InRaw(InRaw<T,NeedInput>&& victim) = delete;
InRaw<T,NeedInput>& operator=(const InRaw<T,NeedInput>&) = delete;
// default implementation:
InRaw(const js::Value& v, Context*)
: InRaw(v)
{ }
js::Value to_json() const
{
throw std::logic_error( std::string(typeid(T).name()) + " is not for output!" );
}
c_type get_value() const { return value; }
js::Value value;
};
// helper classes to specify in- and out-parameters
template<class T, bool NeedInput=true>
struct InOut : public In<T,NeedInput>
{
typedef In<T,NeedInput> Base;
enum { is_output = true, need_input = NeedInput };
explicit InOut(const T& t) : Base(t) {}
~InOut() = default;
InOut<T,NeedInput>& operator=(const InOut<T,NeedInput>&) = delete;
// default implementation:
InOut(const js::Value& v, Context*)
: Base( from_json<T>(v) )
{ }
js::Value to_json() const
{
return ::to_json<T>(Base::value);
}
};
template<class T, bool NeedInput = true>
struct Out
{
typedef T* c_type; // the according type in C function parameter
enum { is_output = true, need_input = NeedInput }; // if need_input=false it would no longer consume an element in the input parameter array.
Out() : value{ new T{} }
{
if(typeid(T)==typeid(_message*))
{
std::cerr << "|$ Out<message*>(): this=" << *this << "\n";
}
}
~Out();
Out(const Out<T,NeedInput>& other) = delete;
Out(Out<T,NeedInput>&& victim) = delete;
// just to be sure they are not implicitly defined:
Out<T,NeedInput>& operator=(const Out<T,NeedInput>& other) = delete;
Out<T,NeedInput>& operator=(Out<T,NeedInput>&& victim) = delete;
Out(const js::Value& v, Context*)
: Out()
{ }
js::Value to_json() const
{
return ::to_json<T>(*value);
}
c_type get_value() const { return value; }
T* value = nullptr;
friend
std::ostream& operator<<(std::ostream& o, const Out<T,NeedInput>& out)
{
o << (const void*)&out;
// the if() was added to avoid crashes on memory corruptuon. But clang++ warns, that this check is always true on "well-formed" programs, and he is right. In an ideal world there are no memory corruptions. ;-(
// if(&out)
{
o << ", value=" << (const void*)out.value;
if(out.value)
{
o << ", *value=" << *(out.value);
}
}
return o;
}
};
template<class T, bool NeedInput>
js::Value to_json(const Out<T,NeedInput>& o)
{
return ::to_json(*o.value);
}
template<class T, bool NeedInput>
js::Value to_json(const InOut<T,NeedInput>& o)
{
return ::to_json(o.value);
}
// heloer class for generic calls:

@ -1,25 +1,6 @@
#include "inout.hh"
#include <stdexcept>
#include <cstring> // for memcpy()
namespace
{
char* make_c_string(const std::string& src)
{
char* dest = (char*)malloc(src.size() + 1);
memcpy(dest, src.c_str(), src.size() + 1 );
return dest;
}
}
template<>
In<char const*>::~In()
{
if(value) free(const_cast<char*>(value));
}
#include <pEp/pEpEngine.h>
#define SIMPLE_TYPE(TYPE) \
@ -53,7 +34,7 @@ Out<char const*>::~Out()
{
if(value)
{
free(const_cast<char*>(*value));
pEp_free(const_cast<char*>(*value));
}
delete value;
}
@ -64,7 +45,7 @@ Out<char*>::~Out()
{
if(value)
{
free(*value);
pEp_free(*value);
*value = nullptr;
}
delete value;
@ -138,18 +119,6 @@ std::string from_json<std::string>(const js::Value& v)
}
template<>
char* from_json<char*>(const js::Value& v)
{
return make_c_string( v.get_str() );
}
template<>
const char* from_json<const char*>(const js::Value& v)
{
return make_c_string( v.get_str() );
}
template<class T>
js::Value to_json(const T& t)

@ -205,17 +205,17 @@ PEP_STATUS MIME_decrypt_message_ex(
const FunctionMap functions = {
// from message_api.h
FP( "Message API", new Separator ),
FP( "MIME_encrypt_message", new Func<PEP_STATUS, In<PEP_SESSION, false>, In<const char*>, In<size_t>, In<stringlist_t*>,
FP( "MIME_encrypt_message", new Func<PEP_STATUS, In<PEP_SESSION, false>, In<c_string>, In<size_t>, In<stringlist_t*>,
Out<char*>, In<PEP_enc_format>, In<PEP_encrypt_flags_t>>( &MIME_encrypt_message ) ),
FP( "MIME_encrypt_message_for_self", new Func<PEP_STATUS, In<PEP_SESSION, false>, In<pEp_identity*>, In<const char*>, In<size_t>,
FP( "MIME_encrypt_message_for_self", new Func<PEP_STATUS, In<PEP_SESSION, false>, In<pEp_identity*>, In<c_string>, In<size_t>,
Out<char*>, In<PEP_enc_format>, In<PEP_encrypt_flags_t>>( &MIME_encrypt_message_for_self ) ),
FP( "MIME_decrypt_message", new Func<PEP_STATUS, In<PEP_SESSION, false>, In<const char*>, In<size_t>,
FP( "MIME_decrypt_message", new Func<PEP_STATUS, In<PEP_SESSION, false>, In<c_string>, In<size_t>,
Out<char*>, Out<stringlist_t*>, Out<PEP_rating>, Out<PEP_decrypt_flags_t>>( &MIME_decrypt_message ) ),
// HACK: because "auto sessions" are per TCP connections, add a parameter to set passive_mode each time again
FP( "MIME_encrypt_message_ex", new Func<PEP_STATUS, In<PEP_SESSION, false>, In<const char*>, In<size_t>, In<stringlist_t*>, In<bool>,
FP( "MIME_encrypt_message_ex", new Func<PEP_STATUS, In<PEP_SESSION, false>, In<c_string>, In<size_t>, In<stringlist_t*>, In<bool>,
Out<char*>, In<PEP_enc_format>, In<PEP_encrypt_flags_t>>( &MIME_encrypt_message_ex ) ),
FP( "MIME_decrypt_message_ex", new Func<PEP_STATUS, In<PEP_SESSION, false>, In<const char*>, In<size_t>, In<bool>,
FP( "MIME_decrypt_message_ex", new Func<PEP_STATUS, In<PEP_SESSION, false>, In<c_string>, In<size_t>, In<bool>,
Out<char*>, Out<stringlist_t*>, Out<PEP_rating>, Out<PEP_decrypt_flags_t>>( &MIME_decrypt_message_ex ) ),
FP( "startKeySync", new Func<void, In<JsonAdapter*, false>>( &JsonAdapter::startSync) ),
@ -232,7 +232,7 @@ const FunctionMap functions = {
FP( "get_gpg_path", new Func<PEP_STATUS, Out<const char*>>(&get_gpg_path) ),
FP( "pEp Engine Core API", new Separator),
FP( "log_event", new Func<PEP_STATUS, In<PEP_SESSION,false>, In<const char*>, In<const char*>, In<const char*>, In<const char*>>( &log_event) ),
FP( "log_event", new Func<PEP_STATUS, In<PEP_SESSION,false>, In<c_string>, In<c_string>, In<c_string>, In<c_string>>( &log_event) ),
FP( "get_trustwords", new Func<PEP_STATUS, In<PEP_SESSION,false>, In<const pEp_identity*>, In<const pEp_identity*>, In<Language>, Out<char*>, Out<size_t>, In<bool>>( &get_trustwords) ),
FP( "get_languagelist", new Func<PEP_STATUS, In<PEP_SESSION,false>, Out<char*>>( &get_languagelist) ),
FP( "get_phrase" , new Func<PEP_STATUS, In<PEP_SESSION,false>, In<Language>, In<int>, Out<char*>> ( &get_phrase) ),
@ -241,9 +241,9 @@ const FunctionMap functions = {
FP( "config_unencrypted_subject", new Func<void, In<PEP_SESSION,false>, In<bool>>( &config_unencrypted_subject) ),
FP( "Identity Management API", new Separator),
FP( "get_identity" , new Func<PEP_STATUS, In<PEP_SESSION,false>, In<const char*>, In<const char*>, Out<pEp_identity*>>( &get_identity) ),
FP( "get_identity" , new Func<PEP_STATUS, In<PEP_SESSION,false>, In<c_string>, In<c_string>, Out<pEp_identity*>>( &get_identity) ),
FP( "set_identity" , new Func<PEP_STATUS, In<PEP_SESSION,false>, In<pEp_identity*>> ( &set_identity) ),
FP( "mark_as_comprimized", new Func<PEP_STATUS, In<PEP_SESSION,false>, In<const char*>> ( &mark_as_compromized) ),
FP( "mark_as_comprimized", new Func<PEP_STATUS, In<PEP_SESSION,false>, In<c_string>> ( &mark_as_compromized) ),
FP( "identity_rating" , new Func<PEP_STATUS, In<PEP_SESSION,false>, In<pEp_identity*>, Out<PEP_rating>>( &identity_rating) ),
FP( "outgoing_message_rating", new Func<PEP_STATUS, In<PEP_SESSION,false>, In<message*>, Out<PEP_rating>>( &outgoing_message_rating) ),
FP( "set_identity_flags" , new Func<PEP_STATUS, In<PEP_SESSION,false>, InOut<pEp_identity*>, In<identity_flags_t>>( &set_identity_flags) ),
@ -251,12 +251,12 @@ const FunctionMap functions = {
FP( "Low level Key Management API", new Separator),
FP( "generate_keypair", new Func<PEP_STATUS, In<PEP_SESSION,false>, InOut<pEp_identity*>> ( &generate_keypair) ),
FP( "delete_keypair", new Func<PEP_STATUS, In<PEP_SESSION,false>, In<const char*>> ( &delete_keypair) ),
FP( "import_key" , new Func<PEP_STATUS, In<PEP_SESSION,false>, In<const char*>, In<std::size_t>, Out<identity_list*>> ( &import_key) ),
FP( "export_key" , new Func<PEP_STATUS, In<PEP_SESSION,false>, In<const char*>, Out<char*>, Out<std::size_t>> ( &export_key) ),
FP( "find_keys" , new Func<PEP_STATUS, In<PEP_SESSION,false>, In<const char*>, Out<stringlist_t*>> ( &find_keys) ),
FP( "delete_keypair", new Func<PEP_STATUS, In<PEP_SESSION,false>, In<c_string>> ( &delete_keypair) ),
FP( "import_key" , new Func<PEP_STATUS, In<PEP_SESSION,false>, In<c_string>, In<std::size_t>, Out<identity_list*>> ( &import_key) ),
FP( "export_key" , new Func<PEP_STATUS, In<PEP_SESSION,false>, In<c_string>, Out<char*>, Out<std::size_t>> ( &export_key) ),
FP( "find_keys" , new Func<PEP_STATUS, In<PEP_SESSION,false>, In<c_string>, Out<stringlist_t*>> ( &find_keys) ),
FP( "get_trust" , new Func<PEP_STATUS, In<PEP_SESSION,false>, InOut<pEp_identity*>> ( &get_trust) ),
FP( "own_key_is_listed", new Func<PEP_STATUS, In<PEP_SESSION,false>, In<const char*>, Out<bool>> ( &own_key_is_listed) ),
FP( "own_key_is_listed", new Func<PEP_STATUS, In<PEP_SESSION,false>, In<c_string>, Out<bool>> ( &own_key_is_listed) ),
FP( "own_identities_retrieve", new Func<PEP_STATUS, In<PEP_SESSION,false>, Out<identity_list*>>( &own_identities_retrieve ) ),
FP( "undo_last_mitrust", new Func<PEP_STATUS, In<PEP_SESSION,false>>( &undo_last_mistrust ) ),
@ -267,18 +267,18 @@ const FunctionMap functions = {
FP( "key_mistrusted", new Func<PEP_STATUS, In<PEP_SESSION,false>, In<pEp_identity*>>( &key_mistrusted) ),
FP( "key_reset_trust", new Func<PEP_STATUS, In<PEP_SESSION,false>, In<pEp_identity*>>( &key_reset_trust) ),
FP( "least_trust" , new Func<PEP_STATUS, In<PEP_SESSION,false>, In<const char*>, Out<PEP_comm_type>> ( &least_trust) ),
FP( "get_key_rating", new Func<PEP_STATUS, In<PEP_SESSION,false>, In<const char*>, Out<PEP_comm_type>> ( &get_key_rating) ),
FP( "renew_key" , new Func<PEP_STATUS, In<PEP_SESSION,false>, In<const char*>, In<const timestamp*>> ( &renew_key) ),
FP( "revoke" , new Func<PEP_STATUS, In<PEP_SESSION,false>, In<const char*>, In<const char*>> ( &revoke_key) ),
FP( "key_expired" , new Func<PEP_STATUS, In<PEP_SESSION,false>, In<const char*>, In<time_t>, Out<bool>> ( &key_expired) ),
FP( "least_trust" , new Func<PEP_STATUS, In<PEP_SESSION,false>, In<c_string>, Out<PEP_comm_type>> ( &least_trust) ),
FP( "get_key_rating", new Func<PEP_STATUS, In<PEP_SESSION,false>, In<c_string>, Out<PEP_comm_type>> ( &get_key_rating) ),
FP( "renew_key" , new Func<PEP_STATUS, In<PEP_SESSION,false>, In<c_string>, In<const timestamp*>> ( &renew_key) ),
FP( "revoke" , new Func<PEP_STATUS, In<PEP_SESSION,false>, In<c_string>, In<c_string>> ( &revoke_key) ),
FP( "key_expired" , new Func<PEP_STATUS, In<PEP_SESSION,false>, In<c_string>, In<time_t>, Out<bool>> ( &key_expired) ),
FP( "from blacklist.h & OpenPGP_compat.h", new Separator),
FP( "blacklist_add" , new Func<PEP_STATUS, In<PEP_SESSION,false>, In<const char*>> ( &blacklist_add) ),
FP( "blacklist_delete", new Func<PEP_STATUS, In<PEP_SESSION,false>, In<const char*>> ( &blacklist_delete) ),
FP( "blacklist_is_listed", new Func<PEP_STATUS, In<PEP_SESSION,false>, In<const char*>, Out<bool>> ( &blacklist_is_listed) ),
FP( "blacklist_add" , new Func<PEP_STATUS, In<PEP_SESSION,false>, In<c_string>> ( &blacklist_add) ),
FP( "blacklist_delete", new Func<PEP_STATUS, In<PEP_SESSION,false>, In<c_string>> ( &blacklist_delete) ),
FP( "blacklist_is_listed", new Func<PEP_STATUS, In<PEP_SESSION,false>, In<c_string>, Out<bool>> ( &blacklist_is_listed) ),
FP( "blacklist_retrieve" , new Func<PEP_STATUS, In<PEP_SESSION,false>, Out<stringlist_t*>> ( &blacklist_retrieve) ),
FP( "OpenPGP_list_keyinfo", new Func<PEP_STATUS, In<PEP_SESSION,false>, In<const char*>, Out<stringpair_list_t*>> ( &OpenPGP_list_keyinfo) ),
FP( "OpenPGP_list_keyinfo", new Func<PEP_STATUS, In<PEP_SESSION,false>, In<c_string>, Out<stringpair_list_t*>> ( &OpenPGP_list_keyinfo) ),
FP( "Event Listener & Results", new Separator ),
FP( "registerEventListener" , new Func<PEP_STATUS, In<Context*, false>, In<std::string>, In<unsigned>, In<std::string>> ( &registerEventListener) ),

Loading…
Cancel
Save