interreality.org [VOS]
[Home] [About]
[Screenshots]
[Download]
[News]
[Community]
[Documentation] [Manual]
[Bugs & Requests] [Wiki]

/home/tetron/hack/vos/libs/vos/vos/vosdefs.hh

Go to the documentation of this file.
00001 /*
00002     This file is part of the Virtual Object System of
00003     the Interreality project (http://interreality.org).
00004 
00005     Copyright (C) 2001-2003 Peter Amstutz
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Lesser General Public
00009     License as published by the Free Software Foundation; either
00010     version 2 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Lesser General Public License for more details.
00016 
00017     You should have received a copy of the GNU Lesser General Public
00018     License along with this library; if not, write to the Free Software
00019     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00020 
00021     Peter Amstutz <http://www.interreality.org>
00022 */
00023 
00024 
00025 #ifndef _VOSDEFS_HH_
00026 #define _VOSDEFS_HH_
00027 
00028 #include <vos/vutil/vutildefs.hh>
00029 
00030 /** @namespace VOS Virtual Object System core library */
00031 
00032 #ifdef USE_STRSTREAM
00033 #include <strstream>
00034 #else
00035 #include <sstream>
00036 #endif
00037 
00038 #if defined(_WIN32) && defined(_MSC_VER)
00039 # pragma warning (disable : 4250)
00040 //# pragma warning (disable : 4290) // warns about throw()
00041 # pragma warning (disable : 4995)
00042 # pragma warning (disable : 4251)
00043 # pragma warning (disable : 4003)
00044 # pragma warning (disable : 4275)
00045 #endif
00046 
00047 #if defined(WIN32)
00048 # ifdef VOS_EXPORTS
00049 #  define VOS_API __declspec(dllexport)
00050 # else
00051 #  define VOS_API __declspec(dllimport)
00052 # endif
00053 #else
00054 # ifdef HAVE_GCC_VISIBILITY
00055 #  define VOS_API __attribute__ ((visibility("default")))
00056 # else
00057 #  define VOS_API
00058 # endif
00059 #endif
00060 
00061 #ifdef HAVE_SYS_SOCKET_H
00062 # include <sys/socket.h>
00063 # include <netinet/in.h>
00064 # include <netinet/tcp.h>
00065 # include <netdb.h>
00066 #else
00067 # ifdef HAVE_LIBWS2_32
00068 #  include <winsock2.h>
00069 #  include <ws2tcpip.h>
00070 #  define VOS_USE_WINSOCK2
00071 # else
00072 # warning we do not have a header file for the socket functions, things will probably fail
00073 # endif
00074 #endif
00075 
00076 #include <boost/thread/thread.hpp>
00077 
00078 #ifndef HAVE_SNPRINTF
00079 # include <vos/vutil/snprintf.hh>
00080 #endif
00081 
00082 #include <time.h>
00083 #include <string.h>
00084 #include <sys/types.h>
00085 #include <stdio.h>
00086 
00087 #ifdef HAVE_SYS_TIME_H
00088 # include <sys/time.h>
00089 #endif
00090 
00091 #ifdef HAVE_UNISTD_H
00092 # include <unistd.h>
00093 #endif
00094 
00095 #ifdef SSL_SUPPORT
00096 #include <openssl/ssl.h>
00097 #include <openssl/rand.h>
00098 #include <openssl/err.h>
00099 #else
00100 #define X509 void
00101 #define SSL_CTX void
00102 #define X509_STORE_CTX void
00103 #define SSL void
00104 #endif
00105 
00106 #ifndef SOL_TCP
00107 #define SOL_TCP (getprotobyname("tcp")->p_proto)
00108 #endif
00109 
00110 // work around an annoying difference between winsock2.h and sys/socket.h
00111 #ifdef HAVE_LIBWS2_32
00112 # define SETSOCKOPT_PARAM4_CAST char*
00113 #else
00114 # define SETSOCKOPT_PARAM4_CAST int*
00115 #endif
00116 
00117 
00118 #if !defined(HAVE_SOCKLEN_T) && !defined(__socklen_t_defined)
00119 //#warning socklen_t not defined. Defining it as int.
00120 typedef int socklen_t;
00121 #define __socklen_t_defined 1
00122 #endif
00123 
00124 /* seconds */
00125 #define VOS_DEFAULT_TIMEOUT 45.0
00126 
00127 #if defined(WIN32)
00128 #define DLLEXPORT __declspec(dllexport)
00129 #define DLLIMPORT __declspec(dllimport)
00130 #else
00131 # ifdef HAVE_GCC_VISIBILITY
00132 #  define DLLEXPORT __attribute__ ((visibility("default")))
00133 #  define DLLIMPORT __attribute__ ((visibility("default")))
00134 # else
00135 #  define DLLEXPORT
00136 #  define DLLIMPORT
00137 # endif
00138 #endif
00139 
00140 /** @def BEGIN_REGISTER_METAOBJECT_FACTORIES
00141     Use these macros to define a static registration class for
00142     a metaobject.  This lets you register metaobject factories automatically
00143     when your library is loaded.
00144 
00145     You must also use the REGISTERS_METAOBJECT_FACTORIES macro in a
00146     public header file as well.
00147 
00148     Example:
00149     @code
00150         BEGIN_REGISTER_METAOBJECT_FACTORIES(Example)
00151             Site::addLocalMetaObjectFactory(typeid(Example).name(), &LocalExample::new_LocalExample);
00152             Site::addLocalMetaObjectFactory(typeid(LocalExample).name(), &LocalExample::new_LocalExample);
00153             Site::addLocalMetaObjectFactory("example:example", &LocalExample::new_LocalExample);
00154 
00155             Site::addRemoteMetaObjectFactory(typeid(Example).name(), "example:example", &RemoteExample::new_RemoteExample);
00156             Site::addRemoteMetaObjectFactory(typeid(RemoteExample).name(), "example:example", &RemoteExample::new_RemoteExample);
00157             Site::addRemoteMetaObjectFactory("example:example", "example:example", &RemoteExample::new_RemoteExample);
00158         END_REGISTER_METAOBJECT_FACTORIES(Example)
00159       @endcode
00160 
00161       If your metaobject does not need seperate Local and Remote behavior,
00162       you only need one class, for which you can register the same remote
00163       and local factory.
00164 
00165       @param cl A symbol that uniquely identifies this registration (e.g. use
00166       the MetaObject class name)
00167     */
00168 // @{
00169 #define BEGIN_REGISTER_METAOBJECT_FACTORIES(cl)                              \
00170     class Register##cl {                                                \
00171     public:                                                            \
00172     Register##cl()                                                      \
00173     {
00174 
00175 #define END_REGISTER_METAOBJECT_FACTORIES(cl)                                      \
00176     }                                                                   \
00177     };                                                                  \
00178     DLLEXPORT Register##cl Register##cl##_globalstatic;
00179 // @}
00180 
00181 #define BEGIN_REGISTER_EXTENDERS(cl) BEGIN_REGISTER_METAOBJECT_FACTORIES(cl)
00182 #define END_REGISTER_EXTENDERS(cl) END_REGISTER_METAOBJECT_FACTORIES(cl)
00183 
00184 /** Alternate macro for registering extender factories.
00185     Use this macro when you want to use the same MetaObject class for
00186     both local and remote objects, and don't need to do anything special
00187     at registration.  It creates a registration class named from
00188     a namespace and class name, containing a constructor which registers
00189     the same factory function for local and remote metaobject creation.
00190 
00191     You must also use the REGISTERS_METAOBJECT_FACTORIES macro in a
00192     public header file as well, using <ns>_<cl> as its argument (see below)
00193 
00194     @param ns   Namespace
00195     @param cl   Class name
00196     @param type Type string, should be the same string that getVOSType()
00197 returns.
00198     @param factfn Static method that returns an instance of your class (as
00199         a VOS::MetaObject*), given a "super object" (of type VOS::VobjectBase*)
00200         and a type string.
00201 
00202     For example, imagine you have a metaobject class called ExampleClass.
00203     It is in a namespace called EG.  It might be declared in a header file like
00204     this:
00205     @code
00206         #include <vos/vos/vos.hh>
00207         ...
00208         namespace EG {
00209             class ExampleClass : public VOS::MetaObject {
00210             public:
00211                 ExampleClass(VOS::VobjectBase* s);
00212                 VOS::MetaObject* new_ExampleClass(VOS::VobjectBase* s, const std::string& t) {
00213                    return new ExampleClass(s);
00214                 }
00215                 ...
00216             };
00217         }
00218     @endcode
00219 
00220     Somewhere in the definition (i.e. in your ".cc" or ".cpp" file), include
00221     this macro call:
00222     @code
00223         REGISTER_METAOBJECT_FACTORIES(EG, ExampleClass, "example:example", &EG::ExampleClass::new_ExampleClass)
00224         }
00225     @endcode
00226 
00227 
00228     Now to the public header file, add this:
00229     @code
00230         ...
00231         REGISTERS_METAOBJECT_FACTORIES(EG_ExampleClass)
00232         ...
00233     @endcode
00234 */
00235 #define REGISTER_METAOBJECT_FACTORIES(ns, cl, type, factfn) \
00236     class Register##ns##_##cl { \
00237     public: \
00238         Register##ns##_##cl() { \
00239             Site::addLocalMetaObjectFactory(typeid(ns::cl).name(), factfn); \
00240             Site::addLocalMetaObjectFactory(type, factfn); \
00241             Site::addRemoteMetaObjectFactory(typeid(ns::cl).name(), type, factfn); \
00242             Site::addRemoteMetaObjectFactory(type, type, factfn); \
00243         } \
00244     }; \
00245     DLLEXPORT Register##ns##_cl Register##ns##_cl##_globalstatic;
00246 
00247 /** You must use this macro in a public header file for every use of
00248  * VOS_METAOBJECT_FACTORY or BEGIN_REGISTER_EXTENDERS. (It grabs a reference
00249  *  to the registration cleass, forcing the dynamic library loader to
00250  *  initialize it)
00251  * @param cl    Class name: use the same argument you used in defining the
00252  *              factory registration class.
00253  */
00254 #define IMPORT_METAOBJECT_FACTORIES(cl)                        \
00255     class Register##cl { };                                    \
00256     extern DLLIMPORT Register##cl Register##cl##_globalstatic; \
00257     static Register##cl &Register##cl##_localref = Register##cl##_globalstatic;
00258 
00259 #define REGISTERS_METAOBJECT_FACTORIES(cl) IMPORT_METAOBJECT_FACTORIES(cl)
00260 
00261 #endif