Skip to content

Commit

Permalink
Merge pull request robotology#380 from drdanz/pack_macros
Browse files Browse the repository at this point in the history
Pack macros
  • Loading branch information
paulfitz committed Feb 8, 2015
2 parents 13d2409 + 1cca910 commit f327fa3
Show file tree
Hide file tree
Showing 17 changed files with 281 additions and 204 deletions.
4 changes: 4 additions & 0 deletions bindings/yarp.i
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,10 @@ namespace yarp {
%enddef
#endif

%define YARP_BEGIN_PACK
%enddef
%define YARP_END_PACK
%enddef
%define PACKED_FOR_NET
%enddef

Expand Down
193 changes: 191 additions & 2 deletions conf/template/yarp_config_system.h.in
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-

/*
* Copyright: (C) 2009 RobotCub Consortium
* Author: Paul Fitzpatrick
* Copyright: (C) 2006-2009 RobotCub Consortium
* (C) 2015 iCub Facility, Istituto Italiano di Tecnologia
* Authors: Paul Fitzpatrick <[email protected]>
* Giorgio Metta <[email protected]>
* Daniele E. Domenichelli <[email protected]>
* CopyPolicy: Released under the terms of the LGPLv2.1 or later, see LGPL.TXT
*/

Expand Down Expand Up @@ -34,4 +37,190 @@

#define YARP_POINTER_SIZE ${YARP_POINTER_SIZE}


///@{

/**
* \def YARP_COMPILER_MESSAGE
*
* Print a message at build time on supported compilers.
*
* For example:
* \code{.c}
* YARP_COMPILER_MESSAGE(This is a custom compiler message)
* \endcode
* will print "This is a custom compiler message".
*
* \param x Message.
*
* \see YARP_COMPILER_WARNING, YARP_COMPILER_ERROR, YARP_COMPILER_DEPRECATED_WARNING
*/

/**
* \def YARP_COMPILER_WARNING
*
* Generate a warning at build time on supported compilers.
*
* For example:
* \code{.c}
* YARP_COMPILER_WARNING(This is a custom compiler warning)
* \endcode
* will generate a warning "This is a custom compiler warning"
*
* \param x Warning message.
*
* \see YARP_COMPILER_MESSAGE, YARP_COMPILER_ERROR, YARP_COMPILER_DEPRECATED_WARNING
*
*/

/**
* \def YARP_COMPILER_ERROR
*
* Generate an error at build time on supported compilers.
*
* For example:
* \code{.c}
* YARP_COMPILER_ERROR(This is a custom compiler error)
* \endcode
* will generate an error "This is a custom compiler error".
*
* \param x Error message.
*
* \see YARP_COMPILER_MESSAGE, YARP_COMPILER_WARNING, YARP_COMPILER_DEPRECATED_WARNING
*/

/**
* \def YARP_COMPILER_DEPRECATED_WARNING
*
* Generate a warning at build time on supported compilers if deprecated
* warnings are enabled.
*
* This can be useful for example to deprecate a header file.
*
* \param x Deprecation message.
*
* \see YARP_COMPILER_MESSAGE, YARP_COMPILER_WARNING, YARP_COMPILER_ERROR
*/

///@}


#if defined(_MSC_VER)
// see https://support.microsoft.com/kb/155196/en-us
#define __YARP_STR2(x) #x
#define __YARP_STR1(x) __YARP_STR2(x)
#define __YARP_LOC __FILE__ "("__STR1__(__LINE__)")"
#define YARP_COMPILER_MESSAGE(x) __pragma(message(__LOC__ " : msg" #x)
#define YARP_COMPILER_WARNING(x) __pragma(message(__LOC__ " : warning msg" #x)
#define YARP_COMPILER_ERROR(x) __pragma(message(__LOC__ " : error msg" #x)
#elif defined(__GNUC__)
// see https://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html
#define __YARP_DO_PRAGMA(x) _Pragma (#x)
#define YARP_COMPILER_MESSAGE(x) __YARP_DO_PRAGMA(message #x)
#define YARP_COMPILER_WARNING(x) __YARP_DO_PRAGMA(GCC warning #x)
#define YARP_COMPILER_ERROR(x) __YARP_DO_PRAGMA(GCC error #x)
#else
#define YARP_COMPILER_MESSAGE(x)
#define YARP_COMPILER_WARNING(x)
#define YARP_COMPILER_ERROR(x)
#endif

#ifndef YARP_NO_DEPRECATED_WARNINGS
#define YARP_COMPILER_DEPRECATED_WARNING(x) YARP_COMPILER_WARNING(x)
#else
#define YARP_COMPILER_DEPRECATED_WARNING(x)
#endif


///@{

/**
* \def YARP_BEGIN_PACK
*
* Starts 1 byte packing for structs/classes.
*
* Instructs the compiler that the following structure/class has to be
* packed with 1 byte boundary. This is conditionally generated depending
* on the compiler and architecture. It assures interoperability of network
* communication between compilers.
*
* \see \ref port_expert_data, YARP_END_PACK
*/

/**
* \def YARP_END_PACK
*
* Ends 1 byte packing for structs/classes.
*
* Instructs the compiler that the default packing can be reinstated.
*
* \see \ref port_expert_data YARP_BEGIN_PACK
*/

/**
* \def PACKED_FOR_NET
*
* FIXME Add documentation.
*
* \see \ref port_expert_data
*/

///@}


#if defined(CYGWIN)
# define YARP_BEGIN_PACK \
_Pragma("pack(1)")
# define YARP_END_PACK \
_Pragma("pack()")
#elif defined(_MSC_VER)
// use packing and make no apologies about it
# define YARP_BEGIN_PACK \
__pragma(warning(push)) \
__pragma(warning(disable:4103)) \
__pragma(pack(push, 1))
# define YARP_END_PACK \
__pragma(pack(pop)) \
__pragma(warning(pop))
#elif defined(__linux__)
# define YARP_BEGIN_PACK \
_Pragma("pack(1)")
# define YARP_END_PACK _Pragma("pack()")
#elif defined(__DARWIN__)
# define YARP_BEGIN_PACK \
_Pragma("pack(1)")
# define YARP_END_PACK \
_Pragma("pack()")
#elif defined(__APPLE__)
# define YARP_BEGIN_PACK \
_Pragma("pack(1)")
# define YARP_END_PACK \
_Pragma("pack()")
#elif defined(__QNX4__)
# define YARP_BEGIN_PACK \
_Pragma(" pack (push) ;") \
_Pragma(" pack (1) ;")
# define YARP_END_PACK \
_Pragma(" pack (pop) ;")
#elif defined(__QNX6__)
//_Pragma("align 1")
# define YARP_BEGIN_PACK \
_Pragma("pack(1)")
//_Pragma("align 0")
# define YARP_END_PACK \
_Pragma("pack()")
#else
//#warning "Platform not known, guessing, please update "
# define YARP_BEGIN_PACK \
_Pragma("pack(1)")
# define YARP_END_PACK \
_Pragma("pack()")
#endif

#ifndef PACKED_FOR_NET
# define PACKED_FOR_NET
#endif



#endif
10 changes: 6 additions & 4 deletions doc/port_expert.dox
Original file line number Diff line number Diff line change
Expand Up @@ -301,13 +301,14 @@ may add different amounts of "padding" into your structure

If you define your data-structure as follows:
\code
#include <yarp/os/begin_pack_for_net.h>
#include <yarp/conf/system.h>
YARP_BEGIN_PACK
class Target {
public:
NetInt32 x;
NetInt32 y;
} PACKED_FOR_NET;
#include <yarp/os/end_pack_for_net.h>
YARP_END_PACK
\endcode
Then you'll be in better shape. The integers now have a well defined
representation, and the compiler is requested not to introduce padding.
Expand All @@ -316,8 +317,9 @@ So this is a portable representation.
Even better, for YARP, is to add in a small "header" that describes
your data-type. If you defined Target as follows:
\code
#include <yarp/conf/system.h>
#include <yarp/os/Bottle.h>
#include <yarp/os/begin_pack_for_net.h>
YARP_BEGIN_PACK
class Target {
public:
NetInt32 tag;
Expand All @@ -329,7 +331,7 @@ public:
len = 2;
}
} PACKED_FOR_NET;
#include <yarp/os/end_pack_for_net.h>
YARP_END_PACK
\endcode

