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.mm | |
) | |
TARGET_LINK_LIBRARIES( | |
speech | |
${APPKIT_LIBRARY} | |
) | |
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 <synthesizer.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: | |
synthesizer * 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.hpp> | |
#import <AppKit/AppKit.h> | |
your::friends::library::speech::speaker::speaker(): | |
synth_( | |
[[NSSpeechSynthesizer alloc] init] | |
) | |
{ | |
} | |
your::friends::library::speech::speaker::~speaker() | |
{ | |
[synth_ release]; | |
} | |
void | |
your::friends::library::speech::speaker::speak( | |
std::string const & string | |
) | |
{ | |
[synth_ startSpeakingString: | |
[NSString stringWithUTF8String: | |
string.c_str() | |
] | |
]; | |
} | |
bool | |
your::friends::library::speech::speaker::is_speaking() const | |
{ | |
return [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_SYNTHESIZER_HPP | |
#define YOUR_FRIENDS_LIBRARY_SPEECH_SYNTHESIZER_HPP | |
#if __OBJC__ | |
#import <Appkit/AppKit.h> | |
#endif | |
namespace your | |
{ | |
namespace friends | |
{ | |
namespace library | |
{ | |
namespace speech | |
{ | |
#if __OBJC__ | |
typedef NSSpeechSynthesizer synthesizer; | |
#else | |
typedef void synthesizer; | |
#endif | |
} | |
} | |
} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment