C++/CLR Jim Fawcett CSE687 – Object Oriented Design Spring 2009.

Slides:



Advertisements
Similar presentations
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Introduction to ASP.NET.
Advertisements

Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Coding Standard: General Rules 1.Always be consistent with existing code. 2.Adopt naming conventions consistent with selected framework. 3.Use the same.
The Type System1. 2.NET Type System The type system is the part of the CLR that defines all the types that programmers can use, and allows developers.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
Why COM and.Net? Jim Fawcett CSE775 – Distributed Objects Spring 2005.
C#/.NET Jacob Lewallen. C# vs.NET.NET is a platform. Many languages compile to.NET: –VB.NET –Python.NET –Managed C++ –C#
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
CSE 332: C++ Classes From Procedural to Object-oriented Programming Procedural programming –Functions have been the main focus so far Function parameters.
Peter Juszczyk CS 492/493 - ISGS. // Is this C# or Java? class TestApp { static void Main() { int counter = 0; counter++; } } The answer is C# - In C#
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
OOP Languages: Java vs C++
Introduction to .Net Framework
Overview of Previous Lesson(s) Over View  OOP  A class is a data type that you define to suit customized application requirements.  A class can be.
CSC300 Visual Programming Dr. Craig Reinhart. Objectives Teach the basics of C++ –You won’t be an expert but hopefully a very good novice –GUI development.
C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.
Managed C++. Objectives Overview to Visual C++.NET Concepts and architecture Developing with Managed Extensions for C++ Use cases Managed C++, Visual.
.NET Framework Danish Sami UG Lead.NetFoundry
Programming Languages and Paradigms Object-Oriented Programming (Part II)
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
Introduction to Object Oriented Programming CMSC 331.
Basic Semantics Associating meaning with language entities.
Hoang Anh Viet Hà Nội University of Technology Chapter 1. Introduction to C# Programming.
1 SystemVerilog Enhancement Requests Daniel Schostak Principal Engineer February 26 th 2010.
Area Detector Drivers Towards A Pattern Jon Thompson.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
ISBN Chapter 11 Abstract Data Types and Encapsulation Concepts.
Bill Campbell, UMB Microsoft's.NET C# and The Common Language Runtime.
OOP in C++ CS 124. Program Structure C++ Program: collection of files Source (.cpp) files be compiled separately to be linked into an executable Files.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Managing C++ CHRIS DAHLBERG MID-TIER DEVELOPER SCOTTRADE.
PerlNET: The Camel Talks.NET Jan Dubois The Perl Conference 6 San Diego, July 26 th 2002.
Object Oriented Software Development 4. C# data types, objects and references.
1 CS Programming Languages Class 22 November 14, 2000.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 10 Abstraction - The concept of abstraction is fundamental in programming - Nearly all programming.
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Classes, Interfaces and Packages
DEV394.NET Framework: Migrating To Managed Code Adam Nathan QA Lead Richard Lander Program Manager Microsoft Corporation.
The Execution System1. 2 Introduction Managed code and managed data qualify code or data that executes in cooperation with the execution engine The execution.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
INTRODUCTION BEGINNING C#. C# AND THE.NET RUNTIME AND LIBRARIES The C# compiler compiles and convert C# programs. NET Common Language Runtime (CLR) executes.
Jim Fawcett CSE687 – Object Oriented Design Spring 2015
Windows Programming Environments
CSE687 - Object Oriented Design class notes Survey of the C++ Programming Language Jim Fawcett Spring 2005.
Copyright © Jim Fawcett Spring 2017
Jim Fawcett CSE775 – Distributed Objects Spring 2017
Jim Fawcett CSE687 – Object Oriented Design Spring 2016
Jim Fawcett CSE681 – SW Modeling & Analysis Fall 2014
Jim Fawcett CSE775 – Distributed Objects Spring 2009
Abstract Data Types and Encapsulation Concepts
CSE687 - Object Oriented Design class notes Survey of the C++ Programming Language Jim Fawcett Spring 2004.
Jim Fawcett CSE687-OnLine – Object Oriented Design Summer 2017
Jim Fawcett CSE687 – Object Oriented Design Spring 2016
CS360 Windows Programming
Jim Fawcett CSE687 – Object Oriented Design Spring 2009
Lecture 9 Concepts of Programming Languages
Abstract Data Types and Encapsulation Concepts
Final Exam Review Inheritance Template Functions and Classes
Jim Fawcett CSE681 – SW Modeling & Analysis Fall 2018
Jim Fawcett CSE681 – Software Modeling and Analysis Fall 2006
Jim Fawcett CSE687 – Object Oriented Design Spring 2015
Lecture 9 Concepts of Programming Languages
Presentation transcript:

