Created
March 27, 2011 17:51
-
-
Save JulianKniephoff/889411 to your computer and use it in GitHub Desktop.
Example code for http://juliankniephoff.wordpress.com/2011/03/27/use-objective-c-classes-in-cpp-opaquely-readably-and-safely
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CMAKE_MINIMUM_REQUIRED( | |
VERSION | |
2.8 | |
) | |
PROJECT( | |
speech | |
) | |
FIND_LIBRARY( | |
APPKIT_LIBRARY | |
AppKit | |
) | |
FIND_PATH( | |
APPKIT_INCLUDE_DIR | |
AppKit/AppKit.h | |
) | |
INCLUDE_DIRECTORIES( | |
${APPKIT_INCLUDE_DIR} | |
) | |
INCLUDE_DIRECTORIES( | |
. | |
) | |
ADD_LIBRARY( | |
speech | |
speaker_impl.mm | |
speaker.mm | |
) | |
TARGET_LINK_LIBRARIES( | |
speech | |
${APPKIT_LIBRARY} | |
) | |
SET_TARGET_PROPERTIES( | |
speech | |
PROPERTIES | |
COMPILE_FLAGS -fobjc-arc | |
) | |
ADD_EXECUTABLE( | |
speak | |
speak.cpp | |
) | |
TARGET_LINK_LIBRARIES( | |
speak | |
speech | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <speaker.hpp> | |
int main(int argc, char *argv[]) | |
{ | |
your::friends::library::speech::speaker speaker; | |
for (int word = 1; word < argc; ++word) | |
{ | |
speaker.speak( | |
argv[word] | |
); | |
while ( | |
speaker.is_speaking() | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef YOUR_FRIENDS_LIBRARY_SPEECH_SPEAKER_HPP | |
#define YOUR_FRIENDS_LIBRARY_SPEECH_SPEAKER_HPP | |
#include <speaker_impl_fwd.hpp> | |
#include <string> | |
namespace your | |
{ | |
namespace friends | |
{ | |
namespace library | |
{ | |
namespace speech | |
{ | |
class speaker | |
{ | |
public: | |
speaker(); | |
~speaker(); | |
void | |
speak( | |
std::string const & | |
); | |
bool | |
is_speaking() const; | |
private: | |
speaker_impl * const pimpl_; | |
}; | |
} | |
} | |
} | |
} | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <speaker.hpp> | |
#include <speaker_impl.hpp> | |
#include <Foundation/NSString.h> | |
your::friends::library::speech::speaker::speaker(): | |
pimpl_( | |
new your::friends::library::speech::speaker_impl | |
) | |
{ | |
} | |
your::friends::library::speech::speaker::~speaker() | |
{ | |
delete pimpl_; | |
} | |
void | |
your::friends::library::speech::speaker::speak( | |
std::string const & string | |
) | |
{ | |
[pimpl_->synth_ startSpeakingString: | |
[NSString stringWithUTF8String: | |
string.c_str() | |
] | |
]; | |
} | |
bool | |
your::friends::library::speech::speaker::is_speaking() const | |
{ | |
return [pimpl_->synth_ isSpeaking]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef YOUR_FRIENDS_LIBRARY_SPEECH_SPEAKER_IMPL_HPP | |
#define YOUR_FRIENDS_LIBRARY_SPEECH_SPEAKER_IMPL_HPP | |
#import <AppKit/NSSpeechSynthesizer.h> | |
namespace your | |
{ | |
namespace friends | |
{ | |
namespace library | |
{ | |
namespace speech | |
{ | |
struct speaker_impl | |
{ | |
public: | |
speaker_impl(); | |
NSSpeechSynthesizer * const synth_; | |
}; | |
} | |
} | |
} | |
} | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <speaker_impl.hpp> | |
your::friends::library::speech::speaker_impl::speaker_impl(): | |
synth_( | |
[[NSSpeechSynthesizer alloc] init] | |
) | |
{ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef YOUR_FRIENDS_LIBRARY_SPEECH_SPEAKER_IMPL_FWD_HPP | |
#define YOUR_FRIENDS_LIBRARY_SPEECH_SPEAKER_IMPL_FWD_HPP | |
namespace your | |
{ | |
namespace friends | |
{ | |
namespace library | |
{ | |
namespace speech | |
{ | |
struct speaker_impl; | |
} | |
} | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment