Skip to content

Commit

Permalink
test_generator for msc
Browse files Browse the repository at this point in the history
  • Loading branch information
jll63 committed Aug 22, 2024
1 parent dfc7faf commit c546faf
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 147 deletions.
79 changes: 38 additions & 41 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,46 +85,43 @@ if (NOT MSVC)

# Thanks to https://stackoverflow.com/users/3440745/tsyvarev for providing the
# following cmake incantations.
endif()

if(CMAKE_CONFIGURATION_TYPES)
# Multi-configuration generator
#
# Explicitly use generator expression,
# so CMake won't automatically append the subdirectory with a config name.
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/$<CONFIG>)
else()
# Single-configuration generator
#
# While it is allowable to create config-specific directory,
# we don't do that: every configuration already has its own build tree.
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
endif()

# The executable will be created in the directory specified by
# CMAKE_RUNTIME_OUTPUT_DIRECTORY variable.

add_executable(test_generator_gen test_generator.cpp)
target_compile_definitions(test_generator_gen PRIVATE GENERATE)
target_link_libraries(test_generator_gen YOMM2::yomm2)
add_test(NAME test_generator_gen COMMAND test_generator_gen)

set(GENERATED_FILES
"${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test_generator_slots.hpp"
"${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test_generator_tables.hpp"
)
add_custom_command(
OUTPUT ${GENERATED_FILES}
DEPENDS test_generator_gen
COMMAND test_generator_gen
WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
)
add_custom_target(test_generator_generate DEPENDS ${GENERATED_FILES})
add_library(test_generator_headers INTERFACE)
add_dependencies(test_generator_headers test_generator_generate)
target_include_directories(test_generator_headers INTERFACE ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})

add_executable(test_generator test_generator.cpp)
add_dependencies(test_generator test_generator_headers)
target_link_libraries(test_generator PRIVATE test_generator_headers YOMM2::yomm2)
add_test(NAME test_generator COMMAND test_generator)
if(CMAKE_CONFIGURATION_TYPES)
# Multi-configuration generator
#
# Explicitly use generator expression,
# so CMake won't automatically append the subdirectory with a config name.
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/$<CONFIG>)
else()
# Single-configuration generator
#
# While it is allowable to create config-specific directory,
# we don't do that: every configuration already has its own build tree.
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
endif()

add_library(test_generator_lib OBJECT test_generator_domain.cpp)
target_link_libraries(test_generator_lib YOMM2::yomm2)

add_executable(test_generator_gen test_generator_gen.cpp)
target_link_libraries(test_generator_gen test_generator_lib YOMM2::yomm2)

set(GENERATED_FILES
"${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test_generator_slots.hpp"
"${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/test_generator_tables.hpp"
)
add_custom_command(
OUTPUT ${GENERATED_FILES}
COMMAND test_generator_gen
WORKING_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}
)
add_custom_target(test_generator_generate DEPENDS ${GENERATED_FILES})
add_library(test_generator_generated_lib INTERFACE)
add_dependencies(test_generator_generated_lib test_generator_generate)
target_include_directories(test_generator_generated_lib INTERFACE ${CMAKE_CURRENT_BINARY_DIR})