C++/CLR Jim Fawcett CSE687 – Object Oriented Design Spring 2009

References  C++/CLI –A Design Rationale for C++/CLI, Herb Sutter, –Moving C++ Applications to the Common Language Runtime, Kate Gregory, 7dfd6ea3-138a-404e-b3e ba84f22 7dfd6ea3-138a-404e-b3e ba84f22  Manged Extensions –C++/CLI in Action, Nishant Sivakumar, Manning, 2007

Comparison of Object Models  Standard C++ Object Model –All objects share a rich memory model: Static, stack, and heap –Rich object life-time model: Static objects live for the duration of the program. Objects on stack live within a scope defined by { and }. Objects on heap live at the designer’s discretion. –Semantics based on deep copy model. That’s the good news. That’s the bad news. –For compilation, a source file must include information about all the types it uses. That’s definitely bad news. But it has a work-around, e.g., design to interface not implementation. Use object factories. .Net Managed Object Model –More Spartan memory model: Value types are stack-based only. Reference types (all user defined types and library types) live on the managed heap. –Non-deterministic life-time model: All reference types are garbage collected. That’s the good news. That’s the bad news. –Semantics based on a shallow reference model. –For compilation, a source file is type checked with metadata provided by the types it uses. That is great news. It is this property that makes.Net components so simple.

.Net Object Model

Language Comparison  Standard C++ –Is an ANSI and ISO standard. –Has a standard library. –Universally available: Windows, UNIX, MAC –Well known: Large developer base. Lots of books and articles. –Programming models supported: Objects Procedural Generic –Separation of Interface from Implementation: Syntactically excellent – Implementation is separate from class declaration. Semantically poor – See object model comparison. .Net C#, Managed C++, … –Is an ECMA standard, becoming an ISO standard. –Has defined an ECMA library. –Mono project porting to UNIX –New, but gaining a lot of popularity Developer base growing quickly. Lots of books and articles. –Programming models supported: objects. –Separation of Interface from Implementation: Syntactically poor – Implementation forced in class declaration. Semantically excellent – See object model comparison.

Library Comparison  Standard C++ Library –Portable across most platforms with good standards conformance –I/O support is stream-based console, files, and, strings –Flexible container facility using Standard Template Library (STL) But no hash-table containers –No support for paths and directories –Strings, no regular expressions –No support for threads –No support for inter-process and distributed processing –No support for XML –Platform agnostic .Net Framework Class Library –Windows only but porting efforts underway –I/O support is function-based console and files –Fixed set of containers that are not very type safe. Has hash-table containers –Strong support for paths and directories –Strings and regular expressions –Thread support –Rich set of inter-process and distributed processing constructs –Support for XML processing –Deep support for Windows but very dependent on windows services like COM

Comparison of Library Functionality Functionality.Net Framework Libraries Standard C++ Library Extendable I/OWeakStrong stringsStrong Composable ContainersModerately goodStrong Paths and DirectoriesStrongNo ThreadsStrongNo SocketsModerately goodNo XMLStrongNo FormsStrongNo ReflectionStrongNo

Comparison of Library Functionality Functionality Support Provided in Code from Website Support Provided by you in Projects S’09 Extendable I/O-- strings-- Composable ContainersHashTableNo Paths and DirectoriesFileInfo, FileSystemNo ThreadsThread, Lock classesNo SocketsSocketCommunicatorNo XMLReader, Writer, no DOMXMLDOM class Forms-- ReflectionNo

Managed C++ Syntax  Include system dlls from the GAC: –#include –#include - not needed with C++/CLI  Include standard library modules in the usual way: –#include  Use scope resolution operator to define namespaces –using namespace System::Text;  Declare.Net value types on stack  Declare.Net reference types as pointers to managed heap –String^ str = gcnew String(”Hello World”);

Managed Classes  Syntax: class N { … };native C++ class ref class R { … };CLR reference type value class V { … };CLR value type interface class I { … }; CLR interface type enum class E { … };CLR enumeration type –N is a standard C++ class. None of the rules have changed. –R is a managed class of reference type. It lives on the managed heap and is referenced by a handle: R^ rh = gcnew R; delete rh; [optional: calls destructor which calls Dispose() to release unmanaged resources] Reference types may also be declared as local variables. They still live on the managed heap, but their destructors are called when the thread of execution leaves the local scope. –V is a managed class of value type. It lives in its scope of declaration. Value types must be bit-wise copyable. They have no constructors, destructors, or virtual functions. Value types may be boxed to become objects on the managed heap. –I is a managed interface. You do not declare its methods virtual. You qualify an implementing class’s methods with override (or new if you want to hide the interface’s method). –E is a managed enumeration.  N can hold “values”, handles, and references to managed types.  N can hold values, handles, and references to value types.  N can call methods of managed types.  R can call global functions and members of unmanaged classes without marshaling.  R can hold a pointer to an unmanaged object, but is responsible for creating it on the C++ heap and eventually destroying it.

