SampleCxxModule.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #pragma once
  8. #include <memory>
  9. #include <vector>
  10. #include <cxxreact/CxxModule.h>
  11. namespace facebook {
  12. namespace xplat {
  13. namespace samples {
  14. // In a less contrived example, Sample would be part of a traditional
  15. // C++ library.
  16. class Sample {
  17. public:
  18. std::string hello();
  19. double add(double a, double b);
  20. std::string concat(const std::string &a, const std::string &b);
  21. std::string repeat(int count, const std::string &str);
  22. void save(std::map<std::string, std::string> dict);
  23. std::map<std::string, std::string> load();
  24. void call_later(int msec, std::function<void()> f);
  25. void except();
  26. double twice(double n);
  27. private:
  28. std::map<std::string, std::string> state_;
  29. };
  30. class SampleCxxModule : public module::CxxModule {
  31. public:
  32. SampleCxxModule(std::unique_ptr<Sample> sample);
  33. std::string getName();
  34. virtual auto getConstants() -> std::map<std::string, folly::dynamic>;
  35. virtual auto getMethods() -> std::vector<Method>;
  36. private:
  37. void save(folly::dynamic args);
  38. void load(folly::dynamic args, Callback cb);
  39. std::unique_ptr<Sample> sample_;
  40. };
  41. } // namespace samples
  42. } // namespace xplat
  43. } // namespace facebook
  44. extern "C" facebook::xplat::module::CxxModule *SampleCxxModule();