add_executable(test_generator test_generator.cpp)
target_include_directories(test_generator PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(test_generator PRIVATE test_generator_generated_lib test_generator_lib YOMM2::yomm2)
add_test(NAME test_generator COMMAND test_generator)
107 changes: 1 addition & 106 deletions tests/test_generator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,113 +6,10 @@
#include <sstream>
#include <memory>

#include <yorel/yomm2/policy.hpp>
#include "test_generator_domain.hpp"

using namespace yorel::yomm2;

struct throw_policy : default_policy::rebind<throw_policy>::replace<
policy::error_handler, policy::throw_error> {};

#define YOMM2_DEFAULT_POLICY throw_policy

#include <yorel/yomm2/keywords.hpp>

#ifndef GENERATE
#include "test_generator_slots.hpp"
#endif

struct Animal {
virtual ~Animal() {
}
};

struct Cat : Animal {};
struct Dog : Animal {};

struct Property {
explicit Property(std::string owner) : owner(owner) {
}

virtual ~Property() {
}

std::string owner;
};

struct DomesticCat : Cat, Property {
using Property::Property;
};

struct DomesticDog : Dog, Property {
using Property::Property;
};

register_classes(Animal, Cat, Dog, Property, DomesticCat, DomesticDog);

declare_method(void, kick, (virtual_<Animal&>, std::ostream&));

define_method(void, kick, (Dog&, std::ostream& os)) {
os << "bark";
}

define_method(void, kick, (Cat&, std::ostream& os)) {
os << "hiss";
}

declare_method(
void, meet, (virtual_<Animal&>, virtual_<Animal&>, std::ostream&));

// create an ambiguity with the following two definitions
define_method(void, meet, (Cat&, Animal&, std::ostream& os)) {
os << "ignore";
}

define_method(void, meet, (Animal&, Dog&, std::ostream& os)) {
os << "wag tail";
}
// ^^^ ambiguous

define_method(void, meet, (Dog&, Cat&, std::ostream& os)) {
os << "chase";
}

declare_method(void, identify, (virtual_<Property&>, std::ostream&));

define_method(void, identify, (DomesticCat & animal, std::ostream& os)) {
os << animal.owner << "'s"
<< " cat";
}

define_method(void, identify, (DomesticDog & animal, std::ostream& os)) {
os << animal.owner << "'s"
<< " dog";
}

#ifdef GENERATE

#include <iostream>

#include <yorel/yomm2/generator.hpp>

int main(int argc, char* argv[]) {
using namespace yorel::yomm2;

auto compiler = update<throw_policy>();
generator generator;

std::ofstream slots("test_generator_slots.hpp");
generator.add_forward_declarations()
.write_forward_declarations(slots)
.write_static_offsets(slots);

std::ofstream tables("test_generator_tables.hpp");
generator.encode_dispatch_data(compiler, tables);

return 0;
}

#else

static_assert(detail::has_static_offsets<method_class(
void, kick, (virtual_<Animal&>, std::ostream&))>::value);
static_assert(
Expand Down Expand Up @@ -175,5 +72,3 @@ BOOST_AUTO_TEST_CASE(test_generator) {
identify(*dog, os);
BOOST_TEST(os.str() == "Bob's dog");
}

#endif
36 changes: 36 additions & 0 deletions tests/test_generator_domain.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include "test_generator_domain.hpp"

register_classes(Animal, Cat, Dog, Property, DomesticCat, DomesticDog);

define_method(void, kick, (Dog&, std::ostream& os)) {
os << "bark";
}

define_method(void, kick, (Cat&, std::ostream& os)) {
os << "hiss";
}

// create an ambiguity with the following two definitions
define_method(void, meet, (Cat&, Animal&, std::ostream& os)) {
os << "ignore";
}

define_method(void, meet, (Animal&, Dog&, std::ostream& os)) {
os << "wag tail";
}
// ^^^ ambiguous

define_method(void, meet, (Dog&, Cat&, std::ostream& os)) {
os << "chase";
}


define_method(void, identify, (DomesticCat & animal, std::ostream& os)) {
os << animal.owner << "'s"
<< " cat";
}

define_method(void, identify, (DomesticDog & animal, std::ostream& os)) {
os << animal.owner << "'s"
<< " dog";
}
52 changes: 52 additions & 0 deletions tests/test_generator_domain.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#ifndef TEST_GENERATOR_DOMAIN_HPP
#define TEST_GENERATOR_DOMAIN_HPP

#include <string>

#include <yorel/yomm2/policy.hpp>

struct throw_policy
: yorel::yomm2::default_policy::rebind<throw_policy>::replace<
yorel::yomm2::policy::error_handler,
yorel::yomm2::policy::throw_error> {};

#define YOMM2_DEFAULT_POLICY throw_policy

#include <yorel/yomm2/keywords.hpp>

#if __has_include("test_generator_slots.hpp")
#include "test_generator_slots.hpp"
#endif

struct Animal {
virtual ~Animal() {
}
};

struct Cat : Animal {};
struct Dog : Animal {};

struct Property {
explicit Property(std::string owner) : owner(owner) {
}

virtual ~Property() {
}

std::string owner;
};

struct DomesticCat : Cat, Property {
using Property::Property;
};

struct DomesticDog : Dog, Property {
using Property::Property;
};

declare_method(void, kick, (virtual_<Animal&>, std::ostream&));
declare_method(
void, meet, (virtual_<Animal&>, virtual_<Animal&>, std::ostream&));
declare_method(void, identify, (virtual_<Property&>, std::ostream&));

#endif // TEST_GENERATOR_DOMAIN_HPP
27 changes: 27 additions & 0 deletions tests/test_generator_gen.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) 2018-2024 Jean-Louis Leroy
// Distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)


#include "test_generator_domain.hpp"

#include <iostream>
#include <yorel/yomm2/generator.hpp>

int main(int argc, char* argv[]) {
using namespace yorel::yomm2;

auto compiler = update<throw_policy>();
generator generator;

std::ofstream slots("test_generator_slots.hpp");
generator.add_forward_declarations()
.write_forward_declarations(slots)
.write_static_offsets(slots);

std::ofstream tables("test_generator_tables.hpp");
generator.encode_dispatch_data(compiler, tables);

return 0;
}

0 comments on commit c546faf

Please sign in to comment.