From Kate Gregory’s Presentation see references NativeManaged Pointer / Handle *^ Reference &% Allocate newgcnew Free deletedelete 1 Use Native Heap 2 Use Managed Heap  Use Stack Verifiability * and & never^ and % always 1 Optional 2 Value types only

Mixing Pointers and Arrays  Managed classes hold handles to reference types: –ref class R 2{ … private: String^ rStr; };  Managed classes can also hold pointers to native types: –ref class R1 { … private: std::string* pStr; };  Unmanaged classes can hold managed handles to managed types: –class N { … private: gcroot rStr; };  Using these handles and references they can make calls on each other’s methods.  Managed arrays are declared like this: –Array ^ ssarr = gcnew array (5); –ssarr[i] = String::Concat(“Number”, i.ToString()); 0<= i <= 4  Managed arrays of value types are declared like this: –array ^ strarray = gcnew array (5); –Siarr[i] = i; 0<=i<=4;

Type Conversions C++ TypeCTS Signed TypeCTS Unsigned Type charSbyteByte short intInt16UInt16 int, __int32Int32UInt32 long intInt32UInt32 __int64Int64UInt64 floatSingleN/A doubleDoubleN/A long doubleDoubleN/A boolBooleanN/A

Extensions to Standard C++  Managed classes may have the qualifiers: –abstract –sealed  A managed class may have a constructor qualified as static, used to initialize static data members.  Managed classes may have properties: –property int Length { int get() { return _len; } void set(int value) { _len = value; } }  A managed class may declare a delegate: –delegate void someFunc(int anArg);

Managed Exceptions  A C++ exception that has a managed type is a managed exception.  Application defined exceptions are expected to derive from System::Exception.  Managed exceptions may use a finally clause: –try { … } catch(myExcept &me) { … } __finally { … }  The finally clause always executes, whether the catch handler was invoked or not.  Only reference types, including boxed value types, can be thrown.

Code Targets  An unmanaged C++ program can be compiled to generate managed code using the /clr option.  You can mix managed and unmanaged C++ code in same file.  Managed C++ can call C# code in a separate library and vice versa.

Mixing Managed and Unmanaged Code  You may freely mix unmanaged and managed C++ classes in the same compilation unit. –Managed classes may hold pointers to unmanaged objects. –Unmanaged classes may hold handles to managed objects wrapped in gcroot: #include Declare: gcroot pStr; –That helps the garbage collector track the pStr pointer. –Calls between the managed and unmanaged domains are more expensive than within either domain.  Note, all of the above means, that you can use.Net Framework Class Libraries with unmanaged code, and you can use the C++ Standard Library with managed code.

Using Frameworks in MFC from Kate Gregory’s Presentation  Visual C allows you to use new Frameworks libraries in MFC Applications  MFC includes many integration points –MFC views can host Windows Forms controls –Use your own Windows Forms dialog boxes –MFC lets you use Windows Forms as CView –Data exchange and eventing translation handled by MFC –MFC handles command routing  MFC applications will be able to take advantage of current and future libraries directly with ease

Limitations of Managed Classes  Generics and Templates are now supported.  Only single inheritance of implementation is allowed.  Managed classes can not inherit from unmanaged classes and vice versa. This may be a future addition.  No copy constructors or assignment operators are allowed.  Member functions may not have default arguments.  Friend functions and friend classes are not allowed.  Const and volatile qualifiers on member functions are currently not allowed.

Platform Invocation - PInvoke  Call Win32 API functions like this: –[DllImport(“kernel32.dll”)] extern “C” bool Beep(Int32,Int32); –Where documented signature is: BOOL Beep(DWORD,DWORD) –Or, you can call native C++ which then calls the Win32 API  Can call member functions of an exported class –See Marshaling.cpp, MarshalingLib.h

Additions to Managed C++ in VS 2005  Generics –Syntactically like templates but bind at run time –No specializations –Uses constraints to support calling functions on parameter type  Iterators –Support for each construct  Anonymous Methods –Essentially an inline delegate  Partial Types, new to C#, were always a part of C++ –Class declarations can be separate from implementation –Now, can parse declaration into parts, packaged in separate files

End of Presentation