Then suddenly ports carrying this data become a lot easy to
Expand Down
6 changes: 4 additions & 2 deletions example/port_power/TargetVer1.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
#ifndef TARGETVER1_INC
#define TARGETVER1_INC

#include <yarp/os/begin_pack_for_net.h>
#include <yarp/conf/system.h>

YARP_BEGIN_PACK
class Target {
public:
NetInt32 x;
NetInt32 y;
} PACKED_FOR_NET;
#include <yarp/os/end_pack_for_net.h>
YARP_END_PACK

#endif
5 changes: 3 additions & 2 deletions example/port_power/TargetVer1b.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
#ifndef TARGETVER1B_INC
#define TARGETVER1B_INC

#include <yarp/conf/system.h>
#include <yarp/os/Bottle.h>

#include <yarp/os/begin_pack_for_net.h>
YARP_BEGIN_PACK
class Target {
public:
Target() {
Expand All @@ -24,6 +25,6 @@ class Target {
NetInt32 x;
NetInt32 y;
} PACKED_FOR_NET;
#include <yarp/os/end_pack_for_net.h>
YARP_END_PACK

#endif
5 changes: 3 additions & 2 deletions src/carriers/wire_rep_utils/BlobNetworkHeader.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
#ifndef BLOBHEADER_INC
#define BLOBHEADER_INC

#include <yarp/conf/system.h>
#include <yarp/os/Bottle.h>
#include <yarp/os/NetInt32.h>

// translate to blobs for now; better translation requires type system
#include <yarp/os/begin_pack_for_net.h>
YARP_BEGIN_PACK
class BlobNetworkHeader {
public:
yarp::os::NetInt32 listTag;
Expand All @@ -29,6 +30,6 @@ class BlobNetworkHeader {
}

} PACKED_FOR_NET;
#include <yarp/os/end_pack_for_net.h>
YARP_END_PACK

#endif
5 changes: 3 additions & 2 deletions src/carriers/wire_rep_utils/WireImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#ifndef YARP2_WIREIMAGE
#define YARP2_WIREIMAGE

#include <yarp/conf/system.h>
#include <yarp/os/SizedWriter.h>
#include <yarp/os/StringOutputStream.h>
#include <yarp/os/ConnectionWriter.h>
Expand All @@ -34,14 +35,14 @@
// uint8[] data --> real payload
*/

#include <yarp/os/begin_pack_for_net.h>
YARP_BEGIN_PACK
class RosImageStamp {
public:
yarp::os::NetInt32 seq;
yarp::os::NetInt32 sec;
yarp::os::NetInt32 nsec;
} PACKED_FOR_NET;
#include <yarp/os/end_pack_for_net.h>
YARP_END_PACK

/**
*
Expand Down
7 changes: 5 additions & 2 deletions src/libYARP_OS/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ set(YARP_OS_HDRS include/yarp/os/AbstractCarrier.h
include/yarp/os/AbstractContactable.h
include/yarp/os/all.h
include/yarp/os/api.h
include/yarp/os/begin_pack_for_net.h
include/yarp/os/BinPortable.h
include/yarp/os/Bottle.h
include/yarp/os/BufferedPort.h
Expand All @@ -26,7 +25,6 @@ set(YARP_OS_HDRS include/yarp/os/AbstractCarrier.h
include/yarp/os/ContactStyle.h
include/yarp/os/DummyConnector.h
include/yarp/os/Election.h
include/yarp/os/end_pack_for_net.h
include/yarp/os/Event.h
include/yarp/os/Face.h
include/yarp/os/IConfig.h
Expand Down Expand Up @@ -118,6 +116,11 @@ set(YARP_OS_HDRS include/yarp/os/AbstractCarrier.h
include/yarp/os/YarpPluginSelector.h
include/yarp/os/YarpPluginSettings.h)

if(NOT YARP_NO_DEPRECATED)
list(APPEND YARP_OS_HDRS include/yarp/os/begin_pack_for_net.h
include/yarp/os/end_pack_for_net.h)
endif()

set(YARP_OS_IDL_HDRS include/yarp/os/idl/BareStyle.h
include/yarp/os/idl/BottleStyle.h
include/yarp/os/idl/Unwrapped.h
Expand Down
6 changes: 4 additions & 2 deletions src/libYARP_OS/harness/BinPortableTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*
*/

#include <yarp/conf/system.h>

#include <yarp/os/BinPortable.h>
#include <yarp/os/PortReaderBuffer.h>
#include <yarp/os/Port.h>
Expand All @@ -24,7 +26,7 @@ using namespace yarp::os::impl;
using namespace yarp::os;


#include <yarp/os/begin_pack_for_net.h>
YARP_BEGIN_PACK
class BinPortableTarget {
public:
BinPortableTarget() {
Expand All @@ -37,7 +39,7 @@ class BinPortableTarget {
NetInt32 x;
NetInt32 y;
} PACKED_FOR_NET;
#include <yarp/os/end_pack_for_net.h>
YARP_END_PACK


class BinPortableTest : public UnitTest {
Expand Down
Loading

0 comments on commit f327fa3

Please sign in to comment.