Normalied CR/LF on service compiler templates

This commit is contained in:
Pedro Pinto committed 2017-08-11 20:51:53 +00:00
1 parent 0c80a2d6a8
commit ba05152f15
3 files changed
+287 -287

No files matched your search

@@ -1,63 +1,63 @@
#pragma once
#include "{{filename name}}.pb.h"
#include "lib/api/public/ServiceContext.h"
#include "lib/api/public/ClientChannel.h"
#include "lib/api/public/Result.h"
#include "lib/api/public/Update.h"
#include "lib/utils/bts_export.h"
#include <rxcpp/rx.hpp>
#include <string>
#include <functional>
#include <future>
#include <memory>
{{ns-open package}}
// Forward declaractions
{{#each message_type}}
class {{name}};
{{/each}}
{{#each service}}
{{#each method}}{{#if server_streaming}}
class {{../name}};
typedef bts::api::Update<std::shared_ptr<const {{output_type}}>, const {{key_type.cpptype}}, {{../name}}> {{cap name}}Update;
{{/if}}{{/each}}
// {{comment}}
class BTS_EXPORT {{name}} {
std::unique_ptr<bts::api::ClientChannel> _pchannel;
friend class bts::api::ServiceContext;
{{name}}(std::unique_ptr<bts::api::ClientChannel> pchannel);
// Key functions
{{#each method}} {{#if server_streaming}}
{{#if key_type.natural_key}}
static {{key_type.cpptype}} {{name}}_key(const {{output_type}}& row);
{{else}}
static const {{key_type.cpptype}}& {{name}}_key(const {{output_type}}& row);
{{/if}}
{{/if}} {{/each}}
public:
{{name}}({{name}}&&);
static const char* name() { return "{{name}}"; }
{{#each method}}
{{#if server_streaming}}
// {{comment}}
rxcpp::observable<{{cap name}}Update> subscribe_{{name}}(const {{input_type}}& qualifier);
// {{comment}}
rxcpp::observable<{{cap name}}Update> subscribe_{{name}}();
{{else}}
// {{comment}}
void {{name}}(const {{input_type}}&, std::function<void(bts::api::Result<{{output_type}}>)>, uint32_t timeout_ms=0);
// {{comment}}
bts::api::Result<{{output_type}}> {{name}}(const {{input_type}}&, uint32_t timeout_ms=0);
{{/if}}
{{/each}}
};
{{/each}}
{{ns-close package}}
#pragma once
#include "{{filename name}}.pb.h"
#include "lib/api/public/ServiceContext.h"
#include "lib/api/public/ClientChannel.h"
#include "lib/api/public/Result.h"
#include "lib/api/public/Update.h"
#include "lib/utils/bts_export.h"
#include <rxcpp/rx.hpp>
#include <string>
#include <functional>
#include <future>
#include <memory>
{{ns-open package}}
// Forward declaractions
{{#each message_type}}
class {{name}};
{{/each}}
{{#each service}}
{{#each method}}{{#if server_streaming}}
class {{../name}};
typedef bts::api::Update<std::shared_ptr<const {{output_type}}>, const {{key_type.cpptype}}, {{../name}}> {{cap name}}Update;
{{/if}}{{/each}}
// {{comment}}
class BTS_EXPORT {{name}} {
std::unique_ptr<bts::api::ClientChannel> _pchannel;
friend class bts::api::ServiceContext;
{{name}}(std::unique_ptr<bts::api::ClientChannel> pchannel);
// Key functions
{{#each method}} {{#if server_streaming}}
{{#if key_type.natural_key}}
static {{key_type.cpptype}} {{name}}_key(const {{output_type}}& row);
{{else}}
static const {{key_type.cpptype}}& {{name}}_key(const {{output_type}}& row);
{{/if}}
{{/if}} {{/each}}
public:
{{name}}({{name}}&&);
static const char* name() { return "{{name}}"; }
{{#each method}}
{{#if server_streaming}}
// {{comment}}
rxcpp::observable<{{cap name}}Update> subscribe_{{name}}(const {{input_type}}& qualifier);
// {{comment}}
rxcpp::observable<{{cap name}}Update> subscribe_{{name}}();
{{else}}
// {{comment}}
void {{name}}(const {{input_type}}&, std::function<void(bts::api::Result<{{output_type}}>)>, uint32_t timeout_ms=0);
// {{comment}}
bts::api::Result<{{output_type}}> {{name}}(const {{input_type}}&, uint32_t timeout_ms=0);
{{/if}}
{{/each}}
};
{{/each}}
{{ns-close package}}
@@ -1,167 +1,167 @@
{{#if service}}
#include "{{filename name}}.server.pb.h"
#include "lib/api/public/ServiceContext.h"
#include "lib/api/public/ApiUtils.h"
#include "lib/api/core/ApiServerInterop.h"
#include <unordered_map>
{{ns-open package}}
{{#each service}}
{{#each method}}{{#if server_streaming}}
{{#if key_type.natural_key}}
{{key_type.cpptype}} {{name}}_key(const {{output_type}}& row) {
return row.{{key_field}}();
}
{{else}}
const {{key_type.cpptype}}& {{name}}_key(const {{output_type}}& row) {
return row.{{key_field}}();
}
{{/if}}
{{/if}}{{/each}}
{{name}}Base::{{name}}Base(bts::api::ServiceContext& context, const std::string& qualifier) :
bts::api::ApiServer(context, "{{name}}", qualifier)
{}
const char* {{name}}Base::name() const { return "{{name}}"; };
void {{name}}Base::dispatch(const bts::api::RequestHeader& header, google::protobuf::io::ZeroCopyInputStream& stream, OnResponse on_response) {
switch((ServiceMethods)header.method()) {
case SYS_PING: {
bts::api::PingPong pp;
if(pp.ParsePartialFromZeroCopyStream(&stream)) {
respond(header.request_id(), pp, on_response);
}
} break;
{{#each method}} {{#unless server_streaming}}
case {{upper name}}: {
{{input_type}} msg;
if (msg.ParsePartialFromZeroCopyStream(&stream)) {
auto id = header.request_id();
{{name}}(msg, [this,id,on_response](const {{output_type}}& result) {
respond(id, result, on_response);
});
}
} break;
{{/unless}}{{/each}}
default: {
bts::api::ResponseHeader response;
response.set_request_id(header.request_id());
response.set_code(ApiError::MISSING_METHOD);
uint8_t buffer[64];
int header_size = response.ByteSize() + 1;
serialize(header_size, response, buffer);
on_response(buffer, header_size);
} break;
}
}
{{#each method}}{{#if server_streaming}}
{{../name}}Base::{{cap name}}Publisher {{../name}}Base::make_{{name}}_publisher(const {{input_type}}& qualifier) const {
TableHandle handle = _channel.create_table(format_qualifier("{{name}}", qualifier));
return make_{{name}}_publisher(handle);
}
{{../name}}Base::{{cap name}}Publisher {{../name}}Base::make_{{name}}_publisher() const {
TableHandle handle = _channel.create_table("{{name}}");
return make_{{name}}_publisher(handle);
}
{{../name}}Base::{{cap name}}Publisher {{../name}}Base::make_{{name}}_publisher(TableHandle handle) const {
{{#unless key_type.natural_key}}
std::unordered_map<{{key_type.cpptype}}, int> _map;
int _next_id;
std::shared_ptr<std::mutex> pmutex = std::make_shared<std::mutex>();
auto get_key = [=](const {{key_type.cpptype}}& key) mutable -> int {
std::lock_guard<std::mutex> guard(*pmutex);
auto iter = _map.find(key);
if (iter == _map.end()) {
int id = ++_next_id;
_map.insert(iter, std::make_pair(key, id));
return id;
}
return iter->second;
};
{{/unless}}
return [=](
const std::vector<{{output_type}}>* added,
const std::vector<{{output_type}}>* changed,
std::set<{{key_type.cpptype}}>* removed) mutable {
int add_count = added ? added->size() : 0;
int change_count = changed ? changed->size() : 0;
int remove_count = removed ? removed->size() : 0;
int total_updates = add_count + change_count + remove_count;
// Optimize small collections
StreamUpdate stack_buffer[256];
std::vector<StreamUpdate> heap_buffer;
StreamUpdate* pupdates = stack_buffer;
if (total_updates > 256) {
heap_buffer.resize(total_updates);
pupdates = &heap_buffer[0];
}
int dest = 0;
if (added) {
for(const auto& item : *added) {
StreamUpdate& update = pupdates[dest++];
update.type = StreamUpdate::CREATE;
if (removed != nullptr) {
auto iter = removed->find({{name}}_key(item));
if (iter != removed->end()) {
removed->erase(iter);
update.type = StreamUpdate::REPLACE;
}
}
{{#if key_type.natural_key}}
update.key = (int)({{name}}_key(item));
{{else}}
update.key = get_key({{name}}_key(item));
{{/if}}
update.buffer_len = item.ByteSize();
char* buffer = new char[update.buffer_len];
item.SerializePartialToArray(buffer, update.buffer_len);
update.buffer =buffer;
}
}
if (changed) {
for (const auto& item : *changed) {
StreamUpdate& update = pupdates[dest++];
update.type = StreamUpdate::REPLACE;
{{#if key_type.natural_key}}
update.key = (int)({{name}}_key(item));
{{else}}
update.key = get_key({{name}}_key(item));
{{/if}}
update.buffer_len = item.ByteSize();
char* buffer = new char[update.buffer_len];
item.SerializePartialToArray(buffer, update.buffer_len);
update.buffer =buffer;
}
}
if (removed) {
for (const auto& key : *removed) {
StreamUpdate& update = pupdates[dest++];
update.type = StreamUpdate::REMOVE;
{{#if key_type.natural_key}}
update.key = (int)(key);
{{else}}
update.key = get_key(key);
{{/if}}
update.buffer_len = 0;
update.buffer = nullptr;
}
}
_channel.publish(handle, pupdates, dest);
for (size_t x = 0; x < dest; ++x) {
if (pupdates[x].buffer != nullptr) {
delete[] pupdates[x].buffer;
}
}
};
}
{{/if}} {{/each}}
{{/each}}
{{ns-close package}}
{{/if}}
{{#if service}}
#include "{{filename name}}.server.pb.h"
#include "lib/api/public/ServiceContext.h"
#include "lib/api/public/ApiUtils.h"
#include "lib/api/core/ApiServerInterop.h"
#include <unordered_map>
{{ns-open package}}
{{#each service}}
{{#each method}}{{#if server_streaming}}
{{#if key_type.natural_key}}
{{key_type.cpptype}} {{name}}_key(const {{output_type}}& row) {
return row.{{key_field}}();
}
{{else}}
const {{key_type.cpptype}}& {{name}}_key(const {{output_type}}& row) {
return row.{{key_field}}();
}
{{/if}}
{{/if}}{{/each}}
{{name}}Base::{{name}}Base(bts::api::ServiceContext& context, const std::string& qualifier) :
bts::api::ApiServer(context, "{{name}}", qualifier)
{}
const char* {{name}}Base::name() const { return "{{name}}"; };
void {{name}}Base::dispatch(const bts::api::RequestHeader& header, google::protobuf::io::ZeroCopyInputStream& stream, OnResponse on_response) {
switch((ServiceMethods)header.method()) {
case SYS_PING: {
bts::api::PingPong pp;
if(pp.ParsePartialFromZeroCopyStream(&stream)) {
respond(header.request_id(), pp, on_response);
}
} break;
{{#each method}} {{#unless server_streaming}}
case {{upper name}}: {
{{input_type}} msg;
if (msg.ParsePartialFromZeroCopyStream(&stream)) {
auto id = header.request_id();
{{name}}(msg, [this,id,on_response](const {{output_type}}& result) {
respond(id, result, on_response);
});
}
} break;
{{/unless}}{{/each}}
default: {
bts::api::ResponseHeader response;
response.set_request_id(header.request_id());
response.set_code(ApiError::MISSING_METHOD);
uint8_t buffer[64];
int header_size = response.ByteSize() + 1;
serialize(header_size, response, buffer);
on_response(buffer, header_size);
} break;
}
}
{{#each method}}{{#if server_streaming}}
{{../name}}Base::{{cap name}}Publisher {{../name}}Base::make_{{name}}_publisher(const {{input_type}}& qualifier) const {
TableHandle handle = _channel.create_table(format_qualifier("{{name}}", qualifier));
return make_{{name}}_publisher(handle);
}
{{../name}}Base::{{cap name}}Publisher {{../name}}Base::make_{{name}}_publisher() const {
TableHandle handle = _channel.create_table("{{name}}");
return make_{{name}}_publisher(handle);
}
{{../name}}Base::{{cap name}}Publisher {{../name}}Base::make_{{name}}_publisher(TableHandle handle) const {
{{#unless key_type.natural_key}}
std::unordered_map<{{key_type.cpptype}}, int> _map;
int _next_id;
std::shared_ptr<std::mutex> pmutex = std::make_shared<std::mutex>();
auto get_key = [=](const {{key_type.cpptype}}& key) mutable -> int {
std::lock_guard<std::mutex> guard(*pmutex);
auto iter = _map.find(key);
if (iter == _map.end()) {
int id = ++_next_id;
_map.insert(iter, std::make_pair(key, id));
return id;
}
return iter->second;
};
{{/unless}}
return [=](
const std::vector<{{output_type}}>* added,
const std::vector<{{output_type}}>* changed,
std::set<{{key_type.cpptype}}>* removed) mutable {
int add_count = added ? added->size() : 0;
int change_count = changed ? changed->size() : 0;
int remove_count = removed ? removed->size() : 0;
int total_updates = add_count + change_count + remove_count;
// Optimize small collections
StreamUpdate stack_buffer[256];
std::vector<StreamUpdate> heap_buffer;
StreamUpdate* pupdates = stack_buffer;
if (total_updates > 256) {
heap_buffer.resize(total_updates);
pupdates = &heap_buffer[0];
}
int dest = 0;
if (added) {
for(const auto& item : *added) {
StreamUpdate& update = pupdates[dest++];
update.type = StreamUpdate::CREATE;
if (removed != nullptr) {
auto iter = removed->find({{name}}_key(item));
if (iter != removed->end()) {
removed->erase(iter);
update.type = StreamUpdate::REPLACE;
}
}
{{#if key_type.natural_key}}
update.key = (int)({{name}}_key(item));
{{else}}
update.key = get_key({{name}}_key(item));
{{/if}}
update.buffer_len = item.ByteSize();
char* buffer = new char[update.buffer_len];
item.SerializePartialToArray(buffer, update.buffer_len);
update.buffer =buffer;
}
}
if (changed) {
for (const auto& item : *changed) {
StreamUpdate& update = pupdates[dest++];
update.type = StreamUpdate::REPLACE;
{{#if key_type.natural_key}}
update.key = (int)({{name}}_key(item));
{{else}}
update.key = get_key({{name}}_key(item));
{{/if}}
update.buffer_len = item.ByteSize();
char* buffer = new char[update.buffer_len];
item.SerializePartialToArray(buffer, update.buffer_len);
update.buffer =buffer;
}
}
if (removed) {
for (const auto& key : *removed) {
StreamUpdate& update = pupdates[dest++];
update.type = StreamUpdate::REMOVE;
{{#if key_type.natural_key}}
update.key = (int)(key);
{{else}}
update.key = get_key(key);
{{/if}}
update.buffer_len = 0;
update.buffer = nullptr;
}
}
_channel.publish(handle, pupdates, dest);
for (size_t x = 0; x < dest; ++x) {
if (pupdates[x].buffer != nullptr) {
delete[] pupdates[x].buffer;
}
}
};
}
{{/if}} {{/each}}
{{/each}}
{{ns-close package}}
{{/if}}
@@ -1,57 +1,57 @@
#pragma once
#include "{{filename name}}.pb.h"
#include "lib/api/public/ApiServer.h"
#include "lib/utils/bts_export.h"
#include <string>
#include <functional>
#include <vector>
#include <set>
#include <memory>
// #include <iostream>
// #include <atomic>
namespace bts { namespace api {
class ServiceContext;
}}
{{ns-open package}}
// Forward declaractions
{{#each message_type}}
class {{name}};{{/each}}
{{#each service}}
// {{comment}}
class {{name}}Base : public bts::api::ApiServer {
protected:
{{name}}Base(bts::api::ServiceContext& context, const std::string& qualifier="");
const char* name() const override;
{{#each method}}
{{#if server_streaming}}
typedef std::function<void(const std::vector<{{output_type}}>*, const std::vector<{{output_type}}>*, std::set<{{key_type.cpptype}}>*)> {{cap name}}Publisher;
{{cap name}}Publisher make_{{name}}_publisher() const;
{{cap name}}Publisher make_{{name}}_publisher(const {{input_type}}& qualifier) const;
{{else}}
// {{comment}}
virtual void {{name}}(const {{input_type}}& request, std::function<void(const {{output_type}}&)> reply) = 0;
{{/if}}
{{/each}}
enum ServiceMethods {
SYS_PING = -1,
{{#each method}}
{{upper name}} = {{@index}},
{{/each}}
};
private:
void dispatch(const bts::api::RequestHeader&, google::protobuf::io::ZeroCopyInputStream&, OnResponse) override;
{{#each method}}{{#if server_streaming}}
{{cap name}}Publisher make_{{name}}_publisher(TableHandle) const;
{{/if}}{{/each}}
};
{{/each}}
{{ns-close package}}
#pragma once
#include "{{filename name}}.pb.h"
#include "lib/api/public/ApiServer.h"
#include "lib/utils/bts_export.h"
#include <string>
#include <functional>
#include <vector>
#include <set>
#include <memory>
// #include <iostream>
// #include <atomic>
namespace bts { namespace api {
class ServiceContext;
}}
{{ns-open package}}
// Forward declaractions
{{#each message_type}}
class {{name}};{{/each}}
{{#each service}}
// {{comment}}
class {{name}}Base : public bts::api::ApiServer {
protected:
{{name}}Base(bts::api::ServiceContext& context, const std::string& qualifier="");
const char* name() const override;
{{#each method}}
{{#if server_streaming}}
typedef std::function<void(const std::vector<{{output_type}}>*, const std::vector<{{output_type}}>*, std::set<{{key_type.cpptype}}>*)> {{cap name}}Publisher;
{{cap name}}Publisher make_{{name}}_publisher() const;
{{cap name}}Publisher make_{{name}}_publisher(const {{input_type}}& qualifier) const;
{{else}}
// {{comment}}
virtual void {{name}}(const {{input_type}}& request, std::function<void(const {{output_type}}&)> reply) = 0;
{{/if}}
{{/each}}
enum ServiceMethods {
SYS_PING = -1,
{{#each method}}
{{upper name}} = {{@index}},
{{/each}}
};
private:
void dispatch(const bts::api::RequestHeader&, google::protobuf::io::ZeroCopyInputStream&, OnResponse) override;
{{#each method}}{{#if server_streaming}}
{{cap name}}Publisher make_{{name}}_publisher(TableHandle) const;
{{/if}}{{/each}}
};
{{/each}}
{{ns-close package}}