forked from BigfootDev/flatbuffers
* Added internal - limited - implementation of flatbuffers::unique_ptr for STLs that don't ship with std::unique_ptr. In C++11 and beyond this is just an alias for std::unique_ptr. * Aliased used type traits structs is_scalar is_floating_point is_unsigned into flatbuffers namespace so they can be replaced in C++98 implementations. Right now these point at stlport's TR1 implementations. * Wrapped vector::data() in vector_data(). * Wrapped vector::emplace_back() in vector_emplace_back(). * Wrapper string::back() in string_back(). * Added variants of FlatBufferBuilder::CreateVector() and FlatBufferBuilder::CreateVectorOfStructs() that allow the use of plain function pointers. Generated code has also been modified to use plain functions to build objects rather than std::function() so all generated code will work in C++98 applications. * Added flexbuffers::Builder::Vector(), flexbuffers::Builder::TypedVector() and flexbuffers::Builder::Map() methods that allow the use of plain function pointers. * Changed Parser to internally use plain function pointers when parsing table and vector delimiters. * Added specializations of NumToString() for 64-bit types that aren't supported by stringstream in stlport. * Overloaded numeric_limits for 64-bit types not supported by stlport. * Replaced build_apk.sh (which was broken by deprecation of the "android" tool in the Android SDK) with build.gradle and the appropriate gradle wrapper to build an APK. * Switched Android build to build against all STL variants. * Updated travis configuration to build Android test and sample. Tested: * Verified all tests continue to work on Linux, OSX and Android. * Verified Travis build is green. Change-Id: I9e634363793f85b9f141d21454b10686020a2065
132 lines
4.5 KiB
C++
132 lines
4.5 KiB
C++
/*
|
|
* Copyright 2017 Google Inc. All rights reserved.
|
|
*
|
|
* 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 FLATBUFFERS_REGISTRY_H_
|
|
#define FLATBUFFERS_REGISTRY_H_
|
|
|
|
#include "flatbuffers/idl.h"
|
|
|
|
namespace flatbuffers {
|
|
|
|
// Convenience class to easily parse or generate text for arbitrary FlatBuffers.
|
|
// Simply pre-populate it with all schema filenames that may be in use, and
|
|
// This class will look them up using the file_identifier declared in the
|
|
// schema.
|
|
class Registry {
|
|
public:
|
|
// Call this for all schemas that may be in use. The identifier has
|
|
// a function in the generated code, e.g. MonsterIdentifier().
|
|
void Register(const char *file_identifier, const char *schema_path) {
|
|
Schema schema;
|
|
schema.path_ = schema_path;
|
|
schemas_[file_identifier] = schema;
|
|
}
|
|
|
|
// Generate text from an arbitrary FlatBuffer by looking up its
|
|
// file_identifier in the registry.
|
|
bool FlatBufferToText(const uint8_t *flatbuf, size_t len,
|
|
std::string *dest) {
|
|
// Get the identifier out of the buffer.
|
|
// If the buffer is truncated, exit.
|
|
if (len < sizeof(uoffset_t) +
|
|
FlatBufferBuilder::kFileIdentifierLength) {
|
|
lasterror_ = "buffer truncated";
|
|
return false;
|
|
}
|
|
std::string ident(reinterpret_cast<const char *>(flatbuf) +
|
|
sizeof(uoffset_t),
|
|
FlatBufferBuilder::kFileIdentifierLength);
|
|
// Load and parse the schema.
|
|
Parser parser;
|
|
if (!LoadSchema(ident, &parser)) return false;
|
|
// Now we're ready to generate text.
|
|
if (!GenerateText(parser, flatbuf, dest)) {
|
|
lasterror_ = "unable to generate text for FlatBuffer binary";
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// Converts a binary buffer to text using one of the schemas in the registry,
|
|
// use the file_identifier to indicate which.
|
|
// If DetachedBuffer::data() is null then parsing failed.
|
|
DetachedBuffer TextToFlatBuffer(const char *text,
|
|
const char *file_identifier) {
|
|
// Load and parse the schema.
|
|
Parser parser;
|
|
if (!LoadSchema(file_identifier, &parser)) return DetachedBuffer();
|
|
// Parse the text.
|
|
if (!parser.Parse(text)) {
|
|
lasterror_ = parser.error_;
|
|
return DetachedBuffer();
|
|
}
|
|
// We have a valid FlatBuffer. Detach it from the builder and return.
|
|
return parser.builder_.ReleaseBufferPointer();
|
|
}
|
|
|
|
// Modify any parsing / output options used by the other functions.
|
|
void SetOptions(const IDLOptions &opts) { opts_ = opts; }
|
|
|
|
// If schemas used contain include statements, call this function for every
|
|
// directory the parser should search them for.
|
|
void AddIncludeDirectory(const char *path) {
|
|
include_paths_.push_back(path);
|
|
}
|
|
|
|
// Returns a human readable error if any of the above functions fail.
|
|
const std::string &GetLastError() { return lasterror_; }
|
|
|
|
private:
|
|
bool LoadSchema(const std::string &ident, Parser *parser) {
|
|
// Find the schema, if not, exit.
|
|
auto it = schemas_.find(ident);
|
|
if (it == schemas_.end()) {
|
|
// Don't attach the identifier, since it may not be human readable.
|
|
lasterror_ = "identifier for this buffer not in the registry";
|
|
return false;
|
|
}
|
|
auto &schema = it->second;
|
|
// Load the schema from disk. If not, exit.
|
|
std::string schematext;
|
|
if (!LoadFile(schema.path_.c_str(), false, &schematext)) {
|
|
lasterror_ = "could not load schema: " + schema.path_;
|
|
return false;
|
|
}
|
|
// Parse schema.
|
|
parser->opts = opts_;
|
|
if (!parser->Parse(schematext.c_str(), vector_data(include_paths_),
|
|
schema.path_.c_str())) {
|
|
lasterror_ = parser->error_;
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
struct Schema {
|
|
std::string path_;
|
|
// TODO(wvo) optionally cache schema file or parsed schema here.
|
|
};
|
|
|
|
std::string lasterror_;
|
|
IDLOptions opts_;
|
|
std::vector<const char *> include_paths_;
|
|
std::map<std::string, Schema> schemas_;
|
|
};
|
|
|
|
} // namespace flatbuffers
|
|
|
|
#endif // FLATBUFFERS_REGISTRY_H_
|