Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DLL-inteface for operator<< (MSVC compatibility) #114

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ set(docopt_HEADERS
docopt_private.h
docopt_util.h
docopt_value.h
docopt_api.h
)

#============================================================================
Expand Down
16 changes: 16 additions & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
skip_tags: true

environment:
PYTHON: C:\\Python27-x64
APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017

install:
- set PATH=%PYTHON%;%PYTHON%\\Scripts\\;%PATH%

build: false

build_script:
- mkdir build && cd build
- cmake -G "Visual Studio 15 2017 Win64" ..
- cmake --build . --config Debug

32 changes: 5 additions & 27 deletions docopt.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,13 @@
#ifndef docopt__docopt_h_
#define docopt__docopt_h_

#include "docopt_api.h"
#include "docopt_value.h"

#include <map>
#include <vector>
#include <string>

#ifdef DOCOPT_HEADER_ONLY
#define DOCOPT_INLINE inline
#define DOCOPT_API
#else
#define DOCOPT_INLINE

// With Microsoft Visual Studio, export certain symbols so they
// are available to users of docopt.dll (shared library). The DOCOPT_DLL
// macro should be defined if building a DLL (with Visual Studio),
// and by clients using the DLL. The CMakeLists.txt and the
// docopt-config.cmake it generates handle this.
#ifdef DOCOPT_DLL
// Whoever is *building* the DLL should define DOCOPT_EXPORTS.
// The CMakeLists.txt that comes with docopt does this.
// Clients of docopt.dll should NOT define DOCOPT_EXPORTS.
#ifdef DOCOPT_EXPORTS
#define DOCOPT_API __declspec(dllexport)
#else
#define DOCOPT_API __declspec(dllimport)
#endif
#else
#define DOCOPT_API
#endif
#endif

namespace docopt {

// Usage string could not be parsed (ie, the developer did something wrong)
Expand Down Expand Up @@ -67,7 +43,8 @@ namespace docopt {
/// @throws DocoptExitHelp if 'help' is true and the user has passed the '--help' argument
/// @throws DocoptExitVersion if 'version' is true and the user has passed the '--version' argument
/// @throws DocoptArgumentError if the user's argv did not match the usage patterns
std::map<std::string, value> DOCOPT_API docopt_parse(std::string const& doc,
DOCOPT_API
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little nervous about swapping the order of these (the return type and the annotation). Was this intentional?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

__declspec(dllexport)/__declspec(dllimport) should be of left-side */& (MSVC specfic). This change for one-style with operator<<

std::map<std::string, value> docopt_parse(std::string const& doc,
std::vector<std::string> const& argv,
bool help = true,
bool version = true,
Expand All @@ -80,7 +57,8 @@ namespace docopt {
/// * DocoptExitHelp - print usage string and terminate (with exit code 0)
/// * DocoptExitVersion - print version and terminate (with exit code 0)
/// * DocoptArgumentError - print error and usage string and terminate (with exit code -1)
std::map<std::string, value> DOCOPT_API docopt(std::string const& doc,
DOCOPT_API
std::map<std::string, value> docopt(std::string const& doc,
std::vector<std::string> const& argv,
bool help = true,
std::string const& version = {},
Expand Down
35 changes: 35 additions & 0 deletions docopt_api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// docopt_api.h
// docopt
//
//

#ifndef docopt__api_h_
#define docopt__api_h_

#ifdef DOCOPT_HEADER_ONLY
#define DOCOPT_INLINE inline
#define DOCOPT_API
#else
#define DOCOPT_INLINE

// With Microsoft Visual Studio, export certain symbols so they
// are available to users of docopt.dll (shared library). The DOCOPT_DLL
// macro should be defined if building a DLL (with Visual Studio),
// and by clients using the DLL. The CMakeLists.txt and the
// docopt-config.cmake it generates handle this.
#ifdef DOCOPT_DLL
// Whoever is *building* the DLL should define DOCOPT_EXPORTS.
// The CMakeLists.txt that comes with docopt does this.
// Clients of docopt.dll should NOT define DOCOPT_EXPORTS.
#ifdef DOCOPT_EXPORTS
#define DOCOPT_API __declspec(dllexport)
#else
#define DOCOPT_API __declspec(dllimport)
#endif
#else
#define DOCOPT_API
#endif
#endif

#endif /* defined(docopt__api_h_) */
4 changes: 3 additions & 1 deletion docopt_value.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#ifndef docopt__value_h_
#define docopt__value_h_

#include "docopt_api.h"

#include <stdexcept>
#include <string>
#include <vector>
Expand Down Expand Up @@ -105,7 +107,7 @@ namespace docopt {
};

/// Write out the contents to the ostream
std::ostream& operator<<(std::ostream&, value const&);
DOCOPT_API std::ostream& operator<<(std::ostream&, value const&);
}

namespace std {
Expand Down