Skip to content

Commit

Permalink
implementing elements
Browse files Browse the repository at this point in the history
  • Loading branch information
voivoid committed Apr 3, 2017
1 parent da4c780 commit ff76299
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 23 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@
*.exe
*.out
*.app

CMakeLists.txt.user
9 changes: 6 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.8)
cmake_minimum_required(VERSION 3.7)
project(BsonParser LANGUAGES CXX)

add_library(BsonParser STATIC
Expand All @@ -9,10 +9,13 @@ add_library(BsonParser STATIC
src/utils.cpp
)

find_package(Boost 1.63.0 REQUIRED)
target_link_libraries(BsonParser Boost::boost)

target_include_directories(BsonParser PUBLIC inc)

if(NOT MSVC)
target_compile_options(BsonParser PUBLIC -Wall -Wextra -Werror -std=c++14)
if(NOT MSVC)
target_compile_options(BsonParser PUBLIC -Wall -Wextra -Werror -std=c++1z)
endif()

enable_testing()
Expand Down
File renamed without changes.
13 changes: 12 additions & 1 deletion inc/BSON/Details/utils.h → inc/Bson/Details/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
#include <cstdint>
#include <string>
#include <vector>

#include <boost/variant.hpp>
#include <boost/hana/map.hpp>

namespace Bson
{
Expand All @@ -24,7 +26,16 @@ void writeString(const std::string& string, Ostream& stream);
struct True {};
struct False {};

using Element = boost::variant<Double, std::string, False, True, Int32, Uint64, Int64, Uint64, Decimal>;
struct Element
{
static constexpr auto TypeInfoMap = boost::hana::make_map(boost::hana::make_pair(boost::hana::type_c<Double>, boost::hana::make_pair(0x01, &read<Double>)),
boost::hana::make_pair(boost::hana::type_c<Int32>, boost::hana::make_pair(0x10, &read<Int32>)));
using Value = decltype(boost::hana::unpack(boost::hana::keys(TypeInfoMap), boost::hana::template_<boost::variant>))::type;

std::string name;
Value value;
};

Element readElement(Istream& stream);
void writeElement(const Element& element, Ostream& stream);

Expand Down
2 changes: 0 additions & 2 deletions inc/BSON/parser.h → inc/Bson/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ namespace Bson
class Document
{
public:

private:
List _list;
};

Expand Down
2 changes: 1 addition & 1 deletion src/parser.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "BSON/parser.h"
#include "Bson/parser.h"

namespace Bson
{
Expand Down
36 changes: 22 additions & 14 deletions src/utils.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
#include "BSON/Details/utils.h"
#include "Bson/Details/utils.h"

#include <algorithm>
#include <cassert>
#include <limits>
#include <map>

#include <boost\bimap\bimap.hpp>
#include <boost/hana/fold_left.hpp>
#include <boost/hana/at_key.hpp>

namespace Bson
{

const size_t MaxCStringBuff = 16384;

//enum class ElementType {
// Double = 0x01,
// String = 0x02,
// Int32 = 0x10,
// Uint32 = 0x10,
// Int64 = 0x12,
// Decimal = 0x13
//};
//const boost::bimaps::bimap<int, ElementType> ElementTypes = {};
const auto IdxToTypeMap = boost::hana::fold_left(Element::TypeInfoMap, std::map<int, int>{}, [](auto map, auto pair) {

using Reader = Element::Value(*)(Istream&);
Reader r = &read<Double>;

return map;
});



// TODO: handle endianness

Expand Down Expand Up @@ -83,12 +85,18 @@ void writeString(const std::string& string, Ostream& stream)

Element readElement(Istream& stream)
{
const auto elementType = read<Byte>(stream);
//const auto elementTypeIdx = read<Byte>(stream);
auto name = readCString(stream);



Element elem;
elem.name = std::move(name);

return {};
return elem;
}

void writeElement(const Element& element, Ostream& stream)
void writeElement(const Element& /*element*/, Ostream& /*stream*/)
{

}
Expand Down
4 changes: 2 additions & 2 deletions test/test-utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
#include <string>
#include <vector>

#include <boost\iostreams\device\array.hpp>
#include <boost\iostreams\stream.hpp>
#include <boost/iostreams/device/array.hpp>
#include <boost/iostreams/stream.hpp>

template <typename T, typename WriteFunc, typename ReadFunc>
bool testMemory(const T& value, size_t buffSize, WriteFunc write, ReadFunc read)
Expand Down

0 comments on commit ff76299

Please sign in to comment.