diff --git a/co_sim_io/c/co_sim_io_c.cpp b/co_sim_io/c/co_sim_io_c.cpp index 9cee1c3e..feb6d2fc 100644 --- a/co_sim_io/c/co_sim_io_c.cpp +++ b/co_sim_io/c/co_sim_io_c.cpp @@ -15,6 +15,7 @@ extern "C" { #include "co_sim_io_c.h" } #include "../co_sim_io.hpp" +DEFINE_SINGLETON_MAIN( ); namespace { // get C Info from C++ Info diff --git a/co_sim_io/impl/Threads.h b/co_sim_io/impl/Threads.h new file mode 100644 index 00000000..59b18bb4 --- /dev/null +++ b/co_sim_io/impl/Threads.h @@ -0,0 +1,199 @@ +#ifndef THREADS_H_ +#define THREADS_H_ + +//////////////////////////////////////////////////////////////////////////////// +// macro DEFAULT_THREADING +// Selects the default threading model for certain components of Loki +// If you don't define it, it defaults to single-threaded +// All classes in Loki have configurable threading model; DEFAULT_THREADING +// affects only default template arguments +//////////////////////////////////////////////////////////////////////////////// + +// Last update: June 20, 2001 + +#ifndef DEFAULT_THREADING +#define DEFAULT_THREADING /**/ ::Loki::SingleThreaded +#endif + +namespace Loki +{ +//////////////////////////////////////////////////////////////////////////////// +// class template SingleThreaded +// Implementation of the ThreadingModel policy used by various classes +// Implements a single-threaded model; no synchronization +//////////////////////////////////////////////////////////////////////////////// + + template + class SingleThreaded + { + public: + struct Lock + { + Lock() {} + explicit Lock(const SingleThreaded&) {} + }; + + typedef Host VolatileType; + + typedef int IntType; + + static IntType AtomicAdd(volatile IntType& lval, IntType val) + { return lval += val; } + + static IntType AtomicSubtract(volatile IntType& lval, IntType val) + { return lval -= val; } + + static IntType AtomicMultiply(volatile IntType& lval, IntType val) + { return lval *= val; } + + static IntType AtomicDivide(volatile IntType& lval, IntType val) + { return lval /= val; } + + static IntType AtomicIncrement(volatile IntType& lval) + { return ++lval; } + + static IntType AtomicDecrement(volatile IntType& lval) + { return --lval; } + + static void AtomicAssign(volatile IntType & lval, IntType val) + { lval = val; } + + static void AtomicAssign(IntType & lval, volatile IntType & val) + { lval = val; } + }; + +#ifdef _WINDOWS_ + +//////////////////////////////////////////////////////////////////////////////// +// class template ObjectLevelLockable +// Implementation of the ThreadingModel policy used by various classes +// Implements a object-level locking scheme +//////////////////////////////////////////////////////////////////////////////// + + template + class ObjectLevelLockable + { + mutable CRITICAL_SECTION mtx_; + + public: + ObjectLevelLockable() + { + ::InitializeCriticalSection(&mtx_); + } + + ~ObjectLevelLockable() + { + ::DeleteCriticalSection(&mtx_); + } + + class Lock; + friend class Lock; + + class Lock + { + ObjectLevelLockable const& host_; + + Lock(const Lock&); + Lock& operator=(const Lock&); + public: + + explicit Lock(const ObjectLevelLockable& host) : host_(host) + { + ::EnterCriticalSection(&host_.mtx_); + } + + ~Lock() + { + ::LeaveCriticalSection(&host_.mtx_); + } + }; + + typedef volatile Host VolatileType; + + typedef LONG IntType; + + static IntType AtomicIncrement(volatile IntType& lval) + { return InterlockedIncrement(&const_cast(lval)); } + + static IntType AtomicDecrement(volatile IntType& lval) + { return InterlockedDecrement(&const_cast(lval)); } + + static void AtomicAssign(volatile IntType& lval, IntType val) + { InterlockedExchange(&const_cast(lval), val); } + + static void AtomicAssign(IntType& lval, volatile IntType& val) + { InterlockedExchange(&lval, val); } + }; + + template + class ClassLevelLockable + { + struct Initializer + { + CRITICAL_SECTION mtx_; + + Initializer() + { + ::InitializeCriticalSection(&mtx_); + } + ~Initializer() + { + ::DeleteCriticalSection(&mtx_); + } + }; + + static Initializer initializer_; + + public: + class Lock; + friend class Lock; + + class Lock + { + Lock(const Lock&); + Lock& operator=(const Lock&); + public: + Lock() + { + ::EnterCriticalSection(&initializer_.mtx_); + } + explicit Lock(const ClassLevelLockable&) + { + ::EnterCriticalSection(&initializer_.mtx_); + } + ~Lock() + { + ::LeaveCriticalSection(&initializer_.mtx_); + } + }; + + typedef volatile Host VolatileType; + + typedef LONG IntType; + + static IntType AtomicIncrement(volatile IntType& lval) + { return InterlockedIncrement(&const_cast(lval)); } + + static IntType AtomicDecrement(volatile IntType& lval) + { return InterlockedDecrement(&const_cast(lval)); } + + static void AtomicAssign(volatile IntType& lval, IntType val) + { InterlockedExchange(&const_cast(lval), val); } + + static void AtomicAssign(IntType& lval, volatile IntType& val) + { InterlockedExchange(&lval, val); } + }; + + template + typename ClassLevelLockable::Initializer + ClassLevelLockable::initializer_; + +#endif +} + +//////////////////////////////////////////////////////////////////////////////// +// Change log: +// June 20, 2001: ported by Nick Thurn to gcc 2.95.3. Kudos, Nick!!! +//////////////////////////////////////////////////////////////////////////////// + +#endif diff --git a/co_sim_io/impl/co_sim_io_impl.hpp b/co_sim_io/impl/co_sim_io_impl.hpp index 279a4943..dbf2d86e 100644 --- a/co_sim_io/impl/co_sim_io_impl.hpp +++ b/co_sim_io/impl/co_sim_io_impl.hpp @@ -26,21 +26,84 @@ This file contains the implementation of the functions defined in "co_sim_io.hpp #include "utilities.hpp" #include "version.hpp" +#include "singleton.h" + +using maptype = std::unordered_map>; + +extern maptype registry_map; + +#define DEFINE_SINGLETON_MAIN( ) maptype registry_map + namespace CoSimIO { namespace Internals { -// TODO make sure this is unique even across compilation units (test somehow) -static std::unordered_map> s_co_sim_connections; -static bool HasIO(const std::string& rConnectionName) +template +class Singleton +{ +public: + static T& getInstance() + { + if (!m_instance) { + m_instance = CoSimIO::make_unique(); + } + return *m_instance; + } + +private: + static std::unique_ptr m_instance; +}; + +template +std::unique_ptr Singleton::m_instance = nullptr; + +template +class Singleton2 { +public: + static T& Instance(); + + Singleton2(const Singleton2&) = delete; + Singleton2& operator= (const Singleton2) = delete; + +protected: + Singleton2() {} +}; + +template +T& Singleton2::Instance() { - return s_co_sim_connections.find(rConnectionName) != s_co_sim_connections.end(); +// #ifdef CO_SIM_IO_EXTERN +// extern T t; +// #else + static T t; +// #endif + return t; } -static Connection& GetConnection(const std::string& rConnectionName) +// using maptype = std::unordered_map>; + +// typedef Loki::SingletonHolder SingleA; + +// // this function makes sure that the registry works correctly also across translation units / libraries +// // inline std::unordered_map>& GetRegistry() +// // { +// // static std::unordered_map> s_co_sim_connections; +// // return s_co_sim_connections; +// // } + +// using CoSimIOSingletonType = Singleton2; + +// extern maptype registry_map; + +inline bool HasIO(const std::string& rConnectionName) +{ + return registry_map.find(rConnectionName) != registry_map.end(); +} + +inline Connection& GetConnection(const std::string& rConnectionName) { CO_SIM_IO_ERROR_IF_NOT(HasIO(rConnectionName)) << "Trying to use connection \"" << rConnectionName << "\" which does not exist!" << std::endl; - return *s_co_sim_connections.at(rConnectionName); + return *registry_map.at(rConnectionName); } } // namespace Internals @@ -81,7 +144,7 @@ inline Info Connect(const Info& I_Settings) CO_SIM_IO_ERROR_IF(HasIO(connection_name)) << "A connection from \"" << my_name << "\" to \"" << connect_to << "\"already exists!" << std::endl; - s_co_sim_connections[connection_name] = std::unique_ptr(new Connection(I_Settings)); + registry_map[connection_name] = std::unique_ptr(new Connection(I_Settings)); auto info = GetConnection(connection_name).Connect(I_Settings); info.Set("connection_name", connection_name); @@ -96,7 +159,7 @@ inline Info Disconnect(const Info& I_Info) CO_SIM_IO_ERROR_IF_NOT(HasIO(connection_name)) << "Trying to disconnect connection \"" << connection_name << "\" which does not exist!" << std::endl; auto info = GetConnection(connection_name).Disconnect(I_Info); - s_co_sim_connections.erase(connection_name); + registry_map.erase(connection_name); return info; } @@ -214,4 +277,6 @@ inline Info Register( } // namespace CoSimIO + + #endif // CO_SIM_IO_IMPL_INCLUDED diff --git a/co_sim_io/impl/singleton.h b/co_sim_io/impl/singleton.h new file mode 100644 index 00000000..f0ba8795 --- /dev/null +++ b/co_sim_io/impl/singleton.h @@ -0,0 +1,464 @@ +//////////////////////////////////////////////////////////////////////////////// +// The Loki Library +// Copyright (c) 2001 by Andrei Alexandrescu +// This code accompanies the book: +// Alexandrescu, Andrei. "Modern C++ Design: Generic Programming and Design +// Patterns Applied". Copyright (c) 2001. Addison-Wesley. +// Permission to use, copy, modify, distribute and sell this software for any +// purpose is hereby granted without fee, provided that the above copyright +// notice appear in all copies and that both that copyright notice and this +// permission notice appear in supporting documentation. +// The author or Addison-Wesley Longman make no representations about the +// suitability of this software for any purpose. It is provided "as is" +// without express or implied warranty. +//////////////////////////////////////////////////////////////////////////////// + +#ifndef SINGLETON_INC_ +#define SINGLETON_INC_ + +#include "Threads.h" +#include +#include +#include +#include +#include + +#ifdef _MSC_VER +#define C_CALLING_CONVENTION_QUALIFIER __cdecl +#else +#define C_CALLING_CONVENTION_QUALIFIER +#endif + +namespace Loki +{ + typedef void (C_CALLING_CONVENTION_QUALIFIER *atexit_pfn_t)(); + + namespace Private + { +//////////////////////////////////////////////////////////////////////////////// +// class LifetimeTracker +// Helper class for SetLongevity +//////////////////////////////////////////////////////////////////////////////// + + class LifetimeTracker + { + public: + LifetimeTracker(unsigned int x) : longevity_(x) + {} + + virtual ~LifetimeTracker() = 0; + + static bool Compare(const LifetimeTracker* lhs, + const LifetimeTracker* rhs) + { + return lhs->longevity_ < rhs->longevity_; + } + + private: + unsigned int longevity_; + }; + + // Definition required + inline LifetimeTracker::~LifetimeTracker() {} + + // Helper data + typedef LifetimeTracker** TrackerArray; + extern TrackerArray pTrackerArray; + extern unsigned int elements; + + // Helper destroyer function + template + struct Deleter + { + static void Delete(T* pObj) + { delete pObj; } + }; + + // Concrete lifetime tracker for objects of type T + template + class ConcreteLifetimeTracker : public LifetimeTracker + { + public: + ConcreteLifetimeTracker(T* p,unsigned int longevity, Destroyer d) + : LifetimeTracker(longevity) + , pTracked_(p) + , destroyer_(d) + {} + + ~ConcreteLifetimeTracker() + { destroyer_(pTracked_); } + + private: + T* pTracked_; + Destroyer destroyer_; + }; + + void C_CALLING_CONVENTION_QUALIFIER AtExitFn(); // declaration needed below + } // namespace Private + +//////////////////////////////////////////////////////////////////////////////// +// function template SetLongevity +// Assigns an object a longevity; ensures ordered destructions of objects +// registered thusly during the exit sequence of the application +//////////////////////////////////////////////////////////////////////////////// + + template + void SetLongevity(T* pDynObject, unsigned int longevity, + Destroyer d = Private::Deleter::Delete) + { + using namespace Private; + + TrackerArray pNewArray = static_cast( + std::realloc(pTrackerArray, + sizeof(*pTrackerArray) * (elements + 1))); + if (!pNewArray) throw std::bad_alloc(); + + // Delayed assignment for exception safety + pTrackerArray = pNewArray; + + LifetimeTracker* p = new ConcreteLifetimeTracker( + pDynObject, longevity, d); + + // Insert a pointer to the object into the queue + TrackerArray pos = std::upper_bound( + pTrackerArray, + pTrackerArray + elements, + p, + LifetimeTracker::Compare); + std::copy_backward( + pos, + pTrackerArray + elements, + pTrackerArray + elements + 1); + *pos = p; + ++elements; + + // Register a call to AtExitFn + std::atexit(Private::AtExitFn); + } + +//////////////////////////////////////////////////////////////////////////////// +// class template CreateUsingNew +// Implementation of the CreationPolicy used by SingletonHolder +// Creates objects using a straight call to the new operator +//////////////////////////////////////////////////////////////////////////////// + + template struct CreateUsingNew + { + static T* Create() + { return new T; } + + static void Destroy(T* p) + { delete p; } + }; + +//////////////////////////////////////////////////////////////////////////////// +// class template CreateUsingNew +// Implementation of the CreationPolicy used by SingletonHolder +// Creates objects using a call to std::malloc, followed by a call to the +// placement new operator +//////////////////////////////////////////////////////////////////////////////// + + template struct CreateUsingMalloc + { + static T* Create() + { + void* p = std::malloc(sizeof(T)); + if (!p) return 0; + return new(p) T; + } + + static void Destroy(T* p) + { + p->~T(); + std::free(p); + } + }; + +//////////////////////////////////////////////////////////////////////////////// +// class template CreateStatic +// Implementation of the CreationPolicy used by SingletonHolder +// Creates an object in static memory +// Implementation is slightly nonportable because it uses the MaxAlign trick +// (an union of all types to ensure proper memory alignment). This trick is +// nonportable in theory but highly portable in practice. +//////////////////////////////////////////////////////////////////////////////// + + template struct CreateStatic + { +#if defined(_MSC_VER) && _MSC_VER >= 1300 +#pragma warning( push ) + // alignment of a member was sensitive to packing +#pragma warning( disable : 4121 ) +#endif // _MSC_VER + union MaxAlign + { + char t_[sizeof(T)]; + short int shortInt_; + int int_; + long int longInt_; + float float_; + double double_; + long double longDouble_; + struct Test; + int Test::* pMember_; + int (Test::*pMemberFn_)(int); + }; +#if defined(_MSC_VER) && _MSC_VER >= 1300 +#pragma warning( pop ) +#endif // _MSC_VER + + static T* Create() + { + static MaxAlign staticMemory_; + return new(&staticMemory_) T; + } + + static void Destroy(T* p) + { + p->~T(); + } + }; + +//////////////////////////////////////////////////////////////////////////////// +// class template DefaultLifetime +// Implementation of the LifetimePolicy used by SingletonHolder +// Schedules an object's destruction as per C++ rules +// Forwards to std::atexit +//////////////////////////////////////////////////////////////////////////////// + + template + struct DefaultLifetime + { + static void ScheduleDestruction(T*, atexit_pfn_t pFun) + { std::atexit(pFun); } + + static void OnDeadReference() + { throw std::logic_error("Dead Reference Detected"); } + }; + +//////////////////////////////////////////////////////////////////////////////// +// class template PhoenixSingleton +// Implementation of the LifetimePolicy used by SingletonHolder +// Schedules an object's destruction as per C++ rules, and it allows object +// recreation by not throwing an exception from OnDeadReference +//////////////////////////////////////////////////////////////////////////////// + + template + class PhoenixSingleton + { + public: + static void ScheduleDestruction(T*, atexit_pfn_t pFun) + { +#ifndef ATEXIT_FIXED + if (!destroyedOnce_) +#endif + std::atexit(pFun); + } + + static void OnDeadReference() + { +#ifndef ATEXIT_FIXED + destroyedOnce_ = true; +#endif + } + + private: +#ifndef ATEXIT_FIXED + static bool destroyedOnce_; +#endif + }; + +#ifndef ATEXIT_FIXED + template bool PhoenixSingleton::destroyedOnce_ = false; +#endif + +//////////////////////////////////////////////////////////////////////////////// +// class template Adapter +// Helper for SingletonWithLongevity below +//////////////////////////////////////////////////////////////////////////////// + + namespace Private + { + template + struct Adapter + { + void operator()(T*) { return pFun_(); } + atexit_pfn_t pFun_; + }; + } + +//////////////////////////////////////////////////////////////////////////////// +// class template SingletonWithLongevity +// Implementation of the LifetimePolicy used by SingletonHolder +// Schedules an object's destruction in order of their longevities +// Assumes a visible function GetLongevity(T*) that returns the longevity of the +// object +//////////////////////////////////////////////////////////////////////////////// + + template + class SingletonWithLongevity + { + public: + static void ScheduleDestruction(T* pObj, atexit_pfn_t pFun) + { + Private::Adapter adapter = { pFun }; + SetLongevity(pObj, GetLongevity(pObj), adapter); + } + + static void OnDeadReference() + { throw std::logic_error("Dead Reference Detected"); } + }; + +//////////////////////////////////////////////////////////////////////////////// +// class template NoDestroy +// Implementation of the LifetimePolicy used by SingletonHolder +// Never destroys the object +//////////////////////////////////////////////////////////////////////////////// + + template + struct NoDestroy + { + static void ScheduleDestruction(T*, atexit_pfn_t pFun) + {} + + static void OnDeadReference() + {} + }; + +//////////////////////////////////////////////////////////////////////////////// +// class template SingletonHolder +// Provides Singleton amenities for a type T +// To protect that type from spurious instantiations, you have to protect it +// yourself. +//////////////////////////////////////////////////////////////////////////////// + + template + < + typename T, + template class CreationPolicy = CreateUsingNew, + template class LifetimePolicy = DefaultLifetime, + template class ThreadingModel = SingleThreaded + > + class SingletonHolder + { + public: + static T& Instance(); + + private: + // Helpers + static void MakeInstance(); + static void C_CALLING_CONVENTION_QUALIFIER DestroySingleton(); + + // Protection + SingletonHolder(); + + // Data + typedef typename ThreadingModel::VolatileType PtrInstanceType; + static PtrInstanceType pInstance_; + static bool destroyed_; + }; + +//////////////////////////////////////////////////////////////////////////////// +// SingletonHolder's data +//////////////////////////////////////////////////////////////////////////////// + + template + < + class T, + template class C, + template class L, + template class M + > + typename SingletonHolder::PtrInstanceType + SingletonHolder::pInstance_; + + template + < + class T, + template class C, + template class L, + template class M + > + bool SingletonHolder::destroyed_; + +//////////////////////////////////////////////////////////////////////////////// +// SingletonHolder::Instance +//////////////////////////////////////////////////////////////////////////////// + + template + < + class T, + template class CreationPolicy, + template class LifetimePolicy, + template class ThreadingModel + > + inline T& SingletonHolder::Instance() + { + if (!pInstance_) + { + MakeInstance(); + } + return *pInstance_; + } + +//////////////////////////////////////////////////////////////////////////////// +// SingletonHolder::MakeInstance (helper for Instance) +//////////////////////////////////////////////////////////////////////////////// + + template + < + class T, + template class CreationPolicy, + template class LifetimePolicy, + template class ThreadingModel + > + void SingletonHolder::MakeInstance() + { + typename ThreadingModel::Lock guard; + (void)guard; + + if (!pInstance_) + { + if (destroyed_) + { + LifetimePolicy::OnDeadReference(); + destroyed_ = false; + } + pInstance_ = CreationPolicy::Create(); + LifetimePolicy::ScheduleDestruction(pInstance_, + &DestroySingleton); + } + } + + template + < + class T, + template class CreationPolicy, + template class L, + template class M + > + void C_CALLING_CONVENTION_QUALIFIER + SingletonHolder::DestroySingleton() + { + assert(!destroyed_); + CreationPolicy::Destroy(pInstance_); + pInstance_ = 0; + destroyed_ = true; + } +} // namespace Loki + +//////////////////////////////////////////////////////////////////////////////// +// Change log: +// May 21, 2001: Correct the volatile qualifier - credit due to Darin Adler +// June 20, 2001: ported by Nick Thurn to gcc 2.95.3. Kudos, Nick!!! +// January 08, 2002: Fixed bug in call to realloc - credit due to Nigel Gent and +// Eike Petersen +// March 08, 2002: moved the assignment to pTrackerArray in SetLongevity to fix +// exception safety issue. Credit due to Kari Hoijarvi +// May 09, 2002: Fixed bug in Compare that caused longevities to act backwards. +// Credit due to Scott McDonald. +//////////////////////////////////////////////////////////////////////////////// + +#endif // SINGLETON_INC_ diff --git a/co_sim_io/python/co_sim_io_python.cpp b/co_sim_io/python/co_sim_io_python.cpp index 683b6457..09bfd063 100644 --- a/co_sim_io/python/co_sim_io_python.cpp +++ b/co_sim_io/python/co_sim_io_python.cpp @@ -31,6 +31,7 @@ PYBIND11_DECLARE_HOLDER_TYPE(T, CoSimIO::intrusive_ptr) // CoSimIO includes #include "../co_sim_io.hpp" +DEFINE_SINGLETON_MAIN( ); #include "info_to_python.hpp" #include "model_part_to_python.hpp" #include "vector_to_python.hpp" diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index e7da9d50..e1d65d40 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -31,6 +31,9 @@ if (CO_SIM_IO_ENABLE_MPI) endif() +# singleton tests +add_subdirectory(co_sim_io/impl/singleton_tests) + ### C++ tests ### function(add_cpp_executable TEST_SOURCE_FILE) get_filename_component(TEST_FILENAME ${TEST_SOURCE_FILE} NAME) diff --git a/tests/co_sim_io/impl/singleton_tests/CMakeLists.txt b/tests/co_sim_io/impl/singleton_tests/CMakeLists.txt new file mode 100644 index 00000000..7bd6b431 --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/CMakeLists.txt @@ -0,0 +1,18 @@ +include_directories( ${CMAKE_SOURCE_DIR}/co_sim_io ) + +add_executable ( main_singleton_partner main_partner.cpp ) +install( TARGETS main_singleton_partner DESTINATION ${CMAKE_SOURCE_DIR}/bin/singleton_tests ) +set_target_properties( main_singleton_partner PROPERTIES + CXX_STANDARD 11 + CXX_STANDARD_REQUIRED YES + CXX_EXTENSIONS NO +) + +# add_subdirectory(scenario_1) +# add_subdirectory(scenario_2) +# add_subdirectory(scenario_3) +# add_subdirectory(scenario_4) +# add_subdirectory(scenario_5) +# add_subdirectory(scenario_6) +# add_subdirectory(source) +add_subdirectory(source_cosimio) diff --git a/tests/co_sim_io/impl/singleton_tests/main_partner.cpp b/tests/co_sim_io/impl/singleton_tests/main_partner.cpp new file mode 100644 index 00000000..da7a3c71 --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/main_partner.cpp @@ -0,0 +1,42 @@ +// ______ _____ _ ________ +// / ____/___ / ___/(_)___ ___ / _/ __ | +// / / / __ \\__ \/ / __ `__ \ / // / / / +// / /___/ /_/ /__/ / / / / / / // // /_/ / +// \____/\____/____/_/_/ /_/ /_/___/\____/ +// Kratos CoSimulationApplication +// +// License: BSD License, see license.txt +// +// Main authors: Philipp Bucher (https://github.com/philbucher) +// + +// CoSimulation includes +#include "co_sim_io.hpp" +DEFINE_SINGLETON_MAIN( ); + +#define COSIMIO_CHECK_EQUAL(a, b) \ + if (a != b) { \ + std::cout << "in line " << __LINE__ << " : " << a \ + << " is not equal to " << b << std::endl; \ + return 1; \ + } + +int main() +{ + CoSimIO::Info settings; + settings.Set("my_name", "singleton_tester_partner"); + settings.Set("connect_to", "singleton_tester"); + settings.Set("echo_level", 1); + settings.Set("version", "1.25"); + + auto info = CoSimIO::Connect(settings); + COSIMIO_CHECK_EQUAL(info.Get("connection_status"), CoSimIO::ConnectionStatus::Connected); + const std::string connection_name = info.Get("connection_name"); + + CoSimIO::Info disconnect_settings; + disconnect_settings.Set("connection_name", connection_name); + info = CoSimIO::Disconnect(disconnect_settings); // disconnect afterwards + COSIMIO_CHECK_EQUAL(info.Get("connection_status"), CoSimIO::ConnectionStatus::Disconnected); + + return 0; +} diff --git a/tests/co_sim_io/impl/singleton_tests/scenario_1/CMakeLists.txt b/tests/co_sim_io/impl/singleton_tests/scenario_1/CMakeLists.txt new file mode 100644 index 00000000..fdfbe567 --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/scenario_1/CMakeLists.txt @@ -0,0 +1,15 @@ +include_directories( ${CMAKE_SOURCE_DIR}/co_sim_io ) + +add_library(singleton_tests_scenario_1_ext_lib SHARED ext_lib.cpp ) +install( TARGETS singleton_tests_scenario_1_ext_lib DESTINATION ${CMAKE_SOURCE_DIR}/bin ) + +add_executable ( main_scenario_1 main.cpp ) +target_link_libraries( main_scenario_1 singleton_tests_scenario_1_ext_lib ) +install( TARGETS main_scenario_1 DESTINATION ${CMAKE_SOURCE_DIR}/bin/singleton_tests/scenario_1/ ) +set_target_properties( main_scenario_1 PROPERTIES + CXX_STANDARD 11 + CXX_STANDARD_REQUIRED YES + CXX_EXTENSIONS NO +) + +add_test(NAME singleton_tests_scenario_1 COMMAND sh ../../../../run.sh $ $) diff --git a/tests/co_sim_io/impl/singleton_tests/scenario_1/ext_lib.cpp b/tests/co_sim_io/impl/singleton_tests/scenario_1/ext_lib.cpp new file mode 100644 index 00000000..1ed94749 --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/scenario_1/ext_lib.cpp @@ -0,0 +1,21 @@ +// ______ _____ _ ________ +// / ____/___ / ___/(_)___ ___ / _/ __ | +// / / / __ \\__ \/ / __ `__ \ / // / / / +// / /___/ /_/ /__/ / / / / / / // // /_/ / +// \____/\____/____/_/_/ /_/ /_/___/\____/ +// Kratos CoSimulationApplication +// +// License: BSD License, see license.txt +// +// Main authors: Philipp Bucher (https://github.com/philbucher) +// + +// Project includes +#include "co_sim_io.hpp" +DEFINE_SINGLETON_MAIN( ); +#include "ext_lib.hpp" + +bool CheckExtLibHasConnection(const std::string& ConnectionName) +{ + return CoSimIO::Internals::HasIO(ConnectionName); +} \ No newline at end of file diff --git a/tests/co_sim_io/impl/singleton_tests/scenario_1/ext_lib.hpp b/tests/co_sim_io/impl/singleton_tests/scenario_1/ext_lib.hpp new file mode 100644 index 00000000..3840e405 --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/scenario_1/ext_lib.hpp @@ -0,0 +1,16 @@ +// ______ _____ _ ________ +// / ____/___ / ___/(_)___ ___ / _/ __ | +// / / / __ \\__ \/ / __ `__ \ / // / / / +// / /___/ /_/ /__/ / / / / / / // // /_/ / +// \____/\____/____/_/_/ /_/ /_/___/\____/ +// Kratos CoSimulationApplication +// +// License: BSD License, see license.txt +// +// Main authors: Philipp Bucher (https://github.com/philbucher) +// + +// System includes +#include + +bool CheckExtLibHasConnection(const std::string& ConnectionName); diff --git a/tests/co_sim_io/impl/singleton_tests/scenario_1/main.cpp b/tests/co_sim_io/impl/singleton_tests/scenario_1/main.cpp new file mode 100644 index 00000000..f12ed3af --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/scenario_1/main.cpp @@ -0,0 +1,58 @@ +// ______ _____ _ ________ +// / ____/___ / ___/(_)___ ___ / _/ __ | +// / / / __ \\__ \/ / __ `__ \ / // / / / +// / /___/ /_/ /__/ / / / / / / // // /_/ / +// \____/\____/____/_/_/ /_/ /_/___/\____/ +// Kratos CoSimulationApplication +// +// License: BSD License, see license.txt +// +// Main authors: Philipp Bucher (https://github.com/philbucher) +// + +/* +Testing if the CoSimIO can be safely included in complex libraries +In this scenario the CoSimIO is included in the main executable +and in the external library +Connecting and disconnecting is done in the main executable +*/ + +// CoSimulation includes +#include "co_sim_io.hpp" +#include "ext_lib.hpp" + +#define COSIMIO_CHECK_EQUAL(a, b) \ + if (a != b) { \ + std::cout << "in line " << __LINE__ << " : " << a \ + << " is not equal to " << b << std::endl; \ + return 1; \ + } + +#define COSIMIO_CHECK_True(a) \ + if (!a) { \ + std::cout << "in line " << __LINE__ << " : " \ + << "false is not true" << std::endl; \ + return 1; \ + } + +int main() +{ + CoSimIO::Info settings; + settings.Set("my_name", "singleton_tester"); + settings.Set("connect_to", "singleton_tester_partner"); + settings.Set("echo_level", 1); + settings.Set("version", "1.25"); + + auto info = CoSimIO::Connect(settings); + COSIMIO_CHECK_EQUAL(info.Get("connection_status"), CoSimIO::ConnectionStatus::Connected); + const std::string connection_name = info.Get("connection_name"); + + COSIMIO_CHECK_True(CheckExtLibHasConnection(connection_name)); + + CoSimIO::Info disconnect_settings; + disconnect_settings.Set("connection_name", connection_name); + info = CoSimIO::Disconnect(disconnect_settings); // disconnect afterwards + COSIMIO_CHECK_EQUAL(info.Get("connection_status"), CoSimIO::ConnectionStatus::Disconnected); + + return 0; +} diff --git a/tests/co_sim_io/impl/singleton_tests/scenario_2/CMakeLists.txt b/tests/co_sim_io/impl/singleton_tests/scenario_2/CMakeLists.txt new file mode 100644 index 00000000..fe8810ec --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/scenario_2/CMakeLists.txt @@ -0,0 +1,15 @@ +include_directories( ${CMAKE_SOURCE_DIR}/co_sim_io ) + +add_library(singleton_tests_scenario_2_ext_lib SHARED ext_lib.cpp ) +install( TARGETS singleton_tests_scenario_2_ext_lib DESTINATION ${CMAKE_SOURCE_DIR}/bin ) + +add_executable ( main_scenario_2 main.cpp ) +target_link_libraries( main_scenario_2 singleton_tests_scenario_2_ext_lib ) +install( TARGETS main_scenario_2 DESTINATION ${CMAKE_SOURCE_DIR}/bin/singleton_tests/scenario_2/ ) +set_target_properties( main_scenario_2 PROPERTIES + CXX_STANDARD 11 + CXX_STANDARD_REQUIRED YES + CXX_EXTENSIONS NO +) + +add_test(NAME singleton_tests_scenario_2 COMMAND sh ../../../../run.sh $ $) diff --git a/tests/co_sim_io/impl/singleton_tests/scenario_2/ext_lib.cpp b/tests/co_sim_io/impl/singleton_tests/scenario_2/ext_lib.cpp new file mode 100644 index 00000000..cd5614e7 --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/scenario_2/ext_lib.cpp @@ -0,0 +1,35 @@ +// ______ _____ _ ________ +// / ____/___ / ___/(_)___ ___ / _/ __ | +// / / / __ \\__ \/ / __ `__ \ / // / / / +// / /___/ /_/ /__/ / / / / / / // // /_/ / +// \____/\____/____/_/_/ /_/ /_/___/\____/ +// Kratos CoSimulationApplication +// +// License: BSD License, see license.txt +// +// Main authors: Philipp Bucher (https://github.com/philbucher) +// + +// Project includes +#include "co_sim_io.hpp" +DEFINE_SINGLETON_MAIN( ); +#include "ext_lib.hpp" + +std::string ConnectToCoSimIO() +{ + CoSimIO::Info settings; + settings.Set("my_name", "singleton_tester"); + settings.Set("connect_to", "singleton_tester_partner"); + settings.Set("echo_level", 1); + settings.Set("version", "1.25"); + + auto info = CoSimIO::Connect(settings); + return info.Get("connection_name"); +} + +void DisconnectFromCoSimIO(const std::string& rConnectionName) +{ + CoSimIO::Info disconnect_settings; + disconnect_settings.Set("connection_name", rConnectionName); + CoSimIO::Disconnect(disconnect_settings); +} \ No newline at end of file diff --git a/tests/co_sim_io/impl/singleton_tests/scenario_2/ext_lib.hpp b/tests/co_sim_io/impl/singleton_tests/scenario_2/ext_lib.hpp new file mode 100644 index 00000000..2ec1c264 --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/scenario_2/ext_lib.hpp @@ -0,0 +1,18 @@ +// ______ _____ _ ________ +// / ____/___ / ___/(_)___ ___ / _/ __ | +// / / / __ \\__ \/ / __ `__ \ / // / / / +// / /___/ /_/ /__/ / / / / / / // // /_/ / +// \____/\____/____/_/_/ /_/ /_/___/\____/ +// Kratos CoSimulationApplication +// +// License: BSD License, see license.txt +// +// Main authors: Philipp Bucher (https://github.com/philbucher) +// + +// System includes +#include + +std::string ConnectToCoSimIO(); + +void DisconnectFromCoSimIO(const std::string& rConnectionName); diff --git a/tests/co_sim_io/impl/singleton_tests/scenario_2/main.cpp b/tests/co_sim_io/impl/singleton_tests/scenario_2/main.cpp new file mode 100644 index 00000000..46e9a5f0 --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/scenario_2/main.cpp @@ -0,0 +1,40 @@ +// ______ _____ _ ________ +// / ____/___ / ___/(_)___ ___ / _/ __ | +// / / / __ \\__ \/ / __ `__ \ / // / / / +// / /___/ /_/ /__/ / / / / / / // // /_/ / +// \____/\____/____/_/_/ /_/ /_/___/\____/ +// Kratos CoSimulationApplication +// +// License: BSD License, see license.txt +// +// Main authors: Philipp Bucher (https://github.com/philbucher) +// + +/* +Testing if the CoSimIO can be safely included in complex libraries +In this scenario the CoSimIO is included in the main executable +and in the external library +Connecting and disconnecting is done in the external library +*/ + +// CoSimulation includes +#include "co_sim_io.hpp" +#include "ext_lib.hpp" + +#define COSIMIO_CHECK_True(a) \ + if (!a) { \ + std::cout << "in line " << __LINE__ << " : " \ + << "false is not true" << std::endl; \ + return 1; \ + } + +int main() +{ + const std::string connection_name = ConnectToCoSimIO(); + + COSIMIO_CHECK_True(CoSimIO::Internals::HasIO(connection_name)); + + DisconnectFromCoSimIO(connection_name); + + return 0; +} diff --git a/tests/co_sim_io/impl/singleton_tests/scenario_3/CMakeLists.txt b/tests/co_sim_io/impl/singleton_tests/scenario_3/CMakeLists.txt new file mode 100644 index 00000000..54bedfed --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/scenario_3/CMakeLists.txt @@ -0,0 +1,15 @@ +include_directories( ${CMAKE_SOURCE_DIR}/co_sim_io ) + +add_library(singleton_tests_scenario_3_ext_lib SHARED ext_lib.cpp ) +install( TARGETS singleton_tests_scenario_3_ext_lib DESTINATION ${CMAKE_SOURCE_DIR}/bin ) + +add_executable ( main_scenario_3 main.cpp ) +target_link_libraries( main_scenario_3 singleton_tests_scenario_3_ext_lib ) +install( TARGETS main_scenario_3 DESTINATION ${CMAKE_SOURCE_DIR}/bin/singleton_tests/scenario_3/ ) +set_target_properties( main_scenario_3 PROPERTIES + CXX_STANDARD 11 + CXX_STANDARD_REQUIRED YES + CXX_EXTENSIONS NO +) + +add_test(NAME singleton_tests_scenario_3 COMMAND sh ../../../../run.sh $ $) diff --git a/tests/co_sim_io/impl/singleton_tests/scenario_3/ext_lib.cpp b/tests/co_sim_io/impl/singleton_tests/scenario_3/ext_lib.cpp new file mode 100644 index 00000000..7bf50de6 --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/scenario_3/ext_lib.cpp @@ -0,0 +1,26 @@ +// ______ _____ _ ________ +// / ____/___ / ___/(_)___ ___ / _/ __ | +// / / / __ \\__ \/ / __ `__ \ / // / / / +// / /___/ /_/ /__/ / / / / / / // // /_/ / +// \____/\____/____/_/_/ /_/ /_/___/\____/ +// Kratos CoSimulationApplication +// +// License: BSD License, see license.txt +// +// Main authors: Philipp Bucher (https://github.com/philbucher) +// + +#include "ext_lib.hpp" + +std::string ConnectToCoSimIO(CoSimIO::Info& rSettings) +{ + auto info = CoSimIO::Connect(rSettings); + return info.Get("connection_name"); +} + +void DisconnectFromCoSimIO(const std::string& rConnectionName) +{ + CoSimIO::Info disconnect_settings; + disconnect_settings.Set("connection_name", rConnectionName); + CoSimIO::Disconnect(disconnect_settings); +} \ No newline at end of file diff --git a/tests/co_sim_io/impl/singleton_tests/scenario_3/ext_lib.hpp b/tests/co_sim_io/impl/singleton_tests/scenario_3/ext_lib.hpp new file mode 100644 index 00000000..1835147f --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/scenario_3/ext_lib.hpp @@ -0,0 +1,22 @@ +// ______ _____ _ ________ +// / ____/___ / ___/(_)___ ___ / _/ __ | +// / / / __ \\__ \/ / __ `__ \ / // / / / +// / /___/ /_/ /__/ / / / / / / // // /_/ / +// \____/\____/____/_/_/ /_/ /_/___/\____/ +// Kratos CoSimulationApplication +// +// License: BSD License, see license.txt +// +// Main authors: Philipp Bucher (https://github.com/philbucher) +// + +// System includes +#include + +// Project includes +#include "co_sim_io.hpp" +DEFINE_SINGLETON_MAIN( ); + +std::string ConnectToCoSimIO(CoSimIO::Info& rSettings); + +void DisconnectFromCoSimIO(const std::string& rConnectionName); diff --git a/tests/co_sim_io/impl/singleton_tests/scenario_3/main.cpp b/tests/co_sim_io/impl/singleton_tests/scenario_3/main.cpp new file mode 100644 index 00000000..b889f6e2 --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/scenario_3/main.cpp @@ -0,0 +1,48 @@ +// ______ _____ _ ________ +// / ____/___ / ___/(_)___ ___ / _/ __ | +// / / / __ \\__ \/ / __ `__ \ / // / / / +// / /___/ /_/ /__/ / / / / / / // // /_/ / +// \____/\____/____/_/_/ /_/ /_/___/\____/ +// Kratos CoSimulationApplication +// +// License: BSD License, see license.txt +// +// Main authors: Philipp Bucher (https://github.com/philbucher) +// + +/* +Testing if the CoSimIO can be safely included in complex libraries +In this scenario the CoSimIO is included in the main executable +and in the external library +Connecting and disconnecting is done in the external library +Difference to scenario 2: The CoSimIO is also included in the +header of the ext library +*/ + +// CoSimulation includes +#include "co_sim_io.hpp" +#include "ext_lib.hpp" + +#define COSIMIO_CHECK_True(a) \ + if (!a) { \ + std::cout << "in line " << __LINE__ << " : " \ + << "false is not true" << std::endl; \ + return 1; \ + } + +int main() +{ + CoSimIO::Info settings; + settings.Set("my_name", "singleton_tester"); + settings.Set("connect_to", "singleton_tester_partner"); + settings.Set("echo_level", 1); + settings.Set("version", "1.25"); + + const std::string connection_name = ConnectToCoSimIO(settings); + + COSIMIO_CHECK_True(CoSimIO::Internals::HasIO(connection_name)); + + DisconnectFromCoSimIO(connection_name); + + return 0; +} diff --git a/tests/co_sim_io/impl/singleton_tests/scenario_4/CMakeLists.txt b/tests/co_sim_io/impl/singleton_tests/scenario_4/CMakeLists.txt new file mode 100644 index 00000000..0e083f6a --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/scenario_4/CMakeLists.txt @@ -0,0 +1,18 @@ +include_directories( ${CMAKE_SOURCE_DIR}/co_sim_io ) + +add_library(singleton_tests_scenario_4_ext_lib_1 SHARED ext_lib_1.cpp ) +install( TARGETS singleton_tests_scenario_4_ext_lib_1 DESTINATION ${CMAKE_SOURCE_DIR}/bin ) + +add_library(singleton_tests_scenario_4_ext_lib_2 SHARED ext_lib_2.cpp ) +install( TARGETS singleton_tests_scenario_4_ext_lib_2 DESTINATION ${CMAKE_SOURCE_DIR}/bin ) + +add_executable ( main_scenario_4 main.cpp ) +target_link_libraries( main_scenario_4 singleton_tests_scenario_4_ext_lib_1 singleton_tests_scenario_4_ext_lib_2 ) +install( TARGETS main_scenario_4 DESTINATION ${CMAKE_SOURCE_DIR}/bin/singleton_tests/scenario_4/ ) +set_target_properties( main_scenario_4 PROPERTIES + CXX_STANDARD 11 + CXX_STANDARD_REQUIRED YES + CXX_EXTENSIONS NO +) + +add_test(NAME singleton_tests_scenario_4 COMMAND sh ../../../../run.sh $ $) diff --git a/tests/co_sim_io/impl/singleton_tests/scenario_4/ext_lib_1.cpp b/tests/co_sim_io/impl/singleton_tests/scenario_4/ext_lib_1.cpp new file mode 100644 index 00000000..29b4b0f4 --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/scenario_4/ext_lib_1.cpp @@ -0,0 +1,35 @@ +// ______ _____ _ ________ +// / ____/___ / ___/(_)___ ___ / _/ __ | +// / / / __ \\__ \/ / __ `__ \ / // / / / +// / /___/ /_/ /__/ / / / / / / // // /_/ / +// \____/\____/____/_/_/ /_/ /_/___/\____/ +// Kratos CoSimulationApplication +// +// License: BSD License, see license.txt +// +// Main authors: Philipp Bucher (https://github.com/philbucher) +// + +// Project includes +#include "co_sim_io.hpp" +DEFINE_SINGLETON_MAIN( ); +#include "ext_lib_1.hpp" + +std::string ConnectToCoSimIO() +{ + CoSimIO::Info settings; + settings.Set("my_name", "singleton_tester"); + settings.Set("connect_to", "singleton_tester_partner"); + settings.Set("echo_level", 1); + settings.Set("version", "1.25"); + + auto info = CoSimIO::Connect(settings); + return info.Get("connection_name"); +} + +void DisconnectFromCoSimIO(const std::string& rConnectionName) +{ + CoSimIO::Info disconnect_settings; + disconnect_settings.Set("connection_name", rConnectionName); + CoSimIO::Disconnect(disconnect_settings); +} \ No newline at end of file diff --git a/tests/co_sim_io/impl/singleton_tests/scenario_4/ext_lib_1.hpp b/tests/co_sim_io/impl/singleton_tests/scenario_4/ext_lib_1.hpp new file mode 100644 index 00000000..2ec1c264 --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/scenario_4/ext_lib_1.hpp @@ -0,0 +1,18 @@ +// ______ _____ _ ________ +// / ____/___ / ___/(_)___ ___ / _/ __ | +// / / / __ \\__ \/ / __ `__ \ / // / / / +// / /___/ /_/ /__/ / / / / / / // // /_/ / +// \____/\____/____/_/_/ /_/ /_/___/\____/ +// Kratos CoSimulationApplication +// +// License: BSD License, see license.txt +// +// Main authors: Philipp Bucher (https://github.com/philbucher) +// + +// System includes +#include + +std::string ConnectToCoSimIO(); + +void DisconnectFromCoSimIO(const std::string& rConnectionName); diff --git a/tests/co_sim_io/impl/singleton_tests/scenario_4/ext_lib_2.cpp b/tests/co_sim_io/impl/singleton_tests/scenario_4/ext_lib_2.cpp new file mode 100644 index 00000000..c0169725 --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/scenario_4/ext_lib_2.cpp @@ -0,0 +1,20 @@ +// ______ _____ _ ________ +// / ____/___ / ___/(_)___ ___ / _/ __ | +// / / / __ \\__ \/ / __ `__ \ / // / / / +// / /___/ /_/ /__/ / / / / / / // // /_/ / +// \____/\____/____/_/_/ /_/ /_/___/\____/ +// Kratos CoSimulationApplication +// +// License: BSD License, see license.txt +// +// Main authors: Philipp Bucher (https://github.com/philbucher) +// + +// Project includes +#include "co_sim_io.hpp" +#include "ext_lib_2.hpp" + +bool ExtLib2HasIO(const std::string& rConnectionName) +{ + return CoSimIO::Internals::HasIO(rConnectionName); +} diff --git a/tests/co_sim_io/impl/singleton_tests/scenario_4/ext_lib_2.hpp b/tests/co_sim_io/impl/singleton_tests/scenario_4/ext_lib_2.hpp new file mode 100644 index 00000000..9eb89cc1 --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/scenario_4/ext_lib_2.hpp @@ -0,0 +1,16 @@ +// ______ _____ _ ________ +// / ____/___ / ___/(_)___ ___ / _/ __ | +// / / / __ \\__ \/ / __ `__ \ / // / / / +// / /___/ /_/ /__/ / / / / / / // // /_/ / +// \____/\____/____/_/_/ /_/ /_/___/\____/ +// Kratos CoSimulationApplication +// +// License: BSD License, see license.txt +// +// Main authors: Philipp Bucher (https://github.com/philbucher) +// + +// System includes +#include + +bool ExtLib2HasIO(const std::string& rConnectionName); diff --git a/tests/co_sim_io/impl/singleton_tests/scenario_4/main.cpp b/tests/co_sim_io/impl/singleton_tests/scenario_4/main.cpp new file mode 100644 index 00000000..ea39b5c3 --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/scenario_4/main.cpp @@ -0,0 +1,43 @@ +// ______ _____ _ ________ +// / ____/___ / ___/(_)___ ___ / _/ __ | +// / / / __ \\__ \/ / __ `__ \ / // / / / +// / /___/ /_/ /__/ / / / / / / // // /_/ / +// \____/\____/____/_/_/ /_/ /_/___/\____/ +// Kratos CoSimulationApplication +// +// License: BSD License, see license.txt +// +// Main authors: Philipp Bucher (https://github.com/philbucher) +// + +/* +Testing if the CoSimIO can be safely included in complex libraries +In this scenario the CoSimIO is included in the main executable +and in the external library +Connecting and disconnecting is done in the external library 1 +The it is checked if the connection is also available in library 2 +*/ + +// CoSimulation includes +#include "co_sim_io.hpp" +#include "ext_lib_1.hpp" +#include "ext_lib_2.hpp" + +#define COSIMIO_CHECK_True(a) \ + if (!a) { \ + std::cout << "in line " << __LINE__ << " : " \ + << "false is not true" << std::endl; \ + return 1; \ + } + +int main() +{ + const std::string connection_name = ConnectToCoSimIO(); + + COSIMIO_CHECK_True(CoSimIO::Internals::HasIO(connection_name)); + COSIMIO_CHECK_True(ExtLib2HasIO(connection_name)); + + DisconnectFromCoSimIO(connection_name); + + return 0; +} \ No newline at end of file diff --git a/tests/co_sim_io/impl/singleton_tests/scenario_5/CMakeLists.txt b/tests/co_sim_io/impl/singleton_tests/scenario_5/CMakeLists.txt new file mode 100644 index 00000000..a08e7233 --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/scenario_5/CMakeLists.txt @@ -0,0 +1,18 @@ +include_directories( ${CMAKE_SOURCE_DIR}/co_sim_io ) + +add_library(singleton_tests_scenario_5_ext_lib_1 SHARED ext_lib_1.cpp ) +install( TARGETS singleton_tests_scenario_5_ext_lib_1 DESTINATION ${CMAKE_SOURCE_DIR}/bin ) + +add_library(singleton_tests_scenario_5_ext_lib_2 SHARED ext_lib_2.cpp ) +install( TARGETS singleton_tests_scenario_5_ext_lib_2 DESTINATION ${CMAKE_SOURCE_DIR}/bin ) + +add_executable ( main_scenario_5 main.cpp ) +target_link_libraries( main_scenario_5 singleton_tests_scenario_5_ext_lib_1 singleton_tests_scenario_5_ext_lib_2 ) +install( TARGETS main_scenario_5 DESTINATION ${CMAKE_SOURCE_DIR}/bin/singleton_tests/scenario_5/ ) +set_target_properties( main_scenario_5 PROPERTIES + CXX_STANDARD 11 + CXX_STANDARD_REQUIRED YES + CXX_EXTENSIONS NO +) + +add_test(NAME singleton_tests_scenario_5 COMMAND sh ../../../../run.sh $ $) diff --git a/tests/co_sim_io/impl/singleton_tests/scenario_5/ext_lib_1.cpp b/tests/co_sim_io/impl/singleton_tests/scenario_5/ext_lib_1.cpp new file mode 100644 index 00000000..29b4b0f4 --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/scenario_5/ext_lib_1.cpp @@ -0,0 +1,35 @@ +// ______ _____ _ ________ +// / ____/___ / ___/(_)___ ___ / _/ __ | +// / / / __ \\__ \/ / __ `__ \ / // / / / +// / /___/ /_/ /__/ / / / / / / // // /_/ / +// \____/\____/____/_/_/ /_/ /_/___/\____/ +// Kratos CoSimulationApplication +// +// License: BSD License, see license.txt +// +// Main authors: Philipp Bucher (https://github.com/philbucher) +// + +// Project includes +#include "co_sim_io.hpp" +DEFINE_SINGLETON_MAIN( ); +#include "ext_lib_1.hpp" + +std::string ConnectToCoSimIO() +{ + CoSimIO::Info settings; + settings.Set("my_name", "singleton_tester"); + settings.Set("connect_to", "singleton_tester_partner"); + settings.Set("echo_level", 1); + settings.Set("version", "1.25"); + + auto info = CoSimIO::Connect(settings); + return info.Get("connection_name"); +} + +void DisconnectFromCoSimIO(const std::string& rConnectionName) +{ + CoSimIO::Info disconnect_settings; + disconnect_settings.Set("connection_name", rConnectionName); + CoSimIO::Disconnect(disconnect_settings); +} \ No newline at end of file diff --git a/tests/co_sim_io/impl/singleton_tests/scenario_5/ext_lib_1.hpp b/tests/co_sim_io/impl/singleton_tests/scenario_5/ext_lib_1.hpp new file mode 100644 index 00000000..2ec1c264 --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/scenario_5/ext_lib_1.hpp @@ -0,0 +1,18 @@ +// ______ _____ _ ________ +// / ____/___ / ___/(_)___ ___ / _/ __ | +// / / / __ \\__ \/ / __ `__ \ / // / / / +// / /___/ /_/ /__/ / / / / / / // // /_/ / +// \____/\____/____/_/_/ /_/ /_/___/\____/ +// Kratos CoSimulationApplication +// +// License: BSD License, see license.txt +// +// Main authors: Philipp Bucher (https://github.com/philbucher) +// + +// System includes +#include + +std::string ConnectToCoSimIO(); + +void DisconnectFromCoSimIO(const std::string& rConnectionName); diff --git a/tests/co_sim_io/impl/singleton_tests/scenario_5/ext_lib_2.cpp b/tests/co_sim_io/impl/singleton_tests/scenario_5/ext_lib_2.cpp new file mode 100644 index 00000000..c0169725 --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/scenario_5/ext_lib_2.cpp @@ -0,0 +1,20 @@ +// ______ _____ _ ________ +// / ____/___ / ___/(_)___ ___ / _/ __ | +// / / / __ \\__ \/ / __ `__ \ / // / / / +// / /___/ /_/ /__/ / / / / / / // // /_/ / +// \____/\____/____/_/_/ /_/ /_/___/\____/ +// Kratos CoSimulationApplication +// +// License: BSD License, see license.txt +// +// Main authors: Philipp Bucher (https://github.com/philbucher) +// + +// Project includes +#include "co_sim_io.hpp" +#include "ext_lib_2.hpp" + +bool ExtLib2HasIO(const std::string& rConnectionName) +{ + return CoSimIO::Internals::HasIO(rConnectionName); +} diff --git a/tests/co_sim_io/impl/singleton_tests/scenario_5/ext_lib_2.hpp b/tests/co_sim_io/impl/singleton_tests/scenario_5/ext_lib_2.hpp new file mode 100644 index 00000000..9eb89cc1 --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/scenario_5/ext_lib_2.hpp @@ -0,0 +1,16 @@ +// ______ _____ _ ________ +// / ____/___ / ___/(_)___ ___ / _/ __ | +// / / / __ \\__ \/ / __ `__ \ / // / / / +// / /___/ /_/ /__/ / / / / / / // // /_/ / +// \____/\____/____/_/_/ /_/ /_/___/\____/ +// Kratos CoSimulationApplication +// +// License: BSD License, see license.txt +// +// Main authors: Philipp Bucher (https://github.com/philbucher) +// + +// System includes +#include + +bool ExtLib2HasIO(const std::string& rConnectionName); diff --git a/tests/co_sim_io/impl/singleton_tests/scenario_5/main.cpp b/tests/co_sim_io/impl/singleton_tests/scenario_5/main.cpp new file mode 100644 index 00000000..d0a7edfe --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/scenario_5/main.cpp @@ -0,0 +1,45 @@ +// ______ _____ _ ________ +// / ____/___ / ___/(_)___ ___ / _/ __ | +// / / / __ \\__ \/ / __ `__ \ / // / / / +// / /___/ /_/ /__/ / / / / / / // // /_/ / +// \____/\____/____/_/_/ /_/ /_/___/\____/ +// Kratos CoSimulationApplication +// +// License: BSD License, see license.txt +// +// Main authors: Philipp Bucher (https://github.com/philbucher) +// + +/* +Testing if the CoSimIO can be safely included in complex libraries +In this scenario the CoSimIO is included in the main executable +and in the external library +Connecting and disconnecting is done in the external library 1 +The it is checked if the connection is also available in library 2 +Difference to scenario 4 is that the main doesn't include CoSimIO +*/ + +// System includes +#include + +// CoSimulation includes +#include "ext_lib_1.hpp" +#include "ext_lib_2.hpp" + +#define COSIMIO_CHECK_True(a) \ + if (!a) { \ + std::cout << "in line " << __LINE__ << " : " \ + << "false is not true" << std::endl; \ + return 1; \ + } + +int main() +{ + const std::string connection_name = ConnectToCoSimIO(); + + COSIMIO_CHECK_True(ExtLib2HasIO(connection_name)); + + DisconnectFromCoSimIO(connection_name); + + return 0; +} \ No newline at end of file diff --git a/tests/co_sim_io/impl/singleton_tests/scenario_6/CMakeLists.txt b/tests/co_sim_io/impl/singleton_tests/scenario_6/CMakeLists.txt new file mode 100644 index 00000000..762e1f4f --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/scenario_6/CMakeLists.txt @@ -0,0 +1,18 @@ +include_directories( ${CMAKE_SOURCE_DIR}/co_sim_io ) + +add_library(singleton_tests_scenario_6_ext_lib_1 SHARED ext_lib_11.cpp ext_lib_12.cpp ext_lib_13.cpp ) +install( TARGETS singleton_tests_scenario_6_ext_lib_1 DESTINATION ${CMAKE_SOURCE_DIR}/bin ) + +add_library(singleton_tests_scenario_6_ext_lib_2 SHARED ext_lib_2.cpp ) +install( TARGETS singleton_tests_scenario_6_ext_lib_2 DESTINATION ${CMAKE_SOURCE_DIR}/bin ) + +add_executable ( main_scenario_6 main.cpp ) +target_link_libraries( main_scenario_6 singleton_tests_scenario_6_ext_lib_1 singleton_tests_scenario_6_ext_lib_2 ) +install( TARGETS main_scenario_6 DESTINATION ${CMAKE_SOURCE_DIR}/bin/singleton_tests/scenario_6/ ) +set_target_properties( main_scenario_6 PROPERTIES + CXX_STANDARD 11 + CXX_STANDARD_REQUIRED YES + CXX_EXTENSIONS NO +) + +add_test(NAME singleton_tests_scenario_6 COMMAND sh ../../../../run.sh $ $) diff --git a/tests/co_sim_io/impl/singleton_tests/scenario_6/ext_lib_1.hpp b/tests/co_sim_io/impl/singleton_tests/scenario_6/ext_lib_1.hpp new file mode 100644 index 00000000..d9af0acf --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/scenario_6/ext_lib_1.hpp @@ -0,0 +1,20 @@ +// ______ _____ _ ________ +// / ____/___ / ___/(_)___ ___ / _/ __ | +// / / / __ \\__ \/ / __ `__ \ / // / / / +// / /___/ /_/ /__/ / / / / / / // // /_/ / +// \____/\____/____/_/_/ /_/ /_/___/\____/ +// Kratos CoSimulationApplication +// +// License: BSD License, see license.txt +// +// Main authors: Philipp Bucher (https://github.com/philbucher) +// + +// System includes +#include + +std::string ConnectToCoSimIO(); + +void DisconnectFromCoSimIO(const std::string& rConnectionName); + +bool ExtLib1HasIO(const std::string& rConnectionName); diff --git a/tests/co_sim_io/impl/singleton_tests/scenario_6/ext_lib_11.cpp b/tests/co_sim_io/impl/singleton_tests/scenario_6/ext_lib_11.cpp new file mode 100644 index 00000000..02f65418 --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/scenario_6/ext_lib_11.cpp @@ -0,0 +1,34 @@ +// ______ _____ _ ________ +// / ____/___ / ___/(_)___ ___ / _/ __ | +// / / / __ \\__ \/ / __ `__ \ / // / / / +// / /___/ /_/ /__/ / / / / / / // // /_/ / +// \____/\____/____/_/_/ /_/ /_/___/\____/ +// Kratos CoSimulationApplication +// +// License: BSD License, see license.txt +// +// Main authors: Philipp Bucher (https://github.com/philbucher) +// + +// Project includes +#include "co_sim_io.hpp" +#include "ext_lib_1.hpp" + +std::string ConnectToCoSimIO() +{ + CoSimIO::Info settings; + settings.Set("my_name", "singleton_tester"); + settings.Set("connect_to", "singleton_tester_partner"); + settings.Set("echo_level", 1); + settings.Set("version", "1.25"); + + auto info = CoSimIO::Connect(settings); + return info.Get("connection_name"); +} + +void DisconnectFromCoSimIO(const std::string& rConnectionName) +{ + CoSimIO::Info disconnect_settings; + disconnect_settings.Set("connection_name", rConnectionName); + CoSimIO::Disconnect(disconnect_settings); +} \ No newline at end of file diff --git a/tests/co_sim_io/impl/singleton_tests/scenario_6/ext_lib_12.cpp b/tests/co_sim_io/impl/singleton_tests/scenario_6/ext_lib_12.cpp new file mode 100644 index 00000000..d13ee2e2 --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/scenario_6/ext_lib_12.cpp @@ -0,0 +1,20 @@ +// ______ _____ _ ________ +// / ____/___ / ___/(_)___ ___ / _/ __ | +// / / / __ \\__ \/ / __ `__ \ / // / / / +// / /___/ /_/ /__/ / / / / / / // // /_/ / +// \____/\____/____/_/_/ /_/ /_/___/\____/ +// Kratos CoSimulationApplication +// +// License: BSD License, see license.txt +// +// Main authors: Philipp Bucher (https://github.com/philbucher) +// + +// Project includes +#include "co_sim_io.hpp" +#include "ext_lib_1.hpp" + +bool ExtLib1HasIO(const std::string& rConnectionName) +{ + return CoSimIO::Internals::HasIO(rConnectionName); +} diff --git a/tests/co_sim_io/impl/singleton_tests/scenario_6/ext_lib_13.cpp b/tests/co_sim_io/impl/singleton_tests/scenario_6/ext_lib_13.cpp new file mode 100644 index 00000000..207495c5 --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/scenario_6/ext_lib_13.cpp @@ -0,0 +1,20 @@ +// ______ _____ _ ________ +// / ____/___ / ___/(_)___ ___ / _/ __ | +// / / / __ \\__ \/ / __ `__ \ / // / / / +// / /___/ /_/ /__/ / / / / / / // // /_/ / +// \____/\____/____/_/_/ /_/ /_/___/\____/ +// Kratos CoSimulationApplication +// +// License: BSD License, see license.txt +// +// Main authors: Philipp Bucher (https://github.com/philbucher) +// + +// Project includes +#include "co_sim_io.hpp" +#include "ext_lib_13.hpp" + +bool ExtLib13HasIO(const std::string& rConnectionName) +{ + return CoSimIO::Internals::HasIO(rConnectionName); +} diff --git a/tests/co_sim_io/impl/singleton_tests/scenario_6/ext_lib_13.hpp b/tests/co_sim_io/impl/singleton_tests/scenario_6/ext_lib_13.hpp new file mode 100644 index 00000000..dabcf604 --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/scenario_6/ext_lib_13.hpp @@ -0,0 +1,16 @@ +// ______ _____ _ ________ +// / ____/___ / ___/(_)___ ___ / _/ __ | +// / / / __ \\__ \/ / __ `__ \ / // / / / +// / /___/ /_/ /__/ / / / / / / // // /_/ / +// \____/\____/____/_/_/ /_/ /_/___/\____/ +// Kratos CoSimulationApplication +// +// License: BSD License, see license.txt +// +// Main authors: Philipp Bucher (https://github.com/philbucher) +// + +// System includes +#include + +bool ExtLib13HasIO(const std::string& rConnectionName); diff --git a/tests/co_sim_io/impl/singleton_tests/scenario_6/ext_lib_2.cpp b/tests/co_sim_io/impl/singleton_tests/scenario_6/ext_lib_2.cpp new file mode 100644 index 00000000..59d8e07f --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/scenario_6/ext_lib_2.cpp @@ -0,0 +1,21 @@ +// ______ _____ _ ________ +// / ____/___ / ___/(_)___ ___ / _/ __ | +// / / / __ \\__ \/ / __ `__ \ / // / / / +// / /___/ /_/ /__/ / / / / / / // // /_/ / +// \____/\____/____/_/_/ /_/ /_/___/\____/ +// Kratos CoSimulationApplication +// +// License: BSD License, see license.txt +// +// Main authors: Philipp Bucher (https://github.com/philbucher) +// + +// Project includes +#include "co_sim_io.hpp" +DEFINE_SINGLETON_MAIN( ); +#include "ext_lib_2.hpp" + +bool ExtLib2HasIO(const std::string& rConnectionName) +{ + return CoSimIO::Internals::HasIO(rConnectionName); +} diff --git a/tests/co_sim_io/impl/singleton_tests/scenario_6/ext_lib_2.hpp b/tests/co_sim_io/impl/singleton_tests/scenario_6/ext_lib_2.hpp new file mode 100644 index 00000000..9eb89cc1 --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/scenario_6/ext_lib_2.hpp @@ -0,0 +1,16 @@ +// ______ _____ _ ________ +// / ____/___ / ___/(_)___ ___ / _/ __ | +// / / / __ \\__ \/ / __ `__ \ / // / / / +// / /___/ /_/ /__/ / / / / / / // // /_/ / +// \____/\____/____/_/_/ /_/ /_/___/\____/ +// Kratos CoSimulationApplication +// +// License: BSD License, see license.txt +// +// Main authors: Philipp Bucher (https://github.com/philbucher) +// + +// System includes +#include + +bool ExtLib2HasIO(const std::string& rConnectionName); diff --git a/tests/co_sim_io/impl/singleton_tests/scenario_6/main.cpp b/tests/co_sim_io/impl/singleton_tests/scenario_6/main.cpp new file mode 100644 index 00000000..ce5079b7 --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/scenario_6/main.cpp @@ -0,0 +1,46 @@ +// ______ _____ _ ________ +// / ____/___ / ___/(_)___ ___ / _/ __ | +// / / / __ \\__ \/ / __ `__ \ / // / / / +// / /___/ /_/ /__/ / / / / / / // // /_/ / +// \____/\____/____/_/_/ /_/ /_/___/\____/ +// Kratos CoSimulationApplication +// +// License: BSD License, see license.txt +// +// Main authors: Philipp Bucher (https://github.com/philbucher) +// + +/* +Testing if the CoSimIO can be safely included in complex libraries +In this scenario the CoSimIO is included in the main executable +and in the external library +This is a more elaborate example with a more complex structure +*/ + +// System includes +#include + +// CoSimulation includes +#include "ext_lib_1.hpp" +#include "ext_lib_13.hpp" +#include "ext_lib_2.hpp" + +#define COSIMIO_CHECK_True(a) \ + if (!a) { \ + std::cout << "in line " << __LINE__ << " : " \ + << "false is not true" << std::endl; \ + return 1; \ + } + +int main() +{ + const std::string connection_name = ConnectToCoSimIO(); + + COSIMIO_CHECK_True(ExtLib1HasIO(connection_name)); + COSIMIO_CHECK_True(ExtLib13HasIO(connection_name)); + COSIMIO_CHECK_True(ExtLib2HasIO(connection_name)); + + DisconnectFromCoSimIO(connection_name); + + return 0; +} \ No newline at end of file diff --git a/tests/co_sim_io/impl/singleton_tests/source/CMakeLists.txt b/tests/co_sim_io/impl/singleton_tests/source/CMakeLists.txt new file mode 100644 index 00000000..8b00449b --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/source/CMakeLists.txt @@ -0,0 +1,18 @@ + +cmake_minimum_required( VERSION 3.12 ) + +project( test LANGUAGES CXX ) + +set( WINDOWS_EXPORT_ALL_SYMBOLS ON ) + +add_library( library1 lib1/lib1source1.cpp lib1/lib1source2.cpp lib1/lib1header.hpp ) +add_library( library2 lib2/lib2source1.cpp lib2/lib2source2.cpp lib2/lib2header.hpp ) + +target_include_directories( library1 PUBLIC . lib1 ) +target_include_directories( library2 PUBLIC . lib2 ) + +add_executable( exe main.cpp ) + +target_link_libraries( exe library1 library2 ) + +add_test(singleton_philipp exe) diff --git a/tests/co_sim_io/impl/singleton_tests/source/lib1/lib1header.hpp b/tests/co_sim_io/impl/singleton_tests/source/lib1/lib1header.hpp new file mode 100644 index 00000000..6da90601 --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/source/lib1/lib1header.hpp @@ -0,0 +1,2 @@ +void lib1function1( ); +void lib1function2( ); \ No newline at end of file diff --git a/tests/co_sim_io/impl/singleton_tests/source/lib1/lib1source1.cpp b/tests/co_sim_io/impl/singleton_tests/source/lib1/lib1source1.cpp new file mode 100644 index 00000000..5b300717 --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/source/lib1/lib1source1.cpp @@ -0,0 +1,9 @@ +#include "lib1header.hpp" +#include "singleheader.hpp" + +#include + +void lib1function2( ) +{ + singleton.increment( ); +} \ No newline at end of file diff --git a/tests/co_sim_io/impl/singleton_tests/source/lib1/lib1source2.cpp b/tests/co_sim_io/impl/singleton_tests/source/lib1/lib1source2.cpp new file mode 100644 index 00000000..f8e8c44a --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/source/lib1/lib1source2.cpp @@ -0,0 +1,7 @@ +#include "lib1header.hpp" +#include "singleheader.hpp" + +void lib1function1( ) +{ + singleton.increment( ); +} \ No newline at end of file diff --git a/tests/co_sim_io/impl/singleton_tests/source/lib2/lib2header.hpp b/tests/co_sim_io/impl/singleton_tests/source/lib2/lib2header.hpp new file mode 100644 index 00000000..00fd131b --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/source/lib2/lib2header.hpp @@ -0,0 +1,2 @@ +void lib2function1( ); +void lib2function2( ); \ No newline at end of file diff --git a/tests/co_sim_io/impl/singleton_tests/source/lib2/lib2source1.cpp b/tests/co_sim_io/impl/singleton_tests/source/lib2/lib2source1.cpp new file mode 100644 index 00000000..4a4feef7 --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/source/lib2/lib2source1.cpp @@ -0,0 +1,9 @@ +#include "lib2header.hpp" +#include "singleheader.hpp" + +#include + +void lib2function2( ) +{ + singleton.increment( ); +} \ No newline at end of file diff --git a/tests/co_sim_io/impl/singleton_tests/source/lib2/lib2source2.cpp b/tests/co_sim_io/impl/singleton_tests/source/lib2/lib2source2.cpp new file mode 100644 index 00000000..8829c945 --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/source/lib2/lib2source2.cpp @@ -0,0 +1,7 @@ +#include "lib2header.hpp" +#include "singleheader.hpp" + +void lib2function1( ) +{ + singleton.increment( ); +} \ No newline at end of file diff --git a/tests/co_sim_io/impl/singleton_tests/source/main.cpp b/tests/co_sim_io/impl/singleton_tests/source/main.cpp new file mode 100644 index 00000000..bb054c49 --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/source/main.cpp @@ -0,0 +1,30 @@ +#include +#include "lib1header.hpp" +#include "lib2header.hpp" + +#include "singleheader.hpp" + +DEFINE_SINGLETON_MAIN( ); + +int main( ) +{ + lib1function1( ); + lib2function1( ); + + singleton.increment( ); + + lib1function2( ); + lib2function2( ); + + lib1function1( ); + lib2function1( ); + + singleton.increment( ); + + lib1function2( ); + lib2function2( ); + + std::cout << "Final count: " << singleton.count( ) << std::endl; + + return singleton.count( ) != 10; +} \ No newline at end of file diff --git a/tests/co_sim_io/impl/singleton_tests/source/singleheader.hpp b/tests/co_sim_io/impl/singleton_tests/source/singleheader.hpp new file mode 100644 index 00000000..5e9cbaf8 --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/source/singleheader.hpp @@ -0,0 +1,22 @@ +class Singleton +{ + int count_; + +public: + Singleton( ) : count_( 0 ) { } + + void increment( ) + { + count_++; + } + + int count( ) + { + return count_; + } +}; + +extern Singleton singleton; + +#define DEFINE_SINGLETON_MAIN( ) Singleton singleton + diff --git a/tests/co_sim_io/impl/singleton_tests/source_cosimio/CMakeLists.txt b/tests/co_sim_io/impl/singleton_tests/source_cosimio/CMakeLists.txt new file mode 100644 index 00000000..8b00449b --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/source_cosimio/CMakeLists.txt @@ -0,0 +1,18 @@ + +cmake_minimum_required( VERSION 3.12 ) + +project( test LANGUAGES CXX ) + +set( WINDOWS_EXPORT_ALL_SYMBOLS ON ) + +add_library( library1 lib1/lib1source1.cpp lib1/lib1source2.cpp lib1/lib1header.hpp ) +add_library( library2 lib2/lib2source1.cpp lib2/lib2source2.cpp lib2/lib2header.hpp ) + +target_include_directories( library1 PUBLIC . lib1 ) +target_include_directories( library2 PUBLIC . lib2 ) + +add_executable( exe main.cpp ) + +target_link_libraries( exe library1 library2 ) + +add_test(singleton_philipp exe) diff --git a/tests/co_sim_io/impl/singleton_tests/source_cosimio/lib1/lib1header.hpp b/tests/co_sim_io/impl/singleton_tests/source_cosimio/lib1/lib1header.hpp new file mode 100644 index 00000000..6da90601 --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/source_cosimio/lib1/lib1header.hpp @@ -0,0 +1,2 @@ +void lib1function1( ); +void lib1function2( ); \ No newline at end of file diff --git a/tests/co_sim_io/impl/singleton_tests/source_cosimio/lib1/lib1source1.cpp b/tests/co_sim_io/impl/singleton_tests/source_cosimio/lib1/lib1source1.cpp new file mode 100644 index 00000000..ebcaddeb --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/source_cosimio/lib1/lib1source1.cpp @@ -0,0 +1,10 @@ +#include "lib1header.hpp" +#include "co_sim_io.hpp" + +#include + +void lib1function2( ) +{ + CoSimIO::Info info; + CoSimIO::Connect(info); +} \ No newline at end of file diff --git a/tests/co_sim_io/impl/singleton_tests/source_cosimio/lib1/lib1source2.cpp b/tests/co_sim_io/impl/singleton_tests/source_cosimio/lib1/lib1source2.cpp new file mode 100644 index 00000000..0c3bb0ad --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/source_cosimio/lib1/lib1source2.cpp @@ -0,0 +1,8 @@ +#include "lib1header.hpp" +#include "co_sim_io.hpp" + +void lib1function1( ) +{ + CoSimIO::Info info; + CoSimIO::Connect(info); +} \ No newline at end of file diff --git a/tests/co_sim_io/impl/singleton_tests/source_cosimio/lib2/lib2header.hpp b/tests/co_sim_io/impl/singleton_tests/source_cosimio/lib2/lib2header.hpp new file mode 100644 index 00000000..00fd131b --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/source_cosimio/lib2/lib2header.hpp @@ -0,0 +1,2 @@ +void lib2function1( ); +void lib2function2( ); \ No newline at end of file diff --git a/tests/co_sim_io/impl/singleton_tests/source_cosimio/lib2/lib2source1.cpp b/tests/co_sim_io/impl/singleton_tests/source_cosimio/lib2/lib2source1.cpp new file mode 100644 index 00000000..695e26a6 --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/source_cosimio/lib2/lib2source1.cpp @@ -0,0 +1,10 @@ +#include "lib2header.hpp" +#include "co_sim_io.hpp" + +#include + +void lib2function2( ) +{ + CoSimIO::Info info; + CoSimIO::Connect(info); +} \ No newline at end of file diff --git a/tests/co_sim_io/impl/singleton_tests/source_cosimio/lib2/lib2source2.cpp b/tests/co_sim_io/impl/singleton_tests/source_cosimio/lib2/lib2source2.cpp new file mode 100644 index 00000000..37eaf2e8 --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/source_cosimio/lib2/lib2source2.cpp @@ -0,0 +1,8 @@ +#include "lib2header.hpp" +#include "co_sim_io.hpp" + +void lib2function1( ) +{ + CoSimIO::Info info; + CoSimIO::Connect(info); +} \ No newline at end of file diff --git a/tests/co_sim_io/impl/singleton_tests/source_cosimio/main.cpp b/tests/co_sim_io/impl/singleton_tests/source_cosimio/main.cpp new file mode 100644 index 00000000..131d58b5 --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/source_cosimio/main.cpp @@ -0,0 +1,32 @@ +#include +#include "lib1header.hpp" +#include "lib2header.hpp" + +#include "co_sim_io.hpp" + +DEFINE_SINGLETON_MAIN( ); + +int main( ) +{ + CoSimIO::Info info; + CoSimIO::Connect(info); + // lib1function1( ); + // lib2function1( ); + + // singleton.increment( ); + + // lib1function2( ); + // lib2function2( ); + + // lib1function1( ); + // lib2function1( ); + + // singleton.increment( ); + + // lib1function2( ); + // lib2function2( ); + + // std::cout << "Final count: " << singleton.count( ) << std::endl; + + // return singleton.count( ) != 10; +} \ No newline at end of file diff --git a/tests/co_sim_io/impl/singleton_tests/source_cosimio/singleheader.hpp b/tests/co_sim_io/impl/singleton_tests/source_cosimio/singleheader.hpp new file mode 100644 index 00000000..5e9cbaf8 --- /dev/null +++ b/tests/co_sim_io/impl/singleton_tests/source_cosimio/singleheader.hpp @@ -0,0 +1,22 @@ +class Singleton +{ + int count_; + +public: + Singleton( ) : count_( 0 ) { } + + void increment( ) + { + count_++; + } + + int count( ) + { + return count_; + } +}; + +extern Singleton singleton; + +#define DEFINE_SINGLETON_MAIN( ) Singleton singleton + diff --git a/tests/co_sim_io/impl/test_include_co_sim_io_1.cpp b/tests/co_sim_io/impl/test_include_co_sim_io_1.cpp index 987a51ef..3d5b516a 100644 --- a/tests/co_sim_io/impl/test_include_co_sim_io_1.cpp +++ b/tests/co_sim_io/impl/test_include_co_sim_io_1.cpp @@ -17,3 +17,4 @@ the "other" file that includes CoSimIO is "test_include_co_sim_io_2.cpp" // Project includes #include "co_sim_io.hpp" +DEFINE_SINGLETON_MAIN( ); diff --git a/tests/integration_tutorials/cpp/connect_disconnect_a.cpp b/tests/integration_tutorials/cpp/connect_disconnect_a.cpp index e29b88ea..24449e62 100644 --- a/tests/integration_tutorials/cpp/connect_disconnect_a.cpp +++ b/tests/integration_tutorials/cpp/connect_disconnect_a.cpp @@ -12,6 +12,7 @@ // CoSimulation includes #include "co_sim_io.hpp" +DEFINE_SINGLETON_MAIN( ); #define COSIMIO_CHECK_EQUAL(a, b) \ if (a != b) { \ diff --git a/tests/integration_tutorials/cpp/connect_disconnect_b.cpp b/tests/integration_tutorials/cpp/connect_disconnect_b.cpp index 4596b41d..555f8562 100644 --- a/tests/integration_tutorials/cpp/connect_disconnect_b.cpp +++ b/tests/integration_tutorials/cpp/connect_disconnect_b.cpp @@ -12,6 +12,7 @@ // CoSimulation includes #include "co_sim_io.hpp" +DEFINE_SINGLETON_MAIN( ); #define COSIMIO_CHECK_EQUAL(a, b) \ if (a != b) { \ diff --git a/tests/integration_tutorials/cpp/data_exchange.cpp b/tests/integration_tutorials/cpp/data_exchange.cpp index e0f7360a..c843f49c 100644 --- a/tests/integration_tutorials/cpp/data_exchange.cpp +++ b/tests/integration_tutorials/cpp/data_exchange.cpp @@ -15,6 +15,7 @@ // CoSimulation includes #include "co_sim_io.hpp" +DEFINE_SINGLETON_MAIN( ); #define COSIMIO_CHECK_EQUAL(a, b) \ if (a != b) { \ diff --git a/tests/integration_tutorials/cpp/export_data.cpp b/tests/integration_tutorials/cpp/export_data.cpp index 442b500a..8dfb1e6d 100644 --- a/tests/integration_tutorials/cpp/export_data.cpp +++ b/tests/integration_tutorials/cpp/export_data.cpp @@ -12,6 +12,7 @@ // CoSimulation includes #include "co_sim_io.hpp" +DEFINE_SINGLETON_MAIN( ); #define COSIMIO_CHECK_EQUAL(a, b) \ if (a != b) { \ diff --git a/tests/integration_tutorials/cpp/export_info.cpp b/tests/integration_tutorials/cpp/export_info.cpp index 4e1f97ac..c0072a85 100644 --- a/tests/integration_tutorials/cpp/export_info.cpp +++ b/tests/integration_tutorials/cpp/export_info.cpp @@ -12,6 +12,7 @@ // CoSimulation includes #include "co_sim_io.hpp" +DEFINE_SINGLETON_MAIN( ); #define COSIMIO_CHECK_EQUAL(a, b) \ if (a != b) { \ diff --git a/tests/integration_tutorials/cpp/export_mesh.cpp b/tests/integration_tutorials/cpp/export_mesh.cpp index d683059d..27c82f0c 100644 --- a/tests/integration_tutorials/cpp/export_mesh.cpp +++ b/tests/integration_tutorials/cpp/export_mesh.cpp @@ -16,6 +16,7 @@ // Project includes #include "co_sim_io.hpp" +DEFINE_SINGLETON_MAIN( ); #define COSIMIO_CHECK_EQUAL(a, b) \ if (a != b) { \ diff --git a/tests/integration_tutorials/cpp/hello.cpp b/tests/integration_tutorials/cpp/hello.cpp index 39d091ec..89e4f894 100644 --- a/tests/integration_tutorials/cpp/hello.cpp +++ b/tests/integration_tutorials/cpp/hello.cpp @@ -12,6 +12,7 @@ // CoSimulation includes #include "co_sim_io.hpp" +DEFINE_SINGLETON_MAIN( ); #define COSIMIO_CHECK_EQUAL(a, b) \ if (a != b) { \ diff --git a/tests/integration_tutorials/cpp/import_data.cpp b/tests/integration_tutorials/cpp/import_data.cpp index be0e1159..1aeec29c 100644 --- a/tests/integration_tutorials/cpp/import_data.cpp +++ b/tests/integration_tutorials/cpp/import_data.cpp @@ -12,6 +12,7 @@ // CoSimulation includes #include "co_sim_io.hpp" +DEFINE_SINGLETON_MAIN( ); #define COSIMIO_CHECK_EQUAL(a, b) \ if (a != b) { \ diff --git a/tests/integration_tutorials/cpp/import_info.cpp b/tests/integration_tutorials/cpp/import_info.cpp index fe64abb8..785a6955 100644 --- a/tests/integration_tutorials/cpp/import_info.cpp +++ b/tests/integration_tutorials/cpp/import_info.cpp @@ -12,6 +12,7 @@ // CoSimulation includes #include "co_sim_io.hpp" +DEFINE_SINGLETON_MAIN( ); #define COSIMIO_CHECK_EQUAL(a, b) \ if (a != b) { \ diff --git a/tests/integration_tutorials/cpp/import_mesh.cpp b/tests/integration_tutorials/cpp/import_mesh.cpp index 213941a5..4a855f09 100644 --- a/tests/integration_tutorials/cpp/import_mesh.cpp +++ b/tests/integration_tutorials/cpp/import_mesh.cpp @@ -16,6 +16,7 @@ // Project includes #include "co_sim_io.hpp" +DEFINE_SINGLETON_MAIN( ); #define COSIMIO_CHECK_EQUAL(a, b) \ if (a != b) { \ diff --git a/tests/integration_tutorials/cpp/mesh_exchange.cpp b/tests/integration_tutorials/cpp/mesh_exchange.cpp index 7a5f1e7f..6c88f92c 100644 --- a/tests/integration_tutorials/cpp/mesh_exchange.cpp +++ b/tests/integration_tutorials/cpp/mesh_exchange.cpp @@ -15,6 +15,7 @@ // CoSimulation includes #include "co_sim_io.hpp" +DEFINE_SINGLETON_MAIN( ); #define COSIMIO_CHECK_EQUAL(a, b) \ if (a != b) { \ diff --git a/tests/integration_tutorials/cpp/mpi/connect_disconnect_mpi_a.cpp b/tests/integration_tutorials/cpp/mpi/connect_disconnect_mpi_a.cpp index 28971df3..b49c4cbe 100644 --- a/tests/integration_tutorials/cpp/mpi/connect_disconnect_mpi_a.cpp +++ b/tests/integration_tutorials/cpp/mpi/connect_disconnect_mpi_a.cpp @@ -15,6 +15,7 @@ // CoSimulation includes #include "co_sim_io_mpi.hpp" +DEFINE_SINGLETON_MAIN( ); #define COSIMIO_CHECK_EQUAL(a, b) \ if (a != b) { \ diff --git a/tests/integration_tutorials/cpp/mpi/connect_disconnect_mpi_b.cpp b/tests/integration_tutorials/cpp/mpi/connect_disconnect_mpi_b.cpp index ab8dba4e..c5823383 100644 --- a/tests/integration_tutorials/cpp/mpi/connect_disconnect_mpi_b.cpp +++ b/tests/integration_tutorials/cpp/mpi/connect_disconnect_mpi_b.cpp @@ -15,6 +15,7 @@ // CoSimulation includes #include "co_sim_io_mpi.hpp" +DEFINE_SINGLETON_MAIN( ); #define COSIMIO_CHECK_EQUAL(a, b) \ if (a != b) { \ diff --git a/tests/integration_tutorials/cpp/run.cpp b/tests/integration_tutorials/cpp/run.cpp index 22de9ecb..fb3706ff 100644 --- a/tests/integration_tutorials/cpp/run.cpp +++ b/tests/integration_tutorials/cpp/run.cpp @@ -12,6 +12,7 @@ // Project includes #include "co_sim_io.hpp" +DEFINE_SINGLETON_MAIN( ); #define COSIMIO_CHECK_EQUAL(a, b) \ if (a != b) { \ diff --git a/tests/integration_tutorials/cpp/runner.cpp b/tests/integration_tutorials/cpp/runner.cpp index fdfc7544..d182a66b 100644 --- a/tests/integration_tutorials/cpp/runner.cpp +++ b/tests/integration_tutorials/cpp/runner.cpp @@ -12,6 +12,7 @@ // Project includes #include "co_sim_io.hpp" +DEFINE_SINGLETON_MAIN( ); #define COSIMIO_CHECK_TRUE(a) \ if (!(a)) { \