Skip to content

Commit

Permalink
outsoruce std ostream operators in utils file
Browse files Browse the repository at this point in the history
  • Loading branch information
trittsv committed Feb 27, 2024
1 parent 8f65138 commit dc7cb10
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 58 deletions.
2 changes: 2 additions & 0 deletions examples/helloworld/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
project(helloworld LANGUAGES C CXX)
cmake_minimum_required(VERSION 3.16)

set(CMAKE_CXX_STANDARD 17)

if(NOT TARGET CycloneDDS-CXX::ddscxx)
find_package(CycloneDDS-CXX REQUIRED)
endif()
Expand Down
2 changes: 2 additions & 0 deletions examples/roundtrip/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
project(throughput LANGUAGES C CXX)
cmake_minimum_required(VERSION 3.16)

set(CMAKE_CXX_STANDARD 17)

if(NOT TARGET CycloneDDS-CXX::ddscxx)
find_package(CycloneDDS-CXX REQUIRED)
endif()
Expand Down
2 changes: 2 additions & 0 deletions examples/throughput/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
project(throughput LANGUAGES C CXX)
cmake_minimum_required(VERSION 3.16)

set(CMAKE_CXX_STANDARD 17)

if(NOT TARGET CycloneDDS-CXX::ddscxx)
find_package(CycloneDDS-CXX REQUIRED)
endif()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright(c) 2006 to 2020 ZettaScale Technology and others
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0, or the Eclipse Distribution License
// v. 1.0 which is available at
// http://www.eclipse.org/org/documents/edl-v10.php.
//
// SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause

/**
* @file
*/

#ifndef CYCLONEDDS_UTIL_OSTREAM_OPERATORS_HPP_
#define CYCLONEDDS_UTIL_OSTREAM_OPERATORS_HPP_

#include "dds/features.hpp"

#include <array>
#include <vector>
#if DDSCXX_USE_BOOST
#include <boost/optional.hpp>
#define DDSCXX_STD_IMPL boost
#else
#include <optional>
#define DDSCXX_STD_IMPL std
#endif


namespace std
{

template <typename T, size_t N>
std::ostream& operator<<(std::ostream& os, const std::array<T, N>& rhs)
{
os << "[";
for (std::size_t i = 0; i < N; i++)
{
os << rhs[i];
if (i < N - 1)
{
os << ", ";
}
}
os << "]";
return os;
}

template<typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& rhs)
{
os << "[";
for(size_t i=0; i<rhs.size(); i++)
{
if (i != 0)
{
os << ", ";
}
os << rhs[i];
}
os << "]";
return os;
}

template<typename T>
std::ostream& operator<<(std::ostream& os, const DDSCXX_STD_IMPL::optional<T>& rhs)
{
return rhs ? os << rhs.value() : os;
}

} //namespace std

#endif /* CYCLONEDDS_UTIL_OSTREAM_OPERATORS_HPP_ */
61 changes: 3 additions & 58 deletions src/idlcxx/src/generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -934,66 +934,11 @@ generate_includes(const idl_pstate_t *pstate, struct generator *generator)
if (fputs("\n", generator->header.handle) < 0)
return IDL_RETCODE_NO_MEMORY;

// ostream hpp
// ostream cpp
// streaming goperators for the used std types
// this could also be in a cyclone utils header instead
// generating in every file
const char *fmt;
fmt = "namespace std\n{\n";
if (fputs(fmt, generator->header.handle) < 0)
return IDL_RETCODE_NO_MEMORY;

if (generator->uses_array)
{
fmt = "template <typename T, size_t N>\n"
"std::ostream& operator<<(std::ostream& os, std::array<T, N> const& rhs)\n"
"{\n"
" os << \"[\";\n"
" for (std::size_t i = 0; i < N; i++)\n"
" {\n"
" os << rhs[i];\n"
" if (i < N - 1)\n"
" {\n"
" os << \", \";\n"
" }\n"
" }\n"
" os << \"]\";\n"
"return os;\n"
"}\n\n";
if (fputs(fmt, generator->header.handle) < 0)
return IDL_RETCODE_NO_MEMORY;
}

if (generator->uses_sequence || generator->uses_bounded_sequence)
{
fmt = "template<typename T>\n"
"std::ostream& operator<<(std::ostream& os, std::vector<T> const& rhs)\n{\n"
" os << \"[\";\n"
" for(size_t i=0; i<rhs.size(); i++)\n {\n"
" if (i != 0)\n"
" {\n"
" os << \", \";\n"
" }\n"
" os << rhs[i];\n"
" }\n"
" os << \"]\";\n"
" return os;\n"
"}\n\n";
if (fputs(fmt, generator->header.handle) < 0)
return IDL_RETCODE_NO_MEMORY;
}
if (generator->uses_optional)
{
fmt = "template<typename T>\n"
"std::ostream& operator<<(std::ostream& os, %s<T> const& rhs)\n{\n"
" return rhs ? os << rhs.value() : os;\n"
"}\n\n";
if (idl_fprintf(generator->header.handle, fmt, opt_tmpl) < 0)
return IDL_RETCODE_NO_MEMORY;
}

fmt = "} //namespace std\n\n";
if (fputs(fmt, generator->header.handle) < 0)
fmt = "#include <org/eclipse/cyclonedds/util/ostream_operators.hpp>\n\n";
if (fputs(fmt, generator->impl.handle) < 0)
return IDL_RETCODE_NO_MEMORY;

return IDL_RETCODE_OK;
Expand Down

0 comments on commit dc7cb10

Please sign in to comment.