RTBKit  0.9
Open-source framework to create real-time ad bidding systems.
js/currency_js.cc
00001 
00010 #include "currency_js.h"
00011 #include "rtbkit/common/currency.h"
00012 #include "soa/js/js_wrapped.h"
00013 #include "soa/js/js_call.h"
00014 #include "soa/js/js_utils.h"
00015 #include "soa/js/js_registry.h"
00016 #include "soa/sigslot/slot.h"
00017 
00018 using namespace std;
00019 using namespace v8;
00020 using namespace node;
00021 using namespace RTBKIT;
00022 
00023 
00024 namespace Datacratic {
00025 namespace JS {
00026 
00027 
00028 /******************************************************************************/
00029 /* AMOUNT JS                                                                  */
00030 /******************************************************************************/
00031 
00032 extern const char* const AmountName;
00033 const char * const AmountName = "Amount";
00034 
00035 struct AmountJS :
00036     public JSWrapped2<
00037         Amount,
00038         AmountJS,
00039         AmountName,
00040         bidRequestModule>
00041 {
00042     AmountJS() {}
00043 
00044     AmountJS(
00045             v8::Handle<v8::Object> This,
00046             const std::shared_ptr<Amount>& amount =
00047                 std::shared_ptr<Amount>())
00048     {
00049         HandleScope scope;
00050         wrap(This, amount);
00051     }
00052 
00053     static Handle<v8::Value>
00054     New(const Arguments& args)
00055     {
00056         try {
00057             string currency = getArg<string>(args, 1, "NONE", "currency");
00058             int64_t value = getArg<int64_t>(args, 2, 0, "value");
00059 
00060             //cerr << "new amount " << currency << " " << value << endl;
00061 
00062             auto obj = std::make_shared<Amount>(currency, value);
00063             new AmountJS(args.This(), obj);
00064 
00065             return args.This();
00066         }
00067         HANDLE_JS_EXCEPTIONS;
00068     }
00069 
00070 
00071     static void
00072     Initialize()
00073     {
00074         Persistent<FunctionTemplate> t = Register(New);
00075 
00076         registerROProperty(&Amount::value, "value");
00077         registerROProperty(&Amount::getCurrencyStr, "currencyCode");
00078 
00079         registerMemberFn(&Amount::isZero, "isZero");
00080         registerMemberFn(&Amount::isNonNegative, "isNonNegative");
00081         registerMemberFn(&Amount::isNegative, "isNegative");
00082 
00083         registerMemberFn(&Amount::limit, "limit");
00084         registerMemberFn(&Amount::currencyIsCompatible, "currencyIsCompatible");
00085 
00086         // Typedef required to disambiguate the operator -
00087         typedef Amount (Amount::*SubOpFn)() const;
00088 
00089         registerMemberFn((SubOpFn)&Amount::operator-, "sub");
00090         registerMemberFn(&Amount::operator+, "add");
00091         registerMemberFn(&Amount::operator<, "lt");
00092         registerMemberFn(&Amount::operator<=, "le");
00093         registerMemberFn(&Amount::operator>, "gt");
00094         registerMemberFn(&Amount::operator>=, "ge");
00095         registerMemberFn(&Amount::operator==, "equals");
00096 
00097 #if 0
00098         registerMemberFn(&Amount::toMicro, "toMicro");
00099         registerMemberFn(&Amount::toUnit, "toUnit");
00100         registerMemberFn(&Amount::toCPM, "toCPM");
00101         registerMemberFn(&Amount::toMicroCPM, "toMicroCPM");
00102 #endif
00103 
00104         registerMemberFn(&Amount::getCurrencyStr, "currencyCode");
00105 
00106         //NODE_SET_PROTOTYPE_METHOD(t, "currencyCode", getCurrencyCode);
00107     }
00108 
00109     static Handle<Value>
00110     getCurrencyCode(const Arguments& args)
00111     {
00112         try {
00113             return Datacratic::JS::toJS(getShared(args)->getCurrencyStr());
00114         }
00115         HANDLE_JS_EXCEPTIONS;
00116 
00117         return Handle<Value>();
00118     }
00119 };
00120 
00121 
00122 Amount from_js(const JSValue& value, Amount*)
00123 {
00124     return *AmountJS::fromJS(value);
00125 }
00126 
00127 Amount* from_js(const JSValue& value, Amount**)
00128 {
00129     return AmountJS::fromJS(value).get();
00130 }
00131 
00132 void to_js(JS::JSValue& value, const Amount& amount)
00133 {
00134     value = AmountJS::toJS(make_shared<Amount>(amount));
00135 }
00136 
00137 
00138 /******************************************************************************/
00139 /* AMOUNT CONSTRUCTOR                                                         */
00140 /******************************************************************************/
00141 
00142 template<typename T, typename V>
00143 Handle<Value> amountFn(const Arguments& args)
00144 {
00145     try {
00146         V value = getArg<V>(args, 0, 0, "value");
00147         return AmountJS::toJS(make_shared<T>(value));
00148     }
00149     HANDLE_JS_EXCEPTIONS;
00150 }
00151 
00152 template<typename Fn>
00153 void registerModuleFn(Handle<Object>& target, const string& name, Fn fn)
00154 {
00155     auto tpl = Persistent<FunctionTemplate>::New(FunctionTemplate::New(fn));
00156     target->Set(String::NewSymbol(name.c_str()), tpl->GetFunction());
00157 }
00158 
00159 void initCurrencyFunctions(Handle<v8::Object>& target)
00160 {
00161     registerModuleFn(target, "MicroUSD",     amountFn<MicroUSD, int64_t>);
00162     registerModuleFn(target, "USD",          amountFn<USD, double>);
00163     registerModuleFn(target, "USD_CPM",      amountFn<USD_CPM, double>);
00164     registerModuleFn(target, "MicroUSD_CPM", amountFn<MicroUSD_CPM, int64_t>);
00165 }
00166 
00167 } // namepsace JS
00168 } // namespace Datacratic
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator