// /// The TFont copy constructor. // TFont::TFont(const TFont& src) { #if !defined(NO_GDI_SHARE_HANDLES) Handle = src.Handle; RefAdd(Handle, Font); #else LOGFONT logFont; src.GetObject(logFont); Handle = ::CreateFontIndirect(&logFont); WARNX(OwlGDI, !Handle, 0, _T("Cannot create TFont from TFont @") << hex << uint32(LPVOID(&src))); CheckValid(); RefAdd(Handle, Font); #endif }
// /// Creates a TFont object from the given LOGFONT. // TFont::TFont(const LOGFONT& logFont) { Handle = ::CreateFontIndirect(&logFont); WARNX(OwlGDI, !Handle, 0, "Cannot create TFont from logfont @" << static_cast<const void*>(&logFont)); CheckValid(); RefAdd(Handle, Font); }
// /// Creates a TFont object and sets the Handle data member to the given borrowed /// handle. The ShouldDelete data member defaults to false, ensuring that the /// borrowed handle will not be deleted when the C++ object is destroyed. // TFont::TFont(HFONT handle, TAutoDelete autoDelete) : TGdiObject(handle, autoDelete) { if (ShouldDelete) RefAdd(Handle, Font); }
// /// Create a bitmap & get its handle, given an other bitmap /// Used by ctors here and in derived classes. Assumes Handle can be /// written over, & adds handle to reference container. // void TBitmap::Create(const TBitmap& src) { TMemoryDC memDC1; TMemoryDC memDC2; BITMAP bm; src.GetObject(bm); if (bm.bmPlanes != 1 || bm.bmBitsPixel != 1) { // create a color bitmap (Assume screen compatible) TScreenDC dc; Handle = ::CreateCompatibleBitmap(dc, bm.bmWidth, bm.bmHeight); } else // create a mono bitmap Handle = ::CreateBitmap(bm.bmWidth, bm.bmHeight, 1, 1, 0); CheckValid(); RefAdd(Handle, Bitmap); memDC1.SelectObject(src); memDC2.SelectObject(*this); memDC2.BitBlt(TRect(TPoint(0, 0), TSize(bm.bmWidth, bm.bmHeight)), memDC1, TPoint(0, 0), SRCCOPY); }
// /// Creates a bitmap object from the given metaFile using the given palette and size /// arguments. // TBitmap::TBitmap(const TMetaFilePict& metaFile, TPalette& palette, const TSize& defSize) { // Adjust size to final metafile size as needed // TMemoryDC memDC; TSize size = metaFile.CalcPlaySize(memDC, defSize); // Create bitmap, either mono or screen compatible // uint16 nColors; palette.GetObject(nColors); if (nColors > 2) { TScreenDC dc; Handle = ::CreateCompatibleBitmap(dc, size.cx, size.cy); } else Handle = ::CreateBitmap(size.cx, size.cy, 1, 1, 0); CheckValid(); RefAdd(Handle, Bitmap); // clear bitmap, then play metafile onto it // memDC.SelectObject(*this); memDC.SelectStockObject(WHITE_BRUSH); memDC.PatBlt(0, 0, size.cx, size.cy); memDC.SelectObject(palette, false); metaFile.PlayOnto(memDC, size); TRACEX(OwlGDI, OWL_CDLEVEL, "TBitmap constructed @" << (void*)this << " from metafile @" << (void*)&metaFile << "."); }
extern void vardef(char *name, Binding *binding, List *defn) { Var *var; validatevar(name); for (; binding != NULL; binding = binding->next) if (streq(name, binding->name)) { binding->defn = defn; rebound = TRUE; return; } RefAdd(name); defn = callsettor(name, defn); if (isexported(name)) isdirty = TRUE; var = dictget(vars, name); if (var != NULL) if (defn != NULL) { var->defn = defn; var->env = NULL; var->flags = hasbindings(defn) ? var_hasbindings : 0; } else vars = dictput(vars, name, NULL); else if (defn != NULL) { var = mkvar(defn); vars = dictput(vars, name, var); } RefRemove(name); }
// /// Creates a TBitmap object with values from the given Clipboard. // TBitmap::TBitmap(const TClipboard& clipboard) : TGdiObject(clipboard.GetClipboardData(CF_BITMAP)) { RefAdd(Handle, Bitmap); RefInc(Handle); TRACEX(OwlGDI, OWL_CDLEVEL, "TBitmap constructed @" << (void*)this << " from clipboard."); }
// /// Construct an alias TBitmap for an existing bitmap handle // /// Creates a TBitmap object and sets the Handle data member to the given borrowed /// handle. The ShouldDelete data member defaults to false, ensuring that the /// borrowed handle will not be deleted when the C++ object is destroyed. // TBitmap::TBitmap(HBITMAP handle, TAutoDelete autoDelete) : TGdiObject(handle, autoDelete) { if (ShouldDelete) RefAdd(Handle, Bitmap); TRACEX(OwlGDI, OWL_CDLEVEL, "TBitmap constructed @" << (void*)this); }
// /// Creates a bitmap object with the values found in the given bitmap structure. // TBitmap::TBitmap(const BITMAP& bitmap) { Handle = ::CreateBitmapIndirect(&bitmap); WARNX(OwlGDI, !Handle, 0, "Cannot create bitmap from BITMAP @" << static_cast<const void*>(&bitmap)); CheckValid(); RefAdd(Handle, Bitmap); TRACEX(OwlGDI, OWL_CDLEVEL, "TBitmap constructed @" << static_cast<void*>(this) << " from BITMAP @" << static_cast<const void*>(&bitmap) << "."); }
// /// Creates a bitmap object for the given application instance from the given /// resource. // TBitmap::TBitmap(HINSTANCE instance, TResId resId) { Handle = ::LoadBitmap(instance, resId); WARNX(OwlGDI, !Handle, 0, "Cannot load bitmap " << resId << " from " << hex << (uint)instance); CheckValid(); RefAdd(Handle, Bitmap); TRACEX(OwlGDI, OWL_CDLEVEL, "TBitmap constructed from resource @" << (void*)this << "."); }
// /// Creates a TFont object from the given LOGFONT. /// This overload is deprecated; use the overload for a LOGFONT reference instead. // TFont::TFont(const LOGFONT * logFont) { PRECONDITION(logFont); Handle = ::CreateFontIndirect((LPLOGFONT)logFont); // API typecast WARNX(OwlGDI, !Handle, 0, _T("Cannot create TFont from logfont @") << hex << uint32(LPVOID(logFont))); CheckValid(); RefAdd(Handle, Font); }
// /// Creates a bitmap object from bitCount bits in the bits buffer with the given /// width, height, and planes argument values. // TBitmap::TBitmap(int width, int height, uint8 planes, uint8 bitCount, const void * bits) { Handle = ::CreateBitmap(width, height, planes, bitCount, bits); WARNX(OwlGDI, !Handle, 0, "Cannot create bitmap (" << width << "x" << height << "x" << planes << ")"); CheckValid(); RefAdd(Handle, Bitmap); TRACEX(OwlGDI, OWL_CDLEVEL, "TBitmap constructed @" << (void*)this << " using parameters (" << dec << width << "x" << height << "x" << planes << ")." << hex); }
// /// Creates a bitmap object with the values found in the given bitmap structure. /// This overload is deprecated. Use the overload that takes a reference instead. // TBitmap::TBitmap(const BITMAP * bitmap) { PRECONDITION(bitmap); Handle = ::CreateBitmapIndirect((LPBITMAP)bitmap); // API cast WARNX(OwlGDI, !Handle, 0, "Cannot create bitmap from BITMAP @" << hex << (void*)bitmap); CheckValid(); RefAdd(Handle, Bitmap); TRACEX(OwlGDI, OWL_CDLEVEL, "TBitmap constructed @" << (void*)this << " from BITMAP @" << (void*)bitmap << "."); }
// /// Creates a bitmap object for the given device context with the given dib and /// usage argument values. // TBitmap::TBitmap(const TDC& dc, const TDib& dib, uint32 usage) { Handle = ::CreateDIBitmap(dc, (BITMAPINFOHEADER *)dib.GetInfoHeader(), usage, (const uint8 *)dib.GetBits(), (BITMAPINFO *)dib.GetInfo(), dib.Usage()); // API casts WARNX(OwlGDI, !Handle, 0, "Cannot create bitmap from DIB " << hex << (uint)(HANDLE)dib << " for " << (uint)(HDC)dc); CheckValid(); RefAdd(Handle, Bitmap); TRACEX(OwlGDI, OWL_CDLEVEL, "TBitmap constructed @" << (void*)this << " from DIB for " << (uint)(HDC)dc << "."); }
// /// Creates a TFont object with the given values. // TFont::TFont(const tstring& facename, int height, int width, int escapement, int orientation, int weight, uint8 pitchAndFamily, uint8 italic, uint8 underline, uint8 strikeout, uint8 charSet, uint8 outputPrecision, uint8 clipPrecision, uint8 quality) { Handle = ::CreateFont(height, width, escapement, orientation, weight, italic, underline, strikeout, charSet, outputPrecision, clipPrecision, quality, pitchAndFamily, facename.length() > 0 ? facename.c_str() : 0); WARNX(OwlGDI, !Handle, 0, _T("Cannot create TFont (") << facename << _T(" ") << height << _T("pt)")); CheckValid(); RefAdd(Handle, Font); }
static noreturn failexec(char *file, List *args) { List *fn; assert(gcisblocked()); fn = varlookup("fn-%exec-failure", NULL); if (fn != NULL) { int olderror = errno; Ref(List *, list, append(fn, mklist(mkstr(file), args))); RefAdd(file); gcenable(); RefRemove(file); eval(list, NULL, 0); RefEnd(list); errno = olderror; } eprint("%s: %s\n", file, esstrerror(errno)); exit(1); }
// /// Create a bitmap & get its handle, given a dib and a palette /// Used by ctors here and in derived classes. Assumes Handle member can be /// over written, & adds handle to reference container. // void TBitmap::Create(const TDib& dib, const TPalette& palette) { TScreenDC dc; dc.SelectObject(palette, false); dc.RealizePalette(); Handle = ::CreateDIBitmap( dc, (LPBITMAPINFOHEADER)dib.GetInfoHeader(), CBM_INIT, (const uint8 *)dib.GetBits(), (LPBITMAPINFO)dib.GetInfo(), dib.Usage() ); // API type casts CheckValid(); RefAdd(Handle, Bitmap); }
// /// Creates a bitmap object for the given device context with the given argument /// values. // TBitmap::TBitmap(const TDC& dc, int width, int height, bool discardable) { if (discardable) { Handle = ::CreateDiscardableBitmap(dc, width, height); WARNX(OwlGDI, !Handle, 0, "Cannot create discardable bitmap (" << width << "x" << height << ") for " << hex << (uint)(HDC)dc); } else { Handle = ::CreateCompatibleBitmap(dc, width, height); WARNX(OwlGDI, !Handle, 0, "Cannot create compatible bitmap (" << width << "x" << height << ") for " << hex << (uint)(HDC)dc); } CheckValid(); RefAdd(Handle, Bitmap); if (discardable) { TRACEX(OwlGDI, OWL_CDLEVEL, "Discardable TBitmap constructed @" << (void*)this << " (" << dec << width << "x" << height << ") for " << hex << (uint)(HDC)dc << "."); } else { TRACEX(OwlGDI, OWL_CDLEVEL, "Compatible TBitmap constructed @" << (void*)this << " (" << dec << width << "x" << height << ") for " << hex << (uint)(HDC)dc << "."); } }
/* expandhome -- do tilde expansion by calling fn %home */ static char *expandhome(char *s, StrList *qp) { int c; size_t slash; List *fn = varlookup("fn-%home", NULL); assert(*s == '~'); assert(qp->str == UNQUOTED || *qp->str == 'r'); if (fn == NULL) return s; for (slash = 1; (c = s[slash]) != '/' && c != '\0'; slash++) ; Ref(char *, string, s); Ref(StrList *, quote, qp); Ref(List *, list, NULL); RefAdd(fn); if (slash > 1) list = mklist(mkstr(gcndup(s + 1, slash - 1)), NULL); RefRemove(fn); list = eval(append(fn, list), NULL, 0); if (list != NULL) { if (list->next != NULL) fail("es:expandhome", "%%home returned more than one value"); Ref(char *, home, getstr(list->term)); if (c == '\0') { string = home; quote->str = QUOTED; } else { char *q; size_t pathlen = strlen(string); size_t homelen = strlen(home); size_t len = pathlen - slash + homelen; s = gcalloc(len + 1, &StringTag); memcpy(s, home, homelen); memcpy(&s[homelen], &string[slash], pathlen - slash); s[len] = '\0'; string = s; q = quote->str; if (q == UNQUOTED) { q = gcalloc(len + 1, &StringTag); memset(q, 'q', homelen); memset(&q[homelen], 'r', pathlen - slash); q[len] = '\0'; } else if (strchr(q, 'r') == NULL) q = QUOTED; else { q = gcalloc(len + 1, &StringTag); memset(q, 'q', homelen); memcpy(&q[homelen], "e->str[slash], pathlen - slash); q[len] = '\0'; } quote->str = q; } RefEnd(home); } RefEnd2(list, quote); RefReturn(string); }
int main() { //basic std::cout << Double(Double(2)) << std::endl; std::cout << Double(3.14) << std::endl; std::cout << Double(2.222222) << std::endl; PrintPower(10); PrintPower(3.14); PrintPower(2.222); Neshto drugo(20); PrintPower(drugo); std::cout << add(5, 10) << std::endl; int a = 10, b = 20; std::cout << "a: " << a << std::endl << "b: " << b << std::endl; std::cout << "swap..." << std::endl; swap(a, b); std::cout << "a: " << a << std::endl << "b: " << b << std::endl; //arrays int p(); std::cout << "p: " << p << std::endl; int q(); std::cout << "q: " << q << std::endl; std::cout << "int(): " << int() << std::endl; int arr[10] = {1,2,3,4,5,6,7,8,9,10}; std::cout << "GET AVERAGE: " << GetAverage<int>(arr, 10) << std::endl; float f_arr[5] = {1.1,2.2,3.3,4.4,5.5}; std::cout << "int args: " << GetAverage<int>(arr, 10) << std::endl; std::cout << "float args: " << GetAverage<float>(&f_arr[0], 5) << std::endl; std::cout << "float args with int function: " << GetAverage<int>(arr, 5); int t = 10; std::cout << "t: " << t << std::endl; RefAdd(t, 5); std::cout << "t: " << t << std::endl; // This will not be compiled // Because we take a const & as // argument and the return type // is not const When break // the constness of the argument //int immutable_number = 10; //++GetTrival(immutable_number); Pair pesho = {20, "Pesho"}; PrintPair(pesho.age, pesho.name); std::string pi_key = "PI"; double pi_value = 3.14; PrintPair(pi_key, pi_value); PrintPair("Pesho", "Goshov"); // print gosho User gosho("Gosho", 2000); int user_id=25; PrintPair(user_id, gosho); // Note: // When we use a template function // with similar parameters (e.g. short, int, long) // which can be converted to one another // the template engine will not reuse the created // functions and implicitly convert the arguments // Example: // short a = 10; // int b = 200000000; // double c = 2.15; // long d = 40000000000; 40 bilions // // PrintPair(a, b); same as PrintPair<short, int> // PrintPair(b, a); --|-- PrintPair<int, short> // PrintPair(a, d); --|-- PrintPair<short, long> // PrintPair(d, c); --|-- PrintPair<long, double> Show(120); Show('B'); Show(21.5); TemplateShow(120); // will produce TemplateShow<int> TemplateShow('X'); // will produce TemplateShow<char> TemplateShow(21.5); // will produce TemplateShow<double> // the compiler will not make an analisys // of the parameter because the function // is called explicitly with type double // and the argument will be taken as double // although it can be put in int TemplateShow<double>(1234); // SizeOfElement(); // cannot be called SizeOfElement<float>(); SizeOfElement<int>(); SizeOfElement<short>(); SizeOfElement<User>(); int int_arr[10] = {1,2,3,0,4,0,2,0,9,10}; PrintNumbers(int_arr, 10); // this will skip 0-s PrintNumbers(int_arr, 10, 2); // this will skip 2-s // TEMPLATE CLASSES USAGE Item i1; i1.SetData(120); i1.PrintData(); TemplateItem<double> t1; t1.SetData(3.14); t1.PrintData(); TemplateItem<char> t2; t2.SetData('X'); char big_x = t2.GetData(); std::cout << big_x << std::endl; t2.PrintData(); //t1 = t2; //this will cause error // because t1 is instace of the template // class with double and t2 is instance with char TemplatePair<int, std::string> t_pair1; TemplatePair<int, int> salary; salary.first = 200; // leva salary.second = 30; // coins std::cout << "salary: "; salary.Print(); TemplatePair<int, int> int_pair1(10, 20); TemplatePair<int, int> int_pair2(int_pair1); int_pair1.Print(); int_pair2.Print(); // it will cause an error //TemplatePair<int, float> int_float_pair(int_pair2); // Exercise: Implement // 1. operator = // 2. operator < // 3. operator > // 4. swap method // 5. modify the constructor with default arguments int pp; int qq = int(); std::cout << "pp: " << pp << std::endl; std::cout << "qq: " << qq << std::endl; // Template of template TemplateArray<TemplatePair<int, int>, 10> t_arr_t_pair; t_arr_t_pair[0] = TemplatePair<int, int>(5, 5); t_arr_t_pair.Print(); typedef TemplatePair<int, int> Salary; TemplatePair<std::string, Salary> peshko; std::vector<int> int_vector; std::vector<TemplatePair<int, int> > int_pair_vector; // this will make a int array of 100 elements DefaultTemplateArray<> default_arr_int; DefaultTemplateArray<double> def_size_d_arr; // TEMPLATE METHODS IntArray int_arr_obj; float float_arr[11]; int_arr_obj.Copy(float_arr); // or more explicitely int_arr_obj.Copy<float>(float_arr); std::cout << "Print int_arr_obj: " << std::endl; for(int i = 0; i < 10; ++i) { std::cout << int_arr_obj[i] << " "; } std::cout << std::endl; std::cout << "Print float_arr: " << std::endl; for(int i = 0; i < 10; ++i) { std::cout << int_arr_obj[i] << " "; } std::cout << std::endl; Comparer<int> c_int(10); float c_float = 10; std::cout << std::boolalpha << c_int.IsEqual(c_float) << std::endl; Comparer<int> cc_int(40); float cc_float; double cc_double; cc_float = cc_int; cc_double = cc_int; // which will invoke // Comparer<int>::operator<float> float(); // Comparer<int>::operator<double> double(); std::cout << "cc_int: " << cc_int.Data() << "\n" << "cc_float: " << cc_float << "\n" << "cc_double: " << cc_double << "\n"; return 